a528404f1238836fd08dc91840042efdb5b46d47
[yaffs-website] / web / core / modules / quickedit / js / theme.es6.js
1 /**
2  * @file
3  * Provides theme functions for all of Quick Edit's client-side HTML.
4  */
5
6 (function($, Drupal) {
7   /**
8    * Theme function for a "backstage" for the Quick Edit module.
9    *
10    * @param {object} settings
11    *   Settings object used to construct the markup.
12    * @param {string} settings.id
13    *   The id to apply to the backstage.
14    *
15    * @return {string}
16    *   The corresponding HTML.
17    */
18   Drupal.theme.quickeditBackstage = function(settings) {
19     let html = '';
20     html += `<div id="${settings.id}" />`;
21     return html;
22   };
23
24   /**
25    * Theme function for a toolbar container of the Quick Edit module.
26    *
27    * @param {object} settings
28    *   Settings object used to construct the markup.
29    * @param {string} settings.id
30    *   the id to apply to the backstage.
31    *
32    * @return {string}
33    *   The corresponding HTML.
34    */
35   Drupal.theme.quickeditEntityToolbar = function(settings) {
36     let html = '';
37     html += `<div id="${
38       settings.id
39     }" class="quickedit quickedit-toolbar-container clearfix">`;
40     html += '<i class="quickedit-toolbar-pointer"></i>';
41     html += '<div class="quickedit-toolbar-content">';
42     html +=
43       '<div class="quickedit-toolbar quickedit-toolbar-entity clearfix icon icon-pencil">';
44     html += '<div class="quickedit-toolbar-label" />';
45     html += '</div>';
46     html +=
47       '<div class="quickedit-toolbar quickedit-toolbar-field clearfix" />';
48     html += '</div><div class="quickedit-toolbar-lining"></div></div>';
49     return html;
50   };
51
52   /**
53    * Theme function for a toolbar container of the Quick Edit module.
54    *
55    * @param {object} settings
56    *   Settings object used to construct the markup.
57    * @param {string} settings.entityLabel
58    *   The title of the active entity.
59    * @param {string} settings.fieldLabel
60    *   The label of the highlighted or active field.
61    *
62    * @return {string}
63    *   The corresponding HTML.
64    */
65   Drupal.theme.quickeditEntityToolbarLabel = function(settings) {
66     // @todo Add XSS regression test coverage in https://www.drupal.org/node/2547437
67     return `<span class="field">${Drupal.checkPlain(
68       settings.fieldLabel,
69     )}</span>${Drupal.checkPlain(settings.entityLabel)}`;
70   };
71
72   /**
73    * Element defining a containing box for the placement of the entity toolbar.
74    *
75    * @return {string}
76    *   The corresponding HTML.
77    */
78   Drupal.theme.quickeditEntityToolbarFence = function() {
79     return '<div id="quickedit-toolbar-fence" />';
80   };
81
82   /**
83    * Theme function for a toolbar container of the Quick Edit module.
84    *
85    * @param {object} settings
86    *   Settings object used to construct the markup.
87    * @param {string} settings.id
88    *   The id to apply to the toolbar container.
89    *
90    * @return {string}
91    *   The corresponding HTML.
92    */
93   Drupal.theme.quickeditFieldToolbar = function(settings) {
94     return `<div id="${settings.id}" />`;
95   };
96
97   /**
98    * Theme function for a toolbar toolgroup of the Quick Edit module.
99    *
100    * @param {object} settings
101    *   Settings object used to construct the markup.
102    * @param {string} [settings.id]
103    *   The id of the toolgroup.
104    * @param {string} settings.classes
105    *   The class of the toolgroup.
106    * @param {Array} settings.buttons
107    *   See {@link Drupal.theme.quickeditButtons}.
108    *
109    * @return {string}
110    *   The corresponding HTML.
111    */
112   Drupal.theme.quickeditToolgroup = function(settings) {
113     // Classes.
114     const classes = settings.classes || [];
115     classes.unshift('quickedit-toolgroup');
116     let html = '';
117     html += `<div class="${classes.join(' ')}"`;
118     if (settings.id) {
119       html += ` id="${settings.id}"`;
120     }
121     html += '>';
122     html += Drupal.theme('quickeditButtons', { buttons: settings.buttons });
123     html += '</div>';
124     return html;
125   };
126
127   /**
128    * Theme function for buttons of the Quick Edit module.
129    *
130    * Can be used for the buttons both in the toolbar toolgroups and in the
131    * modal.
132    *
133    * @param {object} settings
134    *   Settings object used to construct the markup.
135    * @param {Array} settings.buttons
136    * - String type: the type of the button (defaults to 'button')
137    * - Array classes: the classes of the button.
138    * - String label: the label of the button.
139    *
140    * @return {string}
141    *   The corresponding HTML.
142    */
143   Drupal.theme.quickeditButtons = function(settings) {
144     let html = '';
145     for (let i = 0; i < settings.buttons.length; i++) {
146       const button = settings.buttons[i];
147       if (!button.hasOwnProperty('type')) {
148         button.type = 'button';
149       }
150       // Attributes.
151       const attributes = [];
152       const attrMap = settings.buttons[i].attributes || {};
153       Object.keys(attrMap).forEach(attr => {
154         attributes.push(attr + (attrMap[attr] ? `="${attrMap[attr]}"` : ''));
155       });
156       html += `<button type="${button.type}" class="${
157         button.classes
158       }" ${attributes.join(' ')}>${button.label}</button>`;
159     }
160     return html;
161   };
162
163   /**
164    * Theme function for a form container of the Quick Edit module.
165    *
166    * @param {object} settings
167    *   Settings object used to construct the markup.
168    * @param {string} settings.id
169    *   The id to apply to the toolbar container.
170    * @param {string} settings.loadingMsg
171    *   The message to show while loading.
172    *
173    * @return {string}
174    *   The corresponding HTML.
175    */
176   Drupal.theme.quickeditFormContainer = function(settings) {
177     let html = '';
178     html += `<div id="${settings.id}" class="quickedit-form-container">`;
179     html += '  <div class="quickedit-form">';
180     html += '    <div class="placeholder">';
181     html += settings.loadingMsg;
182     html += '    </div>';
183     html += '  </div>';
184     html += '</div>';
185     return html;
186   };
187 })(jQuery, Drupal);