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