theme.es6.js 5.6 KB

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