@teipublisher/pb-components 1.43.5 → 1.44.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/pb-ajax.js CHANGED
@@ -2,9 +2,9 @@ import { LitElement, html, css } from 'lit-element';
2
2
  import '@polymer/iron-ajax';
3
3
  import '@polymer/paper-dialog';
4
4
  import '@polymer/paper-dialog-scrollable';
5
- import { unsafeHTML } from 'lit-html/directives/unsafe-html.js';
6
5
  import { pbMixin } from './pb-mixin.js';
7
- import { translate } from "./pb-i18n.js";
6
+ import { get as i18n } from "./pb-i18n.js";
7
+ import './pb-message.js';
8
8
 
9
9
  /**
10
10
  * Triggers an action on the server and shows a dialog
@@ -14,7 +14,7 @@ import { translate } from "./pb-i18n.js";
14
14
  * The parameters sent to the server-side script will be copied
15
15
  * from the `pb-view` to which this component subscribes, see pb-update event.
16
16
  *
17
- * @slot - unnamed slot for link text
17
+ * @slot - unnamed slot for link content
18
18
  * @slot title - dialog title
19
19
  *
20
20
  * @fires pb-start-update - Fired before the element updates its content
@@ -51,6 +51,19 @@ export class PbAjax extends pbMixin(LitElement) {
51
51
  event: {
52
52
  type: String
53
53
  },
54
+ /**
55
+ * If set, display a confirmation dialog with the message text given in
56
+ * this property. The user may cancel the action.
57
+ */
58
+ confirm: {
59
+ type: String
60
+ },
61
+ /**
62
+ * Set to not show the server's response
63
+ */
64
+ quiet: {
65
+ type: Boolean
66
+ },
54
67
  _message: {
55
68
  type: String
56
69
  }
@@ -60,6 +73,8 @@ export class PbAjax extends pbMixin(LitElement) {
60
73
  constructor() {
61
74
  super();
62
75
  this.method = 'get';
76
+ this.confirm = null;
77
+ this.quiet = false;
63
78
  }
64
79
 
65
80
  connectedCallback() {
@@ -78,22 +93,16 @@ export class PbAjax extends pbMixin(LitElement) {
78
93
  with-credentials
79
94
  @error="${this._handleError}"
80
95
  @response="${this._handleResponse}"></iron-ajax>
81
- ${this.dialogTemplate}
96
+ <pb-message id="confirmDialog"></pb-message>
97
+ <slot name="title" style="display: none"></slot>
82
98
  `;
83
99
  }
84
100
 
85
- get dialogTemplate() {
86
- return html`
87
- <paper-dialog id="messageDialog">
88
- <h2><slot name="title">Action</slot></h2>
89
- <paper-dialog-scrollable>${unsafeHTML(this._message)}</paper-dialog-scrollable>
90
- <div class="buttons">
91
- <paper-button dialog-confirm="dialog-confirm" autofocus="autofocus">
92
- ${translate('dialogs.close')}
93
- </paper-button>
94
- </div>
95
- </paper-dialog>
96
- `;
101
+ firstUpdated() {
102
+ super.firstUpdated();
103
+ const slot = this.shadowRoot.querySelector('slot[name=title]');
104
+ this._dialogTitle = '';
105
+ slot.assignedNodes().forEach(node => {this._dialogTitle += node.innerHTML});
97
106
  }
98
107
 
99
108
  static get styles() {
@@ -109,7 +118,14 @@ export class PbAjax extends pbMixin(LitElement) {
109
118
 
110
119
  _handleClick(ev) {
111
120
  ev.preventDefault();
112
- this.trigger();
121
+ if (this.confirm) {
122
+ const dialog = this.shadowRoot.getElementById('confirmDialog');
123
+ dialog.confirm(this._dialogTitle, i18n(this.confirm))
124
+ .then(() => this.trigger())
125
+ .catch(() => console.log('<pb-ajax> Action cancelled'));
126
+ } else {
127
+ this.trigger();
128
+ }
113
129
  }
114
130
 
115
131
  trigger() {
@@ -122,8 +138,10 @@ export class PbAjax extends pbMixin(LitElement) {
122
138
  _handleResponse() {
123
139
  const resp = this.shadowRoot.getElementById('loadContent').lastResponse;
124
140
  this._message = resp;
125
- const dialog = this.shadowRoot.getElementById('messageDialog');
126
- dialog.open();
141
+ if (!this.quiet) {
142
+ const dialog = this.shadowRoot.getElementById('confirmDialog');
143
+ dialog.show(this._dialogTitle, this._message);
144
+ }
127
145
 
128
146
  this.emitTo('pb-end-update');
129
147