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