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