You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

239 lines
5.4 KiB

1 year ago
  1. /**
  2. * @output wp-admin/js/gallery.js
  3. */
  4. /* global unescape, getUserSetting, setUserSetting, wpgallery, tinymce */
  5. jQuery( function($) {
  6. var gallerySortable, gallerySortableInit, sortIt, clearAll, w, desc = false;
  7. gallerySortableInit = function() {
  8. gallerySortable = $('#media-items').sortable( {
  9. items: 'div.media-item',
  10. placeholder: 'sorthelper',
  11. axis: 'y',
  12. distance: 2,
  13. handle: 'div.filename',
  14. stop: function() {
  15. // When an update has occurred, adjust the order for each item.
  16. var all = $('#media-items').sortable('toArray'), len = all.length;
  17. $.each(all, function(i, id) {
  18. var order = desc ? (len - i) : (1 + i);
  19. $('#' + id + ' .menu_order input').val(order);
  20. });
  21. }
  22. } );
  23. };
  24. sortIt = function() {
  25. var all = $('.menu_order_input'), len = all.length;
  26. all.each(function(i){
  27. var order = desc ? (len - i) : (1 + i);
  28. $(this).val(order);
  29. });
  30. };
  31. clearAll = function(c) {
  32. c = c || 0;
  33. $('.menu_order_input').each( function() {
  34. if ( this.value === '0' || c ) {
  35. this.value = '';
  36. }
  37. });
  38. };
  39. $('#asc').on( 'click', function( e ) {
  40. e.preventDefault();
  41. desc = false;
  42. sortIt();
  43. });
  44. $('#desc').on( 'click', function( e ) {
  45. e.preventDefault();
  46. desc = true;
  47. sortIt();
  48. });
  49. $('#clear').on( 'click', function( e ) {
  50. e.preventDefault();
  51. clearAll(1);
  52. });
  53. $('#showall').on( 'click', function( e ) {
  54. e.preventDefault();
  55. $('#sort-buttons span a').toggle();
  56. $('a.describe-toggle-on').hide();
  57. $('a.describe-toggle-off, table.slidetoggle').show();
  58. $('img.pinkynail').toggle(false);
  59. });
  60. $('#hideall').on( 'click', function( e ) {
  61. e.preventDefault();
  62. $('#sort-buttons span a').toggle();
  63. $('a.describe-toggle-on').show();
  64. $('a.describe-toggle-off, table.slidetoggle').hide();
  65. $('img.pinkynail').toggle(true);
  66. });
  67. // Initialize sortable.
  68. gallerySortableInit();
  69. clearAll();
  70. if ( $('#media-items>*').length > 1 ) {
  71. w = wpgallery.getWin();
  72. $('#save-all, #gallery-settings').show();
  73. if ( typeof w.tinyMCE !== 'undefined' && w.tinyMCE.activeEditor && ! w.tinyMCE.activeEditor.isHidden() ) {
  74. wpgallery.mcemode = true;
  75. wpgallery.init();
  76. } else {
  77. $('#insert-gallery').show();
  78. }
  79. }
  80. });
  81. /* gallery settings */
  82. window.tinymce = null;
  83. window.wpgallery = {
  84. mcemode : false,
  85. editor : {},
  86. dom : {},
  87. is_update : false,
  88. el : {},
  89. I : function(e) {
  90. return document.getElementById(e);
  91. },
  92. init: function() {
  93. var t = this, li, q, i, it, w = t.getWin();
  94. if ( ! t.mcemode ) {
  95. return;
  96. }
  97. li = ('' + document.location.search).replace(/^\?/, '').split('&');
  98. q = {};
  99. for (i=0; i<li.length; i++) {
  100. it = li[i].split('=');
  101. q[unescape(it[0])] = unescape(it[1]);
  102. }
  103. if ( q.mce_rdomain ) {
  104. document.domain = q.mce_rdomain;
  105. }
  106. // Find window & API.
  107. window.tinymce = w.tinymce;
  108. window.tinyMCE = w.tinyMCE;
  109. t.editor = tinymce.EditorManager.activeEditor;
  110. t.setup();
  111. },
  112. getWin : function() {
  113. return window.dialogArguments || opener || parent || top;
  114. },
  115. setup : function() {
  116. var t = this, a, ed = t.editor, g, columns, link, order, orderby;
  117. if ( ! t.mcemode ) {
  118. return;
  119. }
  120. t.el = ed.selection.getNode();
  121. if ( t.el.nodeName !== 'IMG' || ! ed.dom.hasClass(t.el, 'wpGallery') ) {
  122. if ( ( g = ed.dom.select('img.wpGallery') ) && g[0] ) {
  123. t.el = g[0];
  124. } else {
  125. if ( getUserSetting('galfile') === '1' ) {
  126. t.I('linkto-file').checked = 'checked';
  127. }
  128. if ( getUserSetting('galdesc') === '1' ) {
  129. t.I('order-desc').checked = 'checked';
  130. }
  131. if ( getUserSetting('galcols') ) {
  132. t.I('columns').value = getUserSetting('galcols');
  133. }
  134. if ( getUserSetting('galord') ) {
  135. t.I('orderby').value = getUserSetting('galord');
  136. }
  137. jQuery('#insert-gallery').show();
  138. return;
  139. }
  140. }
  141. a = ed.dom.getAttrib(t.el, 'title');
  142. a = ed.dom.decode(a);
  143. if ( a ) {
  144. jQuery('#update-gallery').show();
  145. t.is_update = true;
  146. columns = a.match(/columns=['"]([0-9]+)['"]/);
  147. link = a.match(/link=['"]([^'"]+)['"]/i);
  148. order = a.match(/order=['"]([^'"]+)['"]/i);
  149. orderby = a.match(/orderby=['"]([^'"]+)['"]/i);
  150. if ( link && link[1] ) {
  151. t.I('linkto-file').checked = 'checked';
  152. }
  153. if ( order && order[1] ) {
  154. t.I('order-desc').checked = 'checked';
  155. }
  156. if ( columns && columns[1] ) {
  157. t.I('columns').value = '' + columns[1];
  158. }
  159. if ( orderby && orderby[1] ) {
  160. t.I('orderby').value = orderby[1];
  161. }
  162. } else {
  163. jQuery('#insert-gallery').show();
  164. }
  165. },
  166. update : function() {
  167. var t = this, ed = t.editor, all = '', s;
  168. if ( ! t.mcemode || ! t.is_update ) {
  169. s = '[gallery' + t.getSettings() + ']';
  170. t.getWin().send_to_editor(s);
  171. return;
  172. }
  173. if ( t.el.nodeName !== 'IMG' ) {
  174. return;
  175. }
  176. all = ed.dom.decode( ed.dom.getAttrib( t.el, 'title' ) );
  177. all = all.replace(/\s*(order|link|columns|orderby)=['"]([^'"]+)['"]/gi, '');
  178. all += t.getSettings();
  179. ed.dom.setAttrib(t.el, 'title', all);
  180. t.getWin().tb_remove();
  181. },
  182. getSettings : function() {
  183. var I = this.I, s = '';
  184. if ( I('linkto-file').checked ) {
  185. s += ' link="file"';
  186. setUserSetting('galfile', '1');
  187. }
  188. if ( I('order-desc').checked ) {
  189. s += ' order="DESC"';
  190. setUserSetting('galdesc', '1');
  191. }
  192. if ( I('columns').value !== 3 ) {
  193. s += ' columns="' + I('columns').value + '"';
  194. setUserSetting('galcols', I('columns').value);
  195. }
  196. if ( I('orderby').value !== 'menu_order' ) {
  197. s += ' orderby="' + I('orderby').value + '"';
  198. setUserSetting('galord', I('orderby').value);
  199. }
  200. return s;
  201. }
  202. };