theme.es6.js 5.6 KB

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