wp/wp-includes/js/wplink.js
author ymh <ymh.work@gmail.com>
Tue, 09 Jun 2015 03:35:32 +0200
changeset 5 5e2f62d02dcd
parent 0 d970ebf37754
child 7 cf61fcea0001
permissions -rw-r--r--
upgrade wordpress + plugins
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
     1
/* global ajaxurl, tinymce, wpLinkL10n, setUserSetting, wpActiveEditor */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
var wpLink;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
     4
( function( $ ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
     5
	var editor, searchTimer, River, Query, correctedURL,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
     6
		inputs = {},
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
     7
		rivers = {},
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
     8
		isTouch = ( 'ontouchend' in document );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
     9
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    10
	function getLink() {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    11
		return editor.dom.getParent( editor.selection.getNode(), 'a' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    12
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
	wpLink = {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
		timeToTriggerRiver: 150,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
		minRiverAJAXDuration: 200,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
		riverBottomThreshold: 5,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
		keySensitivity: 100,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
		lastSearch: '',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
		textarea: '',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    22
		init: function() {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    23
			inputs.wrap = $('#wp-link-wrap');
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    24
			inputs.dialog = $( '#wp-link' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    25
			inputs.backdrop = $( '#wp-link-backdrop' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    26
			inputs.submit = $( '#wp-link-submit' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    27
			inputs.close = $( '#wp-link-close' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    28
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    29
			// Input
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    30
			inputs.text = $( '#wp-link-text' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    31
			inputs.url = $( '#wp-link-url' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    32
			inputs.nonce = $( '#_ajax_linking_nonce' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    33
			inputs.openInNewTab = $( '#wp-link-target' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    34
			inputs.search = $( '#wp-link-search' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    35
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
			// Build Rivers
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    37
			rivers.search = new River( $( '#search-results' ) );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    38
			rivers.recent = new River( $( '#most-recent-results' ) );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    39
			rivers.elements = inputs.dialog.find( '.query-results' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    40
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    41
			// Get search notice text
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    42
			inputs.queryNotice = $( '#query-notice-message' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    43
			inputs.queryNoticeTextDefault = inputs.queryNotice.find( '.query-notice-default' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    44
			inputs.queryNoticeTextHint = inputs.queryNotice.find( '.query-notice-hint' );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
			// Bind event handlers
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
			inputs.dialog.keydown( wpLink.keydown );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
			inputs.dialog.keyup( wpLink.keyup );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    49
			inputs.submit.click( function( event ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    50
				event.preventDefault();
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
				wpLink.update();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
			});
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    53
			inputs.close.add( inputs.backdrop ).add( '#wp-link-cancel a' ).click( function( event ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    54
				event.preventDefault();
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
				wpLink.close();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
			});
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    57
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    58
			$( '#wp-link-search-toggle' ).on( 'click', wpLink.toggleInternalLinking );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    59
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    60
			rivers.elements.on( 'river-select', wpLink.updateFields );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    62
			// Display 'hint' message when search field or 'query-results' box are focused
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    63
			inputs.search.on( 'focus.wplink', function() {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    64
				inputs.queryNoticeTextDefault.hide();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    65
				inputs.queryNoticeTextHint.removeClass( 'screen-reader-text' ).show();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    66
			} ).on( 'blur.wplink', function() {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    67
				inputs.queryNoticeTextDefault.show();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    68
				inputs.queryNoticeTextHint.addClass( 'screen-reader-text' ).hide();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    69
			} );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    70
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    71
			inputs.search.keyup( function() {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    72
				var self = this;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    74
				window.clearTimeout( searchTimer );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    75
				searchTimer = window.setTimeout( function() {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    76
					wpLink.searchInternalLinks.call( self );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    77
				}, 500 );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    78
			});
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    79
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    80
			function correctURL() {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    81
				var url = $.trim( inputs.url.val() );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    83
				if ( url && correctedURL !== url && ! /^(?:[a-z]+:|#|\?|\.|\/)/.test( url ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    84
					inputs.url.val( 'http://' + url );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    85
					correctedURL = url;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    86
				}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    87
			}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    88
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    89
			inputs.url.on( 'paste', function() {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    90
				setTimeout( correctURL, 0 );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    91
			} );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    92
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    93
			inputs.url.on( 'blur', correctURL );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
		},
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    95
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    96
		open: function( editorId ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    97
			var ed;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    98
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    99
			$( document.body ).addClass( 'modal-open' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   100
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   101
			wpLink.range = null;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   102
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   103
			if ( editorId ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   104
				window.wpActiveEditor = editorId;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   105
			}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   106
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   107
			if ( ! window.wpActiveEditor ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   108
				return;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   109
			}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   110
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   111
			this.textarea = $( '#' + window.wpActiveEditor ).get( 0 );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   112
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   113
			if ( typeof tinymce !== 'undefined' ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   114
				ed = tinymce.get( wpActiveEditor );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   115
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   116
				if ( ed && ! ed.isHidden() ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   117
					editor = ed;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   118
				} else {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   119
					editor = null;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   120
				}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   121
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   122
				if ( editor && tinymce.isIE ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   123
					editor.windowManager.bookmark = editor.selection.getBookmark();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   124
				}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   125
			}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   126
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   127
			if ( ! wpLink.isMCE() && document.selection ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   128
				this.textarea.focus();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   129
				this.range = document.selection.createRange();
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   130
			}
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   131
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   132
			inputs.wrap.show();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   133
			inputs.backdrop.show();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   134
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   135
			wpLink.refresh();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   136
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   137
			$( document ).trigger( 'wplink-open', inputs.wrap );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   138
		},
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   139
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   140
		isMCE: function() {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   141
			return editor && ! editor.isHidden();
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   142
		},
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   143
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   144
		refresh: function() {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   145
			var text = '';
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   146
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   147
			// Refresh rivers (clear links, check visibility)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   148
			rivers.search.refresh();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   149
			rivers.recent.refresh();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   150
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   151
			if ( wpLink.isMCE() ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   152
				wpLink.mceRefresh();
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   153
			} else {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   154
				// For the Text editor the "Link text" field is always shown
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   155
				if ( ! inputs.wrap.hasClass( 'has-text-field' ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   156
					inputs.wrap.addClass( 'has-text-field' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   157
				}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   158
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   159
				if ( document.selection ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   160
					// Old IE
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   161
					text = document.selection.createRange().text || '';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   162
				} else if ( typeof this.textarea.selectionStart !== 'undefined' &&
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   163
					( this.textarea.selectionStart !== this.textarea.selectionEnd ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   164
					// W3C
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   165
					text = this.textarea.value.substring( this.textarea.selectionStart, this.textarea.selectionEnd ) || '';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   166
				}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   167
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   168
				inputs.text.val( text );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   169
				wpLink.setDefaultValues();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   170
			}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   171
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   172
			if ( isTouch ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   173
				// Close the onscreen keyboard
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   174
				inputs.url.focus().blur();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   175
			} else {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   176
				// Focus the URL field and highlight its contents.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   177
				// If this is moved above the selection changes,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   178
				// IE will show a flashing cursor over the dialog.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   179
				inputs.url.focus()[0].select();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   180
			}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   181
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   182
			// Load the most recent results if this is the first time opening the panel.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   183
			if ( ! rivers.recent.ul.children().length ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   184
				rivers.recent.ajax();
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   185
			}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   186
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   187
			correctedURL = inputs.url.val().replace( /^http:\/\//, '' );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   188
		},
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   189
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   190
		hasSelectedText: function( linkNode ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   191
			var html = editor.selection.getContent();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   192
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   193
			// Partial html and not a fully selected anchor element
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   194
			if ( /</.test( html ) && ( ! /^<a [^>]+>[^<]+<\/a>$/.test( html ) || html.indexOf('href=') === -1 ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   195
				return false;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   196
			}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   197
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   198
			if ( linkNode ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   199
				var nodes = linkNode.childNodes, i;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   200
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   201
				if ( nodes.length === 0 ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   202
					return false;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   203
				}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   204
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   205
				for ( i = nodes.length - 1; i >= 0; i-- ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   206
					if ( nodes[i].nodeType != 3 ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   207
						return false;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   208
					}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   209
				}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   210
			}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   211
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   212
			return true;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   213
		},
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   214
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   215
		mceRefresh: function() {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   216
			var text,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   217
				selectedNode = editor.selection.getNode(),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   218
				linkNode = editor.dom.getParent( selectedNode, 'a[href]' ),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   219
				onlyText = this.hasSelectedText( linkNode );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   220
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   221
			if ( linkNode ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   222
				text = linkNode.innerText || linkNode.textContent;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   223
				inputs.url.val( editor.dom.getAttrib( linkNode, 'href' ) );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   224
				inputs.openInNewTab.prop( 'checked', '_blank' === editor.dom.getAttrib( linkNode, 'target' ) );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   225
				inputs.submit.val( wpLinkL10n.update );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   226
			} else {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   227
				text = editor.selection.getContent({ format: 'text' });
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   228
				this.setDefaultValues();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   229
			}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   230
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   231
			if ( onlyText ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   232
				inputs.text.val( text || '' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   233
				inputs.wrap.addClass( 'has-text-field' );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   234
			} else {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   235
				inputs.text.val( '' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   236
				inputs.wrap.removeClass( 'has-text-field' );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   237
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   238
		},
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   239
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   240
		close: function() {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   241
			$( document.body ).removeClass( 'modal-open' );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   242
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   243
			if ( ! wpLink.isMCE() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   244
				wpLink.textarea.focus();
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   245
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   246
				if ( wpLink.range ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   247
					wpLink.range.moveToBookmark( wpLink.range.getBookmark() );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   248
					wpLink.range.select();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   249
				}
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   250
			} else {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   251
				editor.focus();
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   252
			}
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   253
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   254
			inputs.backdrop.hide();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   255
			inputs.wrap.hide();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   256
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   257
			correctedURL = false;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   258
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   259
			$( document ).trigger( 'wplink-close', inputs.wrap );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   260
		},
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   261
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   262
		getAttrs: function() {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   263
			return {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   264
				href: $.trim( inputs.url.val() ),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   265
				target: inputs.openInNewTab.prop( 'checked' ) ? '_blank' : ''
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   266
			};
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   267
		},
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   268
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   269
		update: function() {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   270
			if ( wpLink.isMCE() ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   271
				wpLink.mceUpdate();
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   272
			} else {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   273
				wpLink.htmlUpdate();
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   274
			}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   275
		},
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   276
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   277
		htmlUpdate: function() {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   278
			var attrs, text, html, begin, end, cursor, selection,
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   279
				textarea = wpLink.textarea;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   280
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   281
			if ( ! textarea ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   282
				return;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   283
			}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   284
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   285
			attrs = wpLink.getAttrs();
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   286
			text = inputs.text.val();
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   287
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   288
			// If there's no href, return.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   289
			if ( ! attrs.href ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   290
				return;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   291
			}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   292
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   293
			// Build HTML
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   294
			html = '<a href="' + attrs.href + '"';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   295
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   296
			if ( attrs.target ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   297
				html += ' target="' + attrs.target + '"';
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   298
			}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   299
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   300
			html += '>';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   301
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   302
			// Insert HTML
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   303
			if ( document.selection && wpLink.range ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   304
				// IE
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   305
				// Note: If no text is selected, IE will not place the cursor
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   306
				//       inside the closing tag.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   307
				textarea.focus();
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   308
				wpLink.range.text = html + ( text || wpLink.range.text ) + '</a>';
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   309
				wpLink.range.moveToBookmark( wpLink.range.getBookmark() );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   310
				wpLink.range.select();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   311
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   312
				wpLink.range = null;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   313
			} else if ( typeof textarea.selectionStart !== 'undefined' ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   314
				// W3C
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   315
				begin = textarea.selectionStart;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   316
				end = textarea.selectionEnd;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   317
				selection = text || textarea.value.substring( begin, end );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   318
				html = html + selection + '</a>';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   319
				cursor = begin + html.length;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   320
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   321
				// If no text is selected, place the cursor inside the closing tag.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   322
				if ( begin === end && ! selection ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   323
					cursor -= 4;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   324
				}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   325
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   326
				textarea.value = (
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   327
					textarea.value.substring( 0, begin ) +
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   328
					html +
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   329
					textarea.value.substring( end, textarea.value.length )
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   330
				);
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   331
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   332
				// Update cursor position
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   333
				textarea.selectionStart = textarea.selectionEnd = cursor;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   334
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   335
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   336
			wpLink.close();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   337
			textarea.focus();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   338
		},
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   339
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   340
		mceUpdate: function() {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   341
			var attrs = wpLink.getAttrs(),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   342
				link, text;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   343
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   344
			wpLink.close();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   345
			editor.focus();
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   346
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   347
			if ( tinymce.isIE ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   348
				editor.selection.moveToBookmark( editor.windowManager.bookmark );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   349
			}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   350
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   351
			if ( ! attrs.href ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   352
				editor.execCommand( 'unlink' );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   353
				return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   354
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   355
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   356
			link = getLink();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   357
			text = inputs.text.val();
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   358
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   359
			if ( link ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   360
				if ( text ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   361
					if ( 'innerText' in link ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   362
						link.innerText = text;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   363
					} else {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   364
						link.textContent = text;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   365
					}
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   366
				}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   367
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   368
				editor.dom.setAttribs( link, attrs );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   369
			} else {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   370
				if ( text ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   371
					editor.selection.setNode( editor.dom.create( 'a', attrs, text ) );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   372
				} else {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   373
					editor.execCommand( 'mceInsertLink', false, attrs );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   374
				}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   375
			}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   376
		},
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   377
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   378
		updateFields: function( e, li ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   379
			inputs.url.val( li.children( '.item-permalink' ).val() );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   380
		},
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   381
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   382
		setDefaultValues: function() {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   383
			var selection,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   384
				emailRegexp = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   385
				urlRegexp = /^(https?|ftp):\/\/[A-Z0-9.-]+\.[A-Z]{2,4}[^ "]*$/i;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   386
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   387
			if ( this.isMCE() ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   388
				selection = editor.selection.getContent();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   389
			} else if ( document.selection && wpLink.range ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   390
				selection = wpLink.range.text;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   391
			} else if ( typeof this.textarea.selectionStart !== 'undefined' ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   392
				selection = this.textarea.value.substring( this.textarea.selectionStart, this.textarea.selectionEnd );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   393
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   394
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   395
			if ( selection && emailRegexp.test( selection ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   396
				// Selection is email address
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   397
				inputs.url.val( 'mailto:' + selection );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   398
			} else if ( selection && urlRegexp.test( selection ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   399
				// Selection is URL
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   400
				inputs.url.val( selection.replace( /&amp;|&#0?38;/gi, '&' ) );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   401
			} else {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   402
				// Set URL to default.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   403
				inputs.url.val( '' );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   404
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   405
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   406
			// Update save prompt.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   407
			inputs.submit.val( wpLinkL10n.save );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   408
		},
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   409
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   410
		searchInternalLinks: function() {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   411
			var t = $( this ), waiting,
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   412
				search = t.val();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   413
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   414
			if ( search.length > 2 ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   415
				rivers.recent.hide();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   416
				rivers.search.show();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   417
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   418
				// Don't search if the keypress didn't change the title.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   419
				if ( wpLink.lastSearch == search )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   420
					return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   421
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   422
				wpLink.lastSearch = search;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   423
				waiting = t.parent().find( '.spinner' ).addClass( 'is-active' );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   424
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   425
				rivers.search.change( search );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   426
				rivers.search.ajax( function() {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   427
					waiting.removeClass( 'is-active' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   428
				});
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   429
			} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   430
				rivers.search.hide();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   431
				rivers.recent.show();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   432
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   433
		},
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   434
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   435
		next: function() {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   436
			rivers.search.next();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   437
			rivers.recent.next();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   438
		},
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   439
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   440
		prev: function() {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   441
			rivers.search.prev();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   442
			rivers.recent.prev();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   443
		},
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   444
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   445
		keydown: function( event ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   446
			var fn, id,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   447
				key = $.ui.keyCode;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   448
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   449
			if ( key.ESCAPE === event.keyCode ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   450
				wpLink.close();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   451
				event.stopImmediatePropagation();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   452
			} else if ( key.TAB === event.keyCode ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   453
				id = event.target.id;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   454
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   455
				// wp-link-submit must always be the last focusable element in the dialog.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   456
				// following focusable elements will be skipped on keyboard navigation.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   457
				if ( id === 'wp-link-submit' && ! event.shiftKey ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   458
					inputs.close.focus();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   459
					event.preventDefault();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   460
				} else if ( id === 'wp-link-close' && event.shiftKey ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   461
					inputs.submit.focus();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   462
					event.preventDefault();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   463
				}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   464
			}
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   465
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   466
			if ( event.keyCode !== key.UP && event.keyCode !== key.DOWN ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   467
				return;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   468
			}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   469
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   470
			if ( document.activeElement &&
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   471
				( document.activeElement.id === 'link-title-field' || document.activeElement.id === 'url-field' ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   472
				return;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   473
			}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   474
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   475
			fn = event.keyCode === key.UP ? 'prev' : 'next';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   476
			clearInterval( wpLink.keyInterval );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   477
			wpLink[ fn ]();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   478
			wpLink.keyInterval = setInterval( wpLink[ fn ], wpLink.keySensitivity );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   479
			event.preventDefault();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   480
		},
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   481
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   482
		keyup: function( event ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   483
			var key = $.ui.keyCode;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   484
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   485
			if ( event.which === key.UP || event.which === key.DOWN ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   486
				clearInterval( wpLink.keyInterval );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   487
				event.preventDefault();
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   488
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   489
		},
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   490
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   491
		delayedCallback: function( func, delay ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   492
			var timeoutTriggered, funcTriggered, funcArgs, funcContext;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   493
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   494
			if ( ! delay )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   495
				return func;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   496
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   497
			setTimeout( function() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   498
				if ( funcTriggered )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   499
					return func.apply( funcContext, funcArgs );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   500
				// Otherwise, wait.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   501
				timeoutTriggered = true;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   502
			}, delay );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   503
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   504
			return function() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   505
				if ( timeoutTriggered )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   506
					return func.apply( this, arguments );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   507
				// Otherwise, wait.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   508
				funcArgs = arguments;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   509
				funcContext = this;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   510
				funcTriggered = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   511
			};
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   512
		},
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   513
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   514
		toggleInternalLinking: function( event ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   515
			var visible = inputs.wrap.hasClass( 'search-panel-visible' );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   516
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   517
			inputs.wrap.toggleClass( 'search-panel-visible', ! visible );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   518
			setUserSetting( 'wplink', visible ? '0' : '1' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   519
			inputs[ ! visible ? 'search' : 'url' ].focus();
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   520
			event.preventDefault();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   521
		}
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   522
	};
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   523
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   524
	River = function( element, search ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   525
		var self = this;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   526
		this.element = element;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   527
		this.ul = element.children( 'ul' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   528
		this.contentHeight = element.children( '#link-selector-height' );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   529
		this.waiting = element.find('.river-waiting');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   530
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   531
		this.change( search );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   532
		this.refresh();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   533
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   534
		$( '#wp-link .query-results, #wp-link #link-selector' ).scroll( function() {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   535
			self.maybeLoad();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   536
		});
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   537
		element.on( 'click', 'li', function( event ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   538
			self.select( $( this ), event );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   539
		});
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   540
	};
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   541
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   542
	$.extend( River.prototype, {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   543
		refresh: function() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   544
			this.deselect();
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   545
			this.visible = this.element.is( ':visible' );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   546
		},
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   547
		show: function() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   548
			if ( ! this.visible ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   549
				this.deselect();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   550
				this.element.show();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   551
				this.visible = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   552
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   553
		},
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   554
		hide: function() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   555
			this.element.hide();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   556
			this.visible = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   557
		},
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   558
		// Selects a list item and triggers the river-select event.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   559
		select: function( li, event ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   560
			var liHeight, elHeight, liTop, elTop;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   561
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   562
			if ( li.hasClass( 'unselectable' ) || li == this.selected )
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   563
				return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   564
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   565
			this.deselect();
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   566
			this.selected = li.addClass( 'selected' );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   567
			// Make sure the element is visible
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   568
			liHeight = li.outerHeight();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   569
			elHeight = this.element.height();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   570
			liTop = li.position().top;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   571
			elTop = this.element.scrollTop();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   572
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   573
			if ( liTop < 0 ) // Make first visible element
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   574
				this.element.scrollTop( elTop + liTop );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   575
			else if ( liTop + liHeight > elHeight ) // Make last visible element
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   576
				this.element.scrollTop( elTop + liTop - elHeight + liHeight );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   577
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   578
			// Trigger the river-select event
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   579
			this.element.trigger( 'river-select', [ li, event, this ] );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   580
		},
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   581
		deselect: function() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   582
			if ( this.selected )
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   583
				this.selected.removeClass( 'selected' );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   584
			this.selected = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   585
		},
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   586
		prev: function() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   587
			if ( ! this.visible )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   588
				return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   589
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   590
			var to;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   591
			if ( this.selected ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   592
				to = this.selected.prev( 'li' );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   593
				if ( to.length )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   594
					this.select( to );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   595
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   596
		},
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   597
		next: function() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   598
			if ( ! this.visible )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   599
				return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   600
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   601
			var to = this.selected ? this.selected.next( 'li' ) : $( 'li:not(.unselectable):first', this.element );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   602
			if ( to.length )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   603
				this.select( to );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   604
		},
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   605
		ajax: function( callback ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   606
			var self = this,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   607
				delay = this.query.page == 1 ? 0 : wpLink.minRiverAJAXDuration,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   608
				response = wpLink.delayedCallback( function( results, params ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   609
					self.process( results, params );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   610
					if ( callback )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   611
						callback( results, params );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   612
				}, delay );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   613
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   614
			this.query.ajax( response );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   615
		},
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   616
		change: function( search ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   617
			if ( this.query && this._search == search )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   618
				return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   619
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   620
			this._search = search;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   621
			this.query = new Query( search );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   622
			this.element.scrollTop( 0 );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   623
		},
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   624
		process: function( results, params ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   625
			var list = '', alt = true, classes = '',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   626
				firstPage = params.page == 1;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   627
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   628
			if ( ! results ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   629
				if ( firstPage ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   630
					list += '<li class="unselectable no-matches-found"><span class="item-title"><em>' +
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   631
						wpLinkL10n.noMatchesFound + '</em></span></li>';
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   632
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   633
			} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   634
				$.each( results, function() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   635
					classes = alt ? 'alternate' : '';
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   636
					classes += this.title ? '' : ' no-title';
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   637
					list += classes ? '<li class="' + classes + '">' : '<li>';
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   638
					list += '<input type="hidden" class="item-permalink" value="' + this.permalink + '" />';
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   639
					list += '<span class="item-title">';
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   640
					list += this.title ? this.title : wpLinkL10n.noTitle;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   641
					list += '</span><span class="item-info">' + this.info + '</span></li>';
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   642
					alt = ! alt;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   643
				});
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   644
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   645
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   646
			this.ul[ firstPage ? 'html' : 'append' ]( list );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   647
		},
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   648
		maybeLoad: function() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   649
			var self = this,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   650
				el = this.element,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   651
				bottom = el.scrollTop() + el.height();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   652
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   653
			if ( ! this.query.ready() || bottom < this.contentHeight.height() - wpLink.riverBottomThreshold )
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   654
				return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   655
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   656
			setTimeout(function() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   657
				var newTop = el.scrollTop(),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   658
					newBottom = newTop + el.height();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   659
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   660
				if ( ! self.query.ready() || newBottom < self.contentHeight.height() - wpLink.riverBottomThreshold )
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   661
					return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   662
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   663
				self.waiting.addClass( 'is-active' );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   664
				el.scrollTop( newTop + self.waiting.outerHeight() );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   665
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   666
				self.ajax( function() {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   667
					self.waiting.removeClass( 'is-active' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   668
				});
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   669
			}, wpLink.timeToTriggerRiver );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   670
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   671
	});
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   672
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   673
	Query = function( search ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   674
		this.page = 1;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   675
		this.allLoaded = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   676
		this.querying = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   677
		this.search = search;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   678
	};
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   679
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   680
	$.extend( Query.prototype, {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   681
		ready: function() {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   682
			return ! ( this.querying || this.allLoaded );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   683
		},
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   684
		ajax: function( callback ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   685
			var self = this,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   686
				query = {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   687
					action : 'wp-link-ajax',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   688
					page : this.page,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   689
					'_ajax_linking_nonce' : inputs.nonce.val()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   690
				};
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   691
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   692
			if ( this.search )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   693
				query.search = this.search;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   694
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   695
			this.querying = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   696
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   697
			$.post( ajaxurl, query, function( r ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   698
				self.page++;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   699
				self.querying = false;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   700
				self.allLoaded = ! r;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   701
				callback( r, query );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   702
			}, 'json' );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   703
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   704
	});
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   705
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   706
	$( document ).ready( wpLink.init );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   707
})( jQuery );