]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/js/tinymce/plugins/wplink/js/wplink.dev.js
Wordpress 3.1
[autoinstalls/wordpress.git] / wp-includes / js / tinymce / plugins / wplink / js / wplink.dev.js
1 var wpLink;
2
3 (function($){
4         var inputs = {}, rivers = {}, ed, River, Query;
5
6         wpLink = {
7                 timeToTriggerRiver: 150,
8                 minRiverAJAXDuration: 200,
9                 riverBottomThreshold: 5,
10                 keySensitivity: 100,
11                 lastSearch: '',
12                 init : function() {
13                         inputs.dialog = $('#wp-link');
14                         inputs.submit = $('#wp-link-submit');
15                         // URL
16                         inputs.url = $('#url-field');
17                         // Secondary options
18                         inputs.title = $('#link-title-field');
19                         // Advanced Options
20                         inputs.openInNewTab = $('#link-target-checkbox');
21                         inputs.search = $('#search-field');
22                         // Build Rivers
23                         rivers.search = new River( $('#search-results') );
24                         rivers.recent = new River( $('#most-recent-results') );
25                         rivers.elements = $('.query-results', inputs.dialog);
26
27                         // Bind event handlers
28                         inputs.dialog.keydown( wpLink.keydown );
29                         inputs.dialog.keyup( wpLink.keyup );
30                         inputs.submit.click( function(e){
31                                 wpLink.update();
32                                 e.preventDefault();
33                         });
34                         $('#wp-link-cancel').click( wpLink.cancel );
35                         $('#internal-toggle').click( wpLink.toggleInternalLinking );
36
37                         rivers.elements.bind('river-select', wpLink.updateFields );
38
39                         inputs.search.keyup( wpLink.searchInternalLinks );
40
41                         inputs.dialog.bind('wpdialogrefresh', wpLink.refresh);
42                 },
43
44                 refresh : function() {
45                         var e;
46                         ed = tinyMCEPopup.editor;
47
48                         // Refresh rivers (clear links, check visibility)
49                         rivers.search.refresh();
50                         rivers.recent.refresh();
51
52                         tinyMCEPopup.restoreSelection();
53
54                         // If link exists, select proper values.
55                         if ( e = ed.dom.getParent(ed.selection.getNode(), 'A') ) {
56                                 // Set URL and description.
57                                 inputs.url.val( e.href );
58                                 inputs.title.val( ed.dom.getAttrib(e, 'title') );
59                                 // Set open in new tab.
60                                 if ( "_blank" == ed.dom.getAttrib(e, 'target') )
61                                         inputs.openInNewTab.attr('checked','checked');
62                                 // Update save prompt.
63                                 inputs.submit.val( wpLinkL10n.update );
64
65                         // If there's no link, set the default values.
66                         } else {
67                                 wpLink.setDefaultValues();
68                                 // Update save prompt.
69                                 inputs.submit.val( wpLinkL10n.save );
70                         }
71
72                         tinyMCEPopup.storeSelection();
73                         // Focus the URL field and highlight its contents.
74                         //     If this is moved above the selection changes,
75                         //     IE will show a flashing cursor over the dialog.
76                         inputs.url.focus()[0].select();
77                         // Load the most recent results if this is the first time opening the panel.
78                         if ( ! rivers.recent.ul.children().length )
79                                 rivers.recent.ajax();
80                 },
81
82                 cancel : function() {
83                         tinyMCEPopup.close();
84                 },
85
86                 update : function() {
87                         var ed = tinyMCEPopup.editor,
88                                 attrs = {
89                                         href : inputs.url.val(),
90                                         title : inputs.title.val(),
91                                         target : inputs.openInNewTab.attr('checked') ? '_blank' : ''
92                                 }, e, b;
93
94                         tinyMCEPopup.restoreSelection();
95                         e = ed.dom.getParent(ed.selection.getNode(), 'A');
96
97                         // If the values are empty, unlink and return
98                         if ( ! attrs.href || attrs.href == 'http://' ) {
99                                 if ( e ) {
100                                         tinyMCEPopup.execCommand("mceBeginUndoLevel");
101                                         b = ed.selection.getBookmark();
102                                         ed.dom.remove(e, 1);
103                                         ed.selection.moveToBookmark(b);
104                                         tinyMCEPopup.execCommand("mceEndUndoLevel");
105                                         tinyMCEPopup.close();
106                                 }
107                                 return;
108                         }
109
110                         tinyMCEPopup.execCommand("mceBeginUndoLevel");
111
112                         if (e == null) {
113                                 ed.getDoc().execCommand("unlink", false, null);
114                                 tinyMCEPopup.execCommand("CreateLink", false, "#mce_temp_url#", {skip_undo : 1});
115
116                                 tinymce.each(ed.dom.select("a"), function(n) {
117                                         if (ed.dom.getAttrib(n, 'href') == '#mce_temp_url#') {
118                                                 e = n;
119                                                 ed.dom.setAttribs(e, attrs);
120                                         }
121                                 });
122
123                                 // Sometimes WebKit lets a user create a link where
124                                 // they shouldn't be able to. In this case, CreateLink
125                                 // injects "#mce_temp_url#" into their content. Fix it.
126                                 if ( $(e).text() == '#mce_temp_url#' ) {
127                                         ed.dom.remove(e);
128                                         e = null;
129                                 }
130                         } else {
131                                 ed.dom.setAttribs(e, attrs);
132                         }
133
134                         // Don't move caret if selection was image
135                         if ( e && (e.childNodes.length != 1 || e.firstChild.nodeName != 'IMG') ) {
136                                 ed.focus();
137                                 ed.selection.select(e);
138                                 ed.selection.collapse(0);
139                                 tinyMCEPopup.storeSelection();
140                         }
141
142                         tinyMCEPopup.execCommand("mceEndUndoLevel");
143                         tinyMCEPopup.close();
144                 },
145
146                 updateFields : function( e, li, originalEvent ) {
147                         inputs.url.val( li.children('.item-permalink').val() );
148                         inputs.title.val( li.hasClass('no-title') ? '' : li.children('.item-title').text() );
149                         if ( originalEvent && originalEvent.type == "click" )
150                                 inputs.url.focus();
151                 },
152                 setDefaultValues : function() {
153                         // Set URL and description to defaults.
154                         // Leave the new tab setting as-is.
155                         inputs.url.val('http://');
156                         inputs.title.val('');
157                 },
158
159                 searchInternalLinks : function() {
160                         var t = $(this), waiting,
161                                 search = t.val();
162
163                         if ( search.length > 2 ) {
164                                 rivers.recent.hide();
165                                 rivers.search.show();
166
167                                 // Don't search if the keypress didn't change the title.
168                                 if ( wpLink.lastSearch == search )
169                                         return;
170
171                                 wpLink.lastSearch = search;
172                                 waiting = t.siblings('img.waiting').show();
173
174                                 rivers.search.change( search );
175                                 rivers.search.ajax( function(){ waiting.hide(); });
176                         } else {
177                                 rivers.search.hide();
178                                 rivers.recent.show();
179                         }
180                 },
181
182                 next : function() {
183                         rivers.search.next();
184                         rivers.recent.next();
185                 },
186                 prev : function() {
187                         rivers.search.prev();
188                         rivers.recent.prev();
189                 },
190
191                 keydown : function( event ) {
192                         var fn, key = $.ui.keyCode;
193
194                         switch( event.which ) {
195                                 case key.UP:
196                                         fn = 'prev';
197                                 case key.DOWN:
198                                         fn = fn || 'next';
199                                         clearInterval( wpLink.keyInterval );
200                                         wpLink[ fn ]();
201                                         wpLink.keyInterval = setInterval( wpLink[ fn ], wpLink.keySensitivity );
202                                         break;
203                                 default:
204                                         return;
205                         }
206                         event.preventDefault();
207                 },
208                 keyup: function( event ) {
209                         var key = $.ui.keyCode;
210
211                         switch( event.which ) {
212                                 case key.ESCAPE:
213                                         wpLink.cancel();
214                                         break;
215                                 case key.UP:
216                                 case key.DOWN:
217                                         clearInterval( wpLink.keyInterval );
218                                         break;
219                                 default:
220                                         return;
221                         }
222                         event.preventDefault();
223                 },
224
225                 delayedCallback : function( func, delay ) {
226                         var timeoutTriggered, funcTriggered, funcArgs, funcContext;
227
228                         if ( ! delay )
229                                 return func;
230
231                         setTimeout( function() {
232                                 if ( funcTriggered )
233                                         return func.apply( funcContext, funcArgs );
234                                 // Otherwise, wait.
235                                 timeoutTriggered = true;
236                         }, delay);
237
238                         return function() {
239                                 if ( timeoutTriggered )
240                                         return func.apply( this, arguments );
241                                 // Otherwise, wait.
242                                 funcArgs = arguments;
243                                 funcContext = this;
244                                 funcTriggered = true;
245                         };
246                 },
247
248                 toggleInternalLinking : function( event ) {
249                         var panel = $('#search-panel'),
250                                 widget = inputs.dialog.wpdialog('widget'),
251                                 // We're about to toggle visibility; it's currently the opposite
252                                 visible = !panel.is(':visible'),
253                                 win = $(window);
254
255                         $(this).toggleClass('toggle-arrow-active', visible);
256
257                         inputs.dialog.height('auto');
258                         panel.slideToggle( 300, function() {
259                                 setUserSetting('wplink', visible ? '1' : '0');
260                                 inputs[ visible ? 'search' : 'url' ].focus();
261
262                                 // Move the box if the box is now expanded, was opened in a collapsed state,
263                                 // and if it needs to be moved. (Judged by bottom not being positive or
264                                 // bottom being smaller than top.)
265                                 var scroll = win.scrollTop(),
266                                         top = widget.offset().top,
267                                         bottom = top + widget.outerHeight(),
268                                         diff = bottom - win.height();
269
270                                 if ( diff > scroll ) {
271                                         widget.animate({'top': diff < top ?  top - diff : scroll }, 200);
272                                 }
273                         });
274                         event.preventDefault();
275                 }
276         }
277
278         River = function( element, search ) {
279                 var self = this;
280                 this.element = element;
281                 this.ul = element.children('ul');
282                 this.waiting = element.find('.river-waiting');
283
284                 this.change( search );
285                 this.refresh();
286
287                 element.scroll( function(){ self.maybeLoad(); });
288                 element.delegate('li', 'click', function(e){ self.select( $(this), e ); });
289         };
290
291         $.extend( River.prototype, {
292                 refresh: function() {
293                         this.deselect();
294                         this.visible = this.element.is(':visible');
295                 },
296                 show: function() {
297                         if ( ! this.visible ) {
298                                 this.deselect();
299                                 this.element.show();
300                                 this.visible = true;
301                         }
302                 },
303                 hide: function() {
304                         this.element.hide();
305                         this.visible = false;
306                 },
307                 // Selects a list item and triggers the river-select event.
308                 select: function( li, event ) {
309                         var liHeight, elHeight, liTop, elTop;
310
311                         if ( li.hasClass('unselectable') || li == this.selected )
312                                 return;
313
314                         this.deselect();
315                         this.selected = li.addClass('selected');
316                         // Make sure the element is visible
317                         liHeight = li.outerHeight();
318                         elHeight = this.element.height();
319                         liTop = li.position().top;
320                         elTop = this.element.scrollTop();
321
322                         if ( liTop < 0 ) // Make first visible element
323                                 this.element.scrollTop( elTop + liTop );
324                         else if ( liTop + liHeight > elHeight ) // Make last visible element
325                                 this.element.scrollTop( elTop + liTop - elHeight + liHeight );
326
327                         // Trigger the river-select event
328                         this.element.trigger('river-select', [ li, event, this ]);
329                 },
330                 deselect: function() {
331                         if ( this.selected )
332                                 this.selected.removeClass('selected');
333                         this.selected = false;
334                 },
335                 prev: function() {
336                         if ( ! this.visible )
337                                 return;
338
339                         var to;
340                         if ( this.selected ) {
341                                 to = this.selected.prev('li');
342                                 if ( to.length )
343                                         this.select( to );
344                         }
345                 },
346                 next: function() {
347                         if ( ! this.visible )
348                                 return;
349
350                         var to = this.selected ? this.selected.next('li') : $('li:not(.unselectable):first', this.element);
351                         if ( to.length )
352                                 this.select( to );
353                 },
354                 ajax: function( callback ) {
355                         var self = this,
356                                 delay = this.query.page == 1 ? 0 : wpLink.minRiverAJAXDuration,
357                                 response = wpLink.delayedCallback( function( results, params ) {
358                                         self.process( results, params );
359                                         if ( callback )
360                                                 callback( results, params );
361                                 }, delay );
362
363                         this.query.ajax( response );
364                 },
365                 change: function( search ) {
366                         if ( this.query && this._search == search )
367                                 return;
368
369                         this._search = search;
370                         this.query = new Query( search );
371                         this.element.scrollTop(0);
372                 },
373                 process: function( results, params ) {
374                         var list = '', alt = true, classes = '',
375                                 firstPage = params.page == 1;
376
377                         if ( !results ) {
378                                 if ( firstPage ) {
379                                         list += '<li class="unselectable"><span class="item-title"><em>'
380                                         + wpLinkL10n.noMatchesFound
381                                         + '</em></span></li>';
382                                 }
383                         } else {
384                                 $.each( results, function() {
385                                         classes = alt ? 'alternate' : '';
386                                         classes += this['title'] ? '' : ' no-title';
387                                         list += classes ? '<li class="' + classes + '">' : '<li>';
388                                         list += '<input type="hidden" class="item-permalink" value="' + this['permalink'] + '" />';
389                                         list += '<span class="item-title">';
390                                         list += this['title'] ? this['title'] : wpLinkL10n.noTitle;
391                                         list += '</span><span class="item-info">' + this['info'] + '</span></li>';
392                                         alt = ! alt;
393                                 });
394                         }
395
396                         this.ul[ firstPage ? 'html' : 'append' ]( list );
397                 },
398                 maybeLoad: function() {
399                         var self = this,
400                                 el = this.element,
401                                 bottom = el.scrollTop() + el.height();
402
403                         if ( ! this.query.ready() || bottom < this.ul.height() - wpLink.riverBottomThreshold )
404                                 return;
405
406                         setTimeout(function() {
407                                 var newTop = el.scrollTop(),
408                                         newBottom = newTop + el.height();
409
410                                 if ( ! self.query.ready() || newBottom < self.ul.height() - wpLink.riverBottomThreshold )
411                                         return;
412
413                                 self.waiting.show();
414                                 el.scrollTop( newTop + self.waiting.outerHeight() );
415
416                                 self.ajax( function() { self.waiting.hide(); });
417                         }, wpLink.timeToTriggerRiver );
418                 }
419         });
420
421         Query = function( search ) {
422                 this.page = 1;
423                 this.allLoaded = false;
424                 this.querying = false;
425                 this.search = search;
426         };
427
428         $.extend( Query.prototype, {
429                 ready: function() {
430                         return !( this.querying || this.allLoaded );
431                 },
432                 ajax: function( callback ) {
433                         var self = this,
434                                 query = {
435                                         action : 'wp-link-ajax',
436                                         page : this.page,
437                                         '_ajax_linking_nonce' : $('#_ajax_linking_nonce').val()
438                                 };
439
440                         if ( this.search )
441                                 query.search = this.search;
442
443                         this.querying = true;
444
445                         $.post( ajaxurl, query, function(r) {
446                                 self.page++;
447                                 self.querying = false;
448                                 self.allLoaded = !r;
449                                 callback( r, query );
450                         }, "json" );
451                 }
452         });
453
454         $(document).ready( wpLink.init );
455 })(jQuery);