mp-wp_genesis 1 // script.aculo.us controls.js v1.8.0, Tue Nov 06 15:01:40 +0300 2007
mp-wp_genesis 2
mp-wp_genesis 3 // Copyright (c) 2005-2007 Thomas Fuchs (http://script.aculo.us, http://mir.aculo.us)
mp-wp_genesis 4 // (c) 2005-2007 Ivan Krstic (http://blogs.law.harvard.edu/ivan)
mp-wp_genesis 5 // (c) 2005-2007 Jon Tirsen (http://www.tirsen.com)
mp-wp_genesis 6 // Contributors:
mp-wp_genesis 7 // Richard Livsey
mp-wp_genesis 8 // Rahul Bhargava
mp-wp_genesis 9 // Rob Wills
mp-wp_genesis 10 //
mp-wp_genesis 11 // script.aculo.us is freely distributable under the terms of an MIT-style license.
mp-wp_genesis 12 // For details, see the script.aculo.us web site: http://script.aculo.us/
mp-wp_genesis 13
mp-wp_genesis 14 // Autocompleter.Base handles all the autocompletion functionality
mp-wp_genesis 15 // that's independent of the data source for autocompletion. This
mp-wp_genesis 16 // includes drawing the autocompletion menu, observing keyboard
mp-wp_genesis 17 // and mouse events, and similar.
mp-wp_genesis 18 //
mp-wp_genesis 19 // Specific autocompleters need to provide, at the very least,
mp-wp_genesis 20 // a getUpdatedChoices function that will be invoked every time
mp-wp_genesis 21 // the text inside the monitored textbox changes. This method
mp-wp_genesis 22 // should get the text for which to provide autocompletion by
mp-wp_genesis 23 // invoking this.getToken(), NOT by directly accessing
mp-wp_genesis 24 // this.element.value. This is to allow incremental tokenized
mp-wp_genesis 25 // autocompletion. Specific auto-completion logic (AJAX, etc)
mp-wp_genesis 26 // belongs in getUpdatedChoices.
mp-wp_genesis 27 //
mp-wp_genesis 28 // Tokenized incremental autocompletion is enabled automatically
mp-wp_genesis 29 // when an autocompleter is instantiated with the 'tokens' option
mp-wp_genesis 30 // in the options parameter, e.g.:
mp-wp_genesis 31 // new Ajax.Autocompleter('id','upd', '/url/', { tokens: ',' });
mp-wp_genesis 32 // will incrementally autocomplete with a comma as the token.
mp-wp_genesis 33 // Additionally, ',' in the above example can be replaced with
mp-wp_genesis 34 // a token array, e.g. { tokens: [',', '\n'] } which
mp-wp_genesis 35 // enables autocompletion on multiple tokens. This is most
mp-wp_genesis 36 // useful when one of the tokens is \n (a newline), as it
mp-wp_genesis 37 // allows smart autocompletion after linebreaks.
mp-wp_genesis 38
mp-wp_genesis 39 if(typeof Effect == 'undefined')
mp-wp_genesis 40 throw("controls.js requires including script.aculo.us' effects.js library");
mp-wp_genesis 41
mp-wp_genesis 42 var Autocompleter = { }
mp-wp_genesis 43 Autocompleter.Base = Class.create({
mp-wp_genesis 44 baseInitialize: function(element, update, options) {
mp-wp_genesis 45 element = $(element)
mp-wp_genesis 46 this.element = element;
mp-wp_genesis 47 this.update = $(update);
mp-wp_genesis 48 this.hasFocus = false;
mp-wp_genesis 49 this.changed = false;
mp-wp_genesis 50 this.active = false;
mp-wp_genesis 51 this.index = 0;
mp-wp_genesis 52 this.entryCount = 0;
mp-wp_genesis 53 this.oldElementValue = this.element.value;
mp-wp_genesis 54
mp-wp_genesis 55 if(this.setOptions)
mp-wp_genesis 56 this.setOptions(options);
mp-wp_genesis 57 else
mp-wp_genesis 58 this.options = options || { };
mp-wp_genesis 59
mp-wp_genesis 60 this.options.paramName = this.options.paramName || this.element.name;
mp-wp_genesis 61 this.options.tokens = this.options.tokens || [];
mp-wp_genesis 62 this.options.frequency = this.options.frequency || 0.4;
mp-wp_genesis 63 this.options.minChars = this.options.minChars || 1;
mp-wp_genesis 64 this.options.onShow = this.options.onShow ||
mp-wp_genesis 65 function(element, update){
mp-wp_genesis 66 if(!update.style.position || update.style.position=='absolute') {
mp-wp_genesis 67 update.style.position = 'absolute';
mp-wp_genesis 68 Position.clone(element, update, {
mp-wp_genesis 69 setHeight: false,
mp-wp_genesis 70 offsetTop: element.offsetHeight
mp-wp_genesis 71 });
mp-wp_genesis 72 }
mp-wp_genesis 73 Effect.Appear(update,{duration:0.15});
mp-wp_genesis 74 };
mp-wp_genesis 75 this.options.onHide = this.options.onHide ||
mp-wp_genesis 76 function(element, update){ new Effect.Fade(update,{duration:0.15}) };
mp-wp_genesis 77
mp-wp_genesis 78 if(typeof(this.options.tokens) == 'string')
mp-wp_genesis 79 this.options.tokens = new Array(this.options.tokens);
mp-wp_genesis 80 // Force carriage returns as token delimiters anyway
mp-wp_genesis 81 if (!this.options.tokens.include('\n'))
mp-wp_genesis 82 this.options.tokens.push('\n');
mp-wp_genesis 83
mp-wp_genesis 84 this.observer = null;
mp-wp_genesis 85
mp-wp_genesis 86 this.element.setAttribute('autocomplete','off');
mp-wp_genesis 87
mp-wp_genesis 88 Element.hide(this.update);
mp-wp_genesis 89
mp-wp_genesis 90 Event.observe(this.element, 'blur', this.onBlur.bindAsEventListener(this));
mp-wp_genesis 91 Event.observe(this.element, 'keypress', this.onKeyPress.bindAsEventListener(this));
mp-wp_genesis 92 },
mp-wp_genesis 93
mp-wp_genesis 94 show: function() {
mp-wp_genesis 95 if(Element.getStyle(this.update, 'display')=='none') this.options.onShow(this.element, this.update);
mp-wp_genesis 96 if(!this.iefix &&
mp-wp_genesis 97 (Prototype.Browser.IE) &&
mp-wp_genesis 98 (Element.getStyle(this.update, 'position')=='absolute')) {
mp-wp_genesis 99 new Insertion.After(this.update,
mp-wp_genesis 100 '<iframe id="' + this.update.id + '_iefix" '+
mp-wp_genesis 101 'style="display:none;position:absolute;filter:progid:DXImageTransform.Microsoft.Alpha(opacity=0);" ' +
mp-wp_genesis 102 'src="javascript:false;" frameborder="0" scrolling="no"></iframe>');
mp-wp_genesis 103 this.iefix = $(this.update.id+'_iefix');
mp-wp_genesis 104 }
mp-wp_genesis 105 if(this.iefix) setTimeout(this.fixIEOverlapping.bind(this), 50);
mp-wp_genesis 106 },
mp-wp_genesis 107
mp-wp_genesis 108 fixIEOverlapping: function() {
mp-wp_genesis 109 Position.clone(this.update, this.iefix, {setTop:(!this.update.style.height)});
mp-wp_genesis 110 this.iefix.style.zIndex = 1;
mp-wp_genesis 111 this.update.style.zIndex = 2;
mp-wp_genesis 112 Element.show(this.iefix);
mp-wp_genesis 113 },
mp-wp_genesis 114
mp-wp_genesis 115 hide: function() {
mp-wp_genesis 116 this.stopIndicator();
mp-wp_genesis 117 if(Element.getStyle(this.update, 'display')!='none') this.options.onHide(this.element, this.update);
mp-wp_genesis 118 if(this.iefix) Element.hide(this.iefix);
mp-wp_genesis 119 },
mp-wp_genesis 120
mp-wp_genesis 121 startIndicator: function() {
mp-wp_genesis 122 if(this.options.indicator) Element.show(this.options.indicator);
mp-wp_genesis 123 },
mp-wp_genesis 124
mp-wp_genesis 125 stopIndicator: function() {
mp-wp_genesis 126 if(this.options.indicator) Element.hide(this.options.indicator);
mp-wp_genesis 127 },
mp-wp_genesis 128
mp-wp_genesis 129 onKeyPress: function(event) {
mp-wp_genesis 130 if(this.active)
mp-wp_genesis 131 switch(event.keyCode) {
mp-wp_genesis 132 case Event.KEY_TAB:
mp-wp_genesis 133 case Event.KEY_RETURN:
mp-wp_genesis 134 this.selectEntry();
mp-wp_genesis 135 Event.stop(event);
mp-wp_genesis 136 case Event.KEY_ESC:
mp-wp_genesis 137 this.hide();
mp-wp_genesis 138 this.active = false;
mp-wp_genesis 139 Event.stop(event);
mp-wp_genesis 140 return;
mp-wp_genesis 141 case Event.KEY_LEFT:
mp-wp_genesis 142 case Event.KEY_RIGHT:
mp-wp_genesis 143 return;
mp-wp_genesis 144 case Event.KEY_UP:
mp-wp_genesis 145 this.markPrevious();
mp-wp_genesis 146 this.render();
mp-wp_genesis 147 if(Prototype.Browser.WebKit) Event.stop(event);
mp-wp_genesis 148 return;
mp-wp_genesis 149 case Event.KEY_DOWN:
mp-wp_genesis 150 this.markNext();
mp-wp_genesis 151 this.render();
mp-wp_genesis 152 if(Prototype.Browser.WebKit) Event.stop(event);
mp-wp_genesis 153 return;
mp-wp_genesis 154 }
mp-wp_genesis 155 else
mp-wp_genesis 156 if(event.keyCode==Event.KEY_TAB || event.keyCode==Event.KEY_RETURN ||
mp-wp_genesis 157 (Prototype.Browser.WebKit > 0 && event.keyCode == 0)) return;
mp-wp_genesis 158
mp-wp_genesis 159 this.changed = true;
mp-wp_genesis 160 this.hasFocus = true;
mp-wp_genesis 161
mp-wp_genesis 162 if(this.observer) clearTimeout(this.observer);
mp-wp_genesis 163 this.observer =
mp-wp_genesis 164 setTimeout(this.onObserverEvent.bind(this), this.options.frequency*1000);
mp-wp_genesis 165 },
mp-wp_genesis 166
mp-wp_genesis 167 activate: function() {
mp-wp_genesis 168 this.changed = false;
mp-wp_genesis 169 this.hasFocus = true;
mp-wp_genesis 170 this.getUpdatedChoices();
mp-wp_genesis 171 },
mp-wp_genesis 172
mp-wp_genesis 173 onHover: function(event) {
mp-wp_genesis 174 var element = Event.findElement(event, 'LI');
mp-wp_genesis 175 if(this.index != element.autocompleteIndex)
mp-wp_genesis 176 {
mp-wp_genesis 177 this.index = element.autocompleteIndex;
mp-wp_genesis 178 this.render();
mp-wp_genesis 179 }
mp-wp_genesis 180 Event.stop(event);
mp-wp_genesis 181 },
mp-wp_genesis 182
mp-wp_genesis 183 onClick: function(event) {
mp-wp_genesis 184 var element = Event.findElement(event, 'LI');
mp-wp_genesis 185 this.index = element.autocompleteIndex;
mp-wp_genesis 186 this.selectEntry();
mp-wp_genesis 187 this.hide();
mp-wp_genesis 188 },
mp-wp_genesis 189
mp-wp_genesis 190 onBlur: function(event) {
mp-wp_genesis 191 // needed to make click events working
mp-wp_genesis 192 setTimeout(this.hide.bind(this), 250);
mp-wp_genesis 193 this.hasFocus = false;
mp-wp_genesis 194 this.active = false;
mp-wp_genesis 195 },
mp-wp_genesis 196
mp-wp_genesis 197 render: function() {
mp-wp_genesis 198 if(this.entryCount > 0) {
mp-wp_genesis 199 for (var i = 0; i < this.entryCount; i++)
mp-wp_genesis 200 this.index==i ?
mp-wp_genesis 201 Element.addClassName(this.getEntry(i),"selected") :
mp-wp_genesis 202 Element.removeClassName(this.getEntry(i),"selected");
mp-wp_genesis 203 if(this.hasFocus) {
mp-wp_genesis 204 this.show();
mp-wp_genesis 205 this.active = true;
mp-wp_genesis 206 }
mp-wp_genesis 207 } else {
mp-wp_genesis 208 this.active = false;
mp-wp_genesis 209 this.hide();
mp-wp_genesis 210 }
mp-wp_genesis 211 },
mp-wp_genesis 212
mp-wp_genesis 213 markPrevious: function() {
mp-wp_genesis 214 if(this.index > 0) this.index--
mp-wp_genesis 215 else this.index = this.entryCount-1;
mp-wp_genesis 216 this.getEntry(this.index).scrollIntoView(true);
mp-wp_genesis 217 },
mp-wp_genesis 218
mp-wp_genesis 219 markNext: function() {
mp-wp_genesis 220 if(this.index < this.entryCount-1) this.index++
mp-wp_genesis 221 else this.index = 0;
mp-wp_genesis 222 this.getEntry(this.index).scrollIntoView(false);
mp-wp_genesis 223 },
mp-wp_genesis 224
mp-wp_genesis 225 getEntry: function(index) {
mp-wp_genesis 226 return this.update.firstChild.childNodes[index];
mp-wp_genesis 227 },
mp-wp_genesis 228
mp-wp_genesis 229 getCurrentEntry: function() {
mp-wp_genesis 230 return this.getEntry(this.index);
mp-wp_genesis 231 },
mp-wp_genesis 232
mp-wp_genesis 233 selectEntry: function() {
mp-wp_genesis 234 this.active = false;
mp-wp_genesis 235 this.updateElement(this.getCurrentEntry());
mp-wp_genesis 236 },
mp-wp_genesis 237
mp-wp_genesis 238 updateElement: function(selectedElement) {
mp-wp_genesis 239 if (this.options.updateElement) {
mp-wp_genesis 240 this.options.updateElement(selectedElement);
mp-wp_genesis 241 return;
mp-wp_genesis 242 }
mp-wp_genesis 243 var value = '';
mp-wp_genesis 244 if (this.options.select) {
mp-wp_genesis 245 var nodes = $(selectedElement).select('.' + this.options.select) || [];
mp-wp_genesis 246 if(nodes.length>0) value = Element.collectTextNodes(nodes[0], this.options.select);
mp-wp_genesis 247 } else
mp-wp_genesis 248 value = Element.collectTextNodesIgnoreClass(selectedElement, 'informal');
mp-wp_genesis 249
mp-wp_genesis 250 var bounds = this.getTokenBounds();
mp-wp_genesis 251 if (bounds[0] != -1) {
mp-wp_genesis 252 var newValue = this.element.value.substr(0, bounds[0]);
mp-wp_genesis 253 var whitespace = this.element.value.substr(bounds[0]).match(/^\s+/);
mp-wp_genesis 254 if (whitespace)
mp-wp_genesis 255 newValue += whitespace[0];
mp-wp_genesis 256 this.element.value = newValue + value + this.element.value.substr(bounds[1]);
mp-wp_genesis 257 } else {
mp-wp_genesis 258 this.element.value = value;
mp-wp_genesis 259 }
mp-wp_genesis 260 this.oldElementValue = this.element.value;
mp-wp_genesis 261 this.element.focus();
mp-wp_genesis 262
mp-wp_genesis 263 if (this.options.afterUpdateElement)
mp-wp_genesis 264 this.options.afterUpdateElement(this.element, selectedElement);
mp-wp_genesis 265 },
mp-wp_genesis 266
mp-wp_genesis 267 updateChoices: function(choices) {
mp-wp_genesis 268 if(!this.changed && this.hasFocus) {
mp-wp_genesis 269 this.update.innerHTML = choices;
mp-wp_genesis 270 Element.cleanWhitespace(this.update);
mp-wp_genesis 271 Element.cleanWhitespace(this.update.down());
mp-wp_genesis 272
mp-wp_genesis 273 if(this.update.firstChild && this.update.down().childNodes) {
mp-wp_genesis 274 this.entryCount =
mp-wp_genesis 275 this.update.down().childNodes.length;
mp-wp_genesis 276 for (var i = 0; i < this.entryCount; i++) {
mp-wp_genesis 277 var entry = this.getEntry(i);
mp-wp_genesis 278 entry.autocompleteIndex = i;
mp-wp_genesis 279 this.addObservers(entry);
mp-wp_genesis 280 }
mp-wp_genesis 281 } else {
mp-wp_genesis 282 this.entryCount = 0;
mp-wp_genesis 283 }
mp-wp_genesis 284
mp-wp_genesis 285 this.stopIndicator();
mp-wp_genesis 286 this.index = 0;
mp-wp_genesis 287
mp-wp_genesis 288 if(this.entryCount==1 && this.options.autoSelect) {
mp-wp_genesis 289 this.selectEntry();
mp-wp_genesis 290 this.hide();
mp-wp_genesis 291 } else {
mp-wp_genesis 292 this.render();
mp-wp_genesis 293 }
mp-wp_genesis 294 }
mp-wp_genesis 295 },
mp-wp_genesis 296
mp-wp_genesis 297 addObservers: function(element) {
mp-wp_genesis 298 Event.observe(element, "mouseover", this.onHover.bindAsEventListener(this));
mp-wp_genesis 299 Event.observe(element, "click", this.onClick.bindAsEventListener(this));
mp-wp_genesis 300 },
mp-wp_genesis 301
mp-wp_genesis 302 onObserverEvent: function() {
mp-wp_genesis 303 this.changed = false;
mp-wp_genesis 304 this.tokenBounds = null;
mp-wp_genesis 305 if(this.getToken().length>=this.options.minChars) {
mp-wp_genesis 306 this.getUpdatedChoices();
mp-wp_genesis 307 } else {
mp-wp_genesis 308 this.active = false;
mp-wp_genesis 309 this.hide();
mp-wp_genesis 310 }
mp-wp_genesis 311 this.oldElementValue = this.element.value;
mp-wp_genesis 312 },
mp-wp_genesis 313
mp-wp_genesis 314 getToken: function() {
mp-wp_genesis 315 var bounds = this.getTokenBounds();
mp-wp_genesis 316 return this.element.value.substring(bounds[0], bounds[1]).strip();
mp-wp_genesis 317 },
mp-wp_genesis 318
mp-wp_genesis 319 getTokenBounds: function() {
mp-wp_genesis 320 if (null != this.tokenBounds) return this.tokenBounds;
mp-wp_genesis 321 var value = this.element.value;
mp-wp_genesis 322 if (value.strip().empty()) return [-1, 0];
mp-wp_genesis 323 var diff = arguments.callee.getFirstDifferencePos(value, this.oldElementValue);
mp-wp_genesis 324 var offset = (diff == this.oldElementValue.length ? 1 : 0);
mp-wp_genesis 325 var prevTokenPos = -1, nextTokenPos = value.length;
mp-wp_genesis 326 var tp;
mp-wp_genesis 327 for (var index = 0, l = this.options.tokens.length; index < l; ++index) {
mp-wp_genesis 328 tp = value.lastIndexOf(this.options.tokens[index], diff + offset - 1);
mp-wp_genesis 329 if (tp > prevTokenPos) prevTokenPos = tp;
mp-wp_genesis 330 tp = value.indexOf(this.options.tokens[index], diff + offset);
mp-wp_genesis 331 if (-1 != tp && tp < nextTokenPos) nextTokenPos = tp;
mp-wp_genesis 332 }
mp-wp_genesis 333 return (this.tokenBounds = [prevTokenPos + 1, nextTokenPos]);
mp-wp_genesis 334 }
mp-wp_genesis 335 });
mp-wp_genesis 336
mp-wp_genesis 337 Autocompleter.Base.prototype.getTokenBounds.getFirstDifferencePos = function(newS, oldS) {
mp-wp_genesis 338 var boundary = Math.min(newS.length, oldS.length);
mp-wp_genesis 339 for (var index = 0; index < boundary; ++index)
mp-wp_genesis 340 if (newS[index] != oldS[index])
mp-wp_genesis 341 return index;
mp-wp_genesis 342 return boundary;
mp-wp_genesis 343 };
mp-wp_genesis 344
mp-wp_genesis 345 Ajax.Autocompleter = Class.create(Autocompleter.Base, {
mp-wp_genesis 346 initialize: function(element, update, url, options) {
mp-wp_genesis 347 this.baseInitialize(element, update, options);
mp-wp_genesis 348 this.options.asynchronous = true;
mp-wp_genesis 349 this.options.onComplete = this.onComplete.bind(this);
mp-wp_genesis 350 this.options.defaultParams = this.options.parameters || null;
mp-wp_genesis 351 this.url = url;
mp-wp_genesis 352 },
mp-wp_genesis 353
mp-wp_genesis 354 getUpdatedChoices: function() {
mp-wp_genesis 355 this.startIndicator();
mp-wp_genesis 356
mp-wp_genesis 357 var entry = encodeURIComponent(this.options.paramName) + '=' +
mp-wp_genesis 358 encodeURIComponent(this.getToken());
mp-wp_genesis 359
mp-wp_genesis 360 this.options.parameters = this.options.callback ?
mp-wp_genesis 361 this.options.callback(this.element, entry) : entry;
mp-wp_genesis 362
mp-wp_genesis 363 if(this.options.defaultParams)
mp-wp_genesis 364 this.options.parameters += '&' + this.options.defaultParams;
mp-wp_genesis 365
mp-wp_genesis 366 new Ajax.Request(this.url, this.options);
mp-wp_genesis 367 },
mp-wp_genesis 368
mp-wp_genesis 369 onComplete: function(request) {
mp-wp_genesis 370 this.updateChoices(request.responseText);
mp-wp_genesis 371 }
mp-wp_genesis 372 });
mp-wp_genesis 373
mp-wp_genesis 374 // The local array autocompleter. Used when you'd prefer to
mp-wp_genesis 375 // inject an array of autocompletion options into the page, rather
mp-wp_genesis 376 // than sending out Ajax queries, which can be quite slow sometimes.
mp-wp_genesis 377 //
mp-wp_genesis 378 // The constructor takes four parameters. The first two are, as usual,
mp-wp_genesis 379 // the id of the monitored textbox, and id of the autocompletion menu.
mp-wp_genesis 380 // The third is the array you want to autocomplete from, and the fourth
mp-wp_genesis 381 // is the options block.
mp-wp_genesis 382 //
mp-wp_genesis 383 // Extra local autocompletion options:
mp-wp_genesis 384 // - choices - How many autocompletion choices to offer
mp-wp_genesis 385 //
mp-wp_genesis 386 // - partialSearch - If false, the autocompleter will match entered
mp-wp_genesis 387 // text only at the beginning of strings in the
mp-wp_genesis 388 // autocomplete array. Defaults to true, which will
mp-wp_genesis 389 // match text at the beginning of any *word* in the
mp-wp_genesis 390 // strings in the autocomplete array. If you want to
mp-wp_genesis 391 // search anywhere in the string, additionally set
mp-wp_genesis 392 // the option fullSearch to true (default: off).
mp-wp_genesis 393 //
mp-wp_genesis 394 // - fullSsearch - Search anywhere in autocomplete array strings.
mp-wp_genesis 395 //
mp-wp_genesis 396 // - partialChars - How many characters to enter before triggering
mp-wp_genesis 397 // a partial match (unlike minChars, which defines
mp-wp_genesis 398 // how many characters are required to do any match
mp-wp_genesis 399 // at all). Defaults to 2.
mp-wp_genesis 400 //
mp-wp_genesis 401 // - ignoreCase - Whether to ignore case when autocompleting.
mp-wp_genesis 402 // Defaults to true.
mp-wp_genesis 403 //
mp-wp_genesis 404 // It's possible to pass in a custom function as the 'selector'
mp-wp_genesis 405 // option, if you prefer to write your own autocompletion logic.
mp-wp_genesis 406 // In that case, the other options above will not apply unless
mp-wp_genesis 407 // you support them.
mp-wp_genesis 408
mp-wp_genesis 409 Autocompleter.Local = Class.create(Autocompleter.Base, {
mp-wp_genesis 410 initialize: function(element, update, array, options) {
mp-wp_genesis 411 this.baseInitialize(element, update, options);
mp-wp_genesis 412 this.options.array = array;
mp-wp_genesis 413 },
mp-wp_genesis 414
mp-wp_genesis 415 getUpdatedChoices: function() {
mp-wp_genesis 416 this.updateChoices(this.options.selector(this));
mp-wp_genesis 417 },
mp-wp_genesis 418
mp-wp_genesis 419 setOptions: function(options) {
mp-wp_genesis 420 this.options = Object.extend({
mp-wp_genesis 421 choices: 10,
mp-wp_genesis 422 partialSearch: true,
mp-wp_genesis 423 partialChars: 2,
mp-wp_genesis 424 ignoreCase: true,
mp-wp_genesis 425 fullSearch: false,
mp-wp_genesis 426 selector: function(instance) {
mp-wp_genesis 427 var ret = []; // Beginning matches
mp-wp_genesis 428 var partial = []; // Inside matches
mp-wp_genesis 429 var entry = instance.getToken();
mp-wp_genesis 430 var count = 0;
mp-wp_genesis 431
mp-wp_genesis 432 for (var i = 0; i < instance.options.array.length &&
mp-wp_genesis 433 ret.length < instance.options.choices ; i++) {
mp-wp_genesis 434
mp-wp_genesis 435 var elem = instance.options.array[i];
mp-wp_genesis 436 var foundPos = instance.options.ignoreCase ?
mp-wp_genesis 437 elem.toLowerCase().indexOf(entry.toLowerCase()) :
mp-wp_genesis 438 elem.indexOf(entry);
mp-wp_genesis 439
mp-wp_genesis 440 while (foundPos != -1) {
mp-wp_genesis 441 if (foundPos == 0 && elem.length != entry.length) {
mp-wp_genesis 442 ret.push("<li><strong>" + elem.substr(0, entry.length) + "</strong>" +
mp-wp_genesis 443 elem.substr(entry.length) + "</li>");
mp-wp_genesis 444 break;
mp-wp_genesis 445 } else if (entry.length >= instance.options.partialChars &&
mp-wp_genesis 446 instance.options.partialSearch && foundPos != -1) {
mp-wp_genesis 447 if (instance.options.fullSearch || /\s/.test(elem.substr(foundPos-1,1))) {
mp-wp_genesis 448 partial.push("<li>" + elem.substr(0, foundPos) + "<strong>" +
mp-wp_genesis 449 elem.substr(foundPos, entry.length) + "</strong>" + elem.substr(
mp-wp_genesis 450 foundPos + entry.length) + "</li>");
mp-wp_genesis 451 break;
mp-wp_genesis 452 }
mp-wp_genesis 453 }
mp-wp_genesis 454
mp-wp_genesis 455 foundPos = instance.options.ignoreCase ?
mp-wp_genesis 456 elem.toLowerCase().indexOf(entry.toLowerCase(), foundPos + 1) :
mp-wp_genesis 457 elem.indexOf(entry, foundPos + 1);
mp-wp_genesis 458
mp-wp_genesis 459 }
mp-wp_genesis 460 }
mp-wp_genesis 461 if (partial.length)
mp-wp_genesis 462 ret = ret.concat(partial.slice(0, instance.options.choices - ret.length))
mp-wp_genesis 463 return "<ul>" + ret.join('') + "</ul>";
mp-wp_genesis 464 }
mp-wp_genesis 465 }, options || { });
mp-wp_genesis 466 }
mp-wp_genesis 467 });
mp-wp_genesis 468
mp-wp_genesis 469 // AJAX in-place editor and collection editor
mp-wp_genesis 470 // Full rewrite by Christophe Porteneuve <tdd@tddsworld.com> (April 2007).
mp-wp_genesis 471
mp-wp_genesis 472 // Use this if you notice weird scrolling problems on some browsers,
mp-wp_genesis 473 // the DOM might be a bit confused when this gets called so do this
mp-wp_genesis 474 // waits 1 ms (with setTimeout) until it does the activation
mp-wp_genesis 475 Field.scrollFreeActivate = function(field) {
mp-wp_genesis 476 setTimeout(function() {
mp-wp_genesis 477 Field.activate(field);
mp-wp_genesis 478 }, 1);
mp-wp_genesis 479 }
mp-wp_genesis 480
mp-wp_genesis 481 Ajax.InPlaceEditor = Class.create({
mp-wp_genesis 482 initialize: function(element, url, options) {
mp-wp_genesis 483 this.url = url;
mp-wp_genesis 484 this.element = element = $(element);
mp-wp_genesis 485 this.prepareOptions();
mp-wp_genesis 486 this._controls = { };
mp-wp_genesis 487 arguments.callee.dealWithDeprecatedOptions(options); // DEPRECATION LAYER!!!
mp-wp_genesis 488 Object.extend(this.options, options || { });
mp-wp_genesis 489 if (!this.options.formId && this.element.id) {
mp-wp_genesis 490 this.options.formId = this.element.id + '-inplaceeditor';
mp-wp_genesis 491 if ($(this.options.formId))
mp-wp_genesis 492 this.options.formId = '';
mp-wp_genesis 493 }
mp-wp_genesis 494 if (this.options.externalControl)
mp-wp_genesis 495 this.options.externalControl = $(this.options.externalControl);
mp-wp_genesis 496 if (!this.options.externalControl)
mp-wp_genesis 497 this.options.externalControlOnly = false;
mp-wp_genesis 498 this._originalBackground = this.element.getStyle('background-color') || 'transparent';
mp-wp_genesis 499 this.element.title = this.options.clickToEditText;
mp-wp_genesis 500 this._boundCancelHandler = this.handleFormCancellation.bind(this);
mp-wp_genesis 501 this._boundComplete = (this.options.onComplete || Prototype.emptyFunction).bind(this);
mp-wp_genesis 502 this._boundFailureHandler = this.handleAJAXFailure.bind(this);
mp-wp_genesis 503 this._boundSubmitHandler = this.handleFormSubmission.bind(this);
mp-wp_genesis 504 this._boundWrapperHandler = this.wrapUp.bind(this);
mp-wp_genesis 505 this.registerListeners();
mp-wp_genesis 506 },
mp-wp_genesis 507 checkForEscapeOrReturn: function(e) {
mp-wp_genesis 508 if (!this._editing || e.ctrlKey || e.altKey || e.shiftKey) return;
mp-wp_genesis 509 if (Event.KEY_ESC == e.keyCode)
mp-wp_genesis 510 this.handleFormCancellation(e);
mp-wp_genesis 511 else if (Event.KEY_RETURN == e.keyCode)
mp-wp_genesis 512 this.handleFormSubmission(e);
mp-wp_genesis 513 },
mp-wp_genesis 514 createControl: function(mode, handler, extraClasses) {
mp-wp_genesis 515 var control = this.options[mode + 'Control'];
mp-wp_genesis 516 var text = this.options[mode + 'Text'];
mp-wp_genesis 517 if ('button' == control) {
mp-wp_genesis 518 var btn = document.createElement('input');
mp-wp_genesis 519 btn.type = 'submit';
mp-wp_genesis 520 btn.value = text;
mp-wp_genesis 521 btn.className = 'editor_' + mode + '_button';
mp-wp_genesis 522 if ('cancel' == mode)
mp-wp_genesis 523 btn.onclick = this._boundCancelHandler;
mp-wp_genesis 524 this._form.appendChild(btn);
mp-wp_genesis 525 this._controls[mode] = btn;
mp-wp_genesis 526 } else if ('link' == control) {
mp-wp_genesis 527 var link = document.createElement('a');
mp-wp_genesis 528 link.href = '#';
mp-wp_genesis 529 link.appendChild(document.createTextNode(text));
mp-wp_genesis 530 link.onclick = 'cancel' == mode ? this._boundCancelHandler : this._boundSubmitHandler;
mp-wp_genesis 531 link.className = 'editor_' + mode + '_link';
mp-wp_genesis 532 if (extraClasses)
mp-wp_genesis 533 link.className += ' ' + extraClasses;
mp-wp_genesis 534 this._form.appendChild(link);
mp-wp_genesis 535 this._controls[mode] = link;
mp-wp_genesis 536 }
mp-wp_genesis 537 },
mp-wp_genesis 538 createEditField: function() {
mp-wp_genesis 539 var text = (this.options.loadTextURL ? this.options.loadingText : this.getText());
mp-wp_genesis 540 var fld;
mp-wp_genesis 541 if (1 >= this.options.rows && !/\r|\n/.test(this.getText())) {
mp-wp_genesis 542 fld = document.createElement('input');
mp-wp_genesis 543 fld.type = 'text';
mp-wp_genesis 544 var size = this.options.size || this.options.cols || 0;
mp-wp_genesis 545 if (0 < size) fld.size = size;
mp-wp_genesis 546 } else {
mp-wp_genesis 547 fld = document.createElement('textarea');
mp-wp_genesis 548 fld.rows = (1 >= this.options.rows ? this.options.autoRows : this.options.rows);
mp-wp_genesis 549 fld.cols = this.options.cols || 40;
mp-wp_genesis 550 }
mp-wp_genesis 551 fld.name = this.options.paramName;
mp-wp_genesis 552 fld.value = text; // No HTML breaks conversion anymore
mp-wp_genesis 553 fld.className = 'editor_field';
mp-wp_genesis 554 if (this.options.submitOnBlur)
mp-wp_genesis 555 fld.onblur = this._boundSubmitHandler;
mp-wp_genesis 556 this._controls.editor = fld;
mp-wp_genesis 557 if (this.options.loadTextURL)
mp-wp_genesis 558 this.loadExternalText();
mp-wp_genesis 559 this._form.appendChild(this._controls.editor);
mp-wp_genesis 560 },
mp-wp_genesis 561 createForm: function() {
mp-wp_genesis 562 var ipe = this;
mp-wp_genesis 563 function addText(mode, condition) {
mp-wp_genesis 564 var text = ipe.options['text' + mode + 'Controls'];
mp-wp_genesis 565 if (!text || condition === false) return;
mp-wp_genesis 566 ipe._form.appendChild(document.createTextNode(text));
mp-wp_genesis 567 };
mp-wp_genesis 568 this._form = $(document.createElement('form'));
mp-wp_genesis 569 this._form.id = this.options.formId;
mp-wp_genesis 570 this._form.addClassName(this.options.formClassName);
mp-wp_genesis 571 this._form.onsubmit = this._boundSubmitHandler;
mp-wp_genesis 572 this.createEditField();
mp-wp_genesis 573 if ('textarea' == this._controls.editor.tagName.toLowerCase())
mp-wp_genesis 574 this._form.appendChild(document.createElement('br'));
mp-wp_genesis 575 if (this.options.onFormCustomization)
mp-wp_genesis 576 this.options.onFormCustomization(this, this._form);
mp-wp_genesis 577 addText('Before', this.options.okControl || this.options.cancelControl);
mp-wp_genesis 578 this.createControl('ok', this._boundSubmitHandler);
mp-wp_genesis 579 addText('Between', this.options.okControl && this.options.cancelControl);
mp-wp_genesis 580 this.createControl('cancel', this._boundCancelHandler, 'editor_cancel');
mp-wp_genesis 581 addText('After', this.options.okControl || this.options.cancelControl);
mp-wp_genesis 582 },
mp-wp_genesis 583 destroy: function() {
mp-wp_genesis 584 if (this._oldInnerHTML)
mp-wp_genesis 585 this.element.innerHTML = this._oldInnerHTML;
mp-wp_genesis 586 this.leaveEditMode();
mp-wp_genesis 587 this.unregisterListeners();
mp-wp_genesis 588 },
mp-wp_genesis 589 enterEditMode: function(e) {
mp-wp_genesis 590 if (this._saving || this._editing) return;
mp-wp_genesis 591 this._editing = true;
mp-wp_genesis 592 this.triggerCallback('onEnterEditMode');
mp-wp_genesis 593 if (this.options.externalControl)
mp-wp_genesis 594 this.options.externalControl.hide();
mp-wp_genesis 595 this.element.hide();
mp-wp_genesis 596 this.createForm();
mp-wp_genesis 597 this.element.parentNode.insertBefore(this._form, this.element);
mp-wp_genesis 598 if (!this.options.loadTextURL)
mp-wp_genesis 599 this.postProcessEditField();
mp-wp_genesis 600 if (e) Event.stop(e);
mp-wp_genesis 601 },
mp-wp_genesis 602 enterHover: function(e) {
mp-wp_genesis 603 if (this.options.hoverClassName)
mp-wp_genesis 604 this.element.addClassName(this.options.hoverClassName);
mp-wp_genesis 605 if (this._saving) return;
mp-wp_genesis 606 this.triggerCallback('onEnterHover');
mp-wp_genesis 607 },
mp-wp_genesis 608 getText: function() {
mp-wp_genesis 609 return this.element.innerHTML;
mp-wp_genesis 610 },
mp-wp_genesis 611 handleAJAXFailure: function(transport) {
mp-wp_genesis 612 this.triggerCallback('onFailure', transport);
mp-wp_genesis 613 if (this._oldInnerHTML) {
mp-wp_genesis 614 this.element.innerHTML = this._oldInnerHTML;
mp-wp_genesis 615 this._oldInnerHTML = null;
mp-wp_genesis 616 }
mp-wp_genesis 617 },
mp-wp_genesis 618 handleFormCancellation: function(e) {
mp-wp_genesis 619 this.wrapUp();
mp-wp_genesis 620 if (e) Event.stop(e);
mp-wp_genesis 621 },
mp-wp_genesis 622 handleFormSubmission: function(e) {
mp-wp_genesis 623 var form = this._form;
mp-wp_genesis 624 var value = $F(this._controls.editor);
mp-wp_genesis 625 this.prepareSubmission();
mp-wp_genesis 626 var params = this.options.callback(form, value) || '';
mp-wp_genesis 627 if (Object.isString(params))
mp-wp_genesis 628 params = params.toQueryParams();
mp-wp_genesis 629 params.editorId = this.element.id;
mp-wp_genesis 630 if (this.options.htmlResponse) {
mp-wp_genesis 631 var options = Object.extend({ evalScripts: true }, this.options.ajaxOptions);
mp-wp_genesis 632 Object.extend(options, {
mp-wp_genesis 633 parameters: params,
mp-wp_genesis 634 onComplete: this._boundWrapperHandler,
mp-wp_genesis 635 onFailure: this._boundFailureHandler
mp-wp_genesis 636 });
mp-wp_genesis 637 new Ajax.Updater({ success: this.element }, this.url, options);
mp-wp_genesis 638 } else {
mp-wp_genesis 639 var options = Object.extend({ method: 'get' }, this.options.ajaxOptions);
mp-wp_genesis 640 Object.extend(options, {
mp-wp_genesis 641 parameters: params,
mp-wp_genesis 642 onComplete: this._boundWrapperHandler,
mp-wp_genesis 643 onFailure: this._boundFailureHandler
mp-wp_genesis 644 });
mp-wp_genesis 645 new Ajax.Request(this.url, options);
mp-wp_genesis 646 }
mp-wp_genesis 647 if (e) Event.stop(e);
mp-wp_genesis 648 },
mp-wp_genesis 649 leaveEditMode: function() {
mp-wp_genesis 650 this.element.removeClassName(this.options.savingClassName);
mp-wp_genesis 651 this.removeForm();
mp-wp_genesis 652 this.leaveHover();
mp-wp_genesis 653 this.element.style.backgroundColor = this._originalBackground;
mp-wp_genesis 654 this.element.show();
mp-wp_genesis 655 if (this.options.externalControl)
mp-wp_genesis 656 this.options.externalControl.show();
mp-wp_genesis 657 this._saving = false;
mp-wp_genesis 658 this._editing = false;
mp-wp_genesis 659 this._oldInnerHTML = null;
mp-wp_genesis 660 this.triggerCallback('onLeaveEditMode');
mp-wp_genesis 661 },
mp-wp_genesis 662 leaveHover: function(e) {
mp-wp_genesis 663 if (this.options.hoverClassName)
mp-wp_genesis 664 this.element.removeClassName(this.options.hoverClassName);
mp-wp_genesis 665 if (this._saving) return;
mp-wp_genesis 666 this.triggerCallback('onLeaveHover');
mp-wp_genesis 667 },
mp-wp_genesis 668 loadExternalText: function() {
mp-wp_genesis 669 this._form.addClassName(this.options.loadingClassName);
mp-wp_genesis 670 this._controls.editor.disabled = true;
mp-wp_genesis 671 var options = Object.extend({ method: 'get' }, this.options.ajaxOptions);
mp-wp_genesis 672 Object.extend(options, {
mp-wp_genesis 673 parameters: 'editorId=' + encodeURIComponent(this.element.id),
mp-wp_genesis 674 onComplete: Prototype.emptyFunction,
mp-wp_genesis 675 onSuccess: function(transport) {
mp-wp_genesis 676 this._form.removeClassName(this.options.loadingClassName);
mp-wp_genesis 677 var text = transport.responseText;
mp-wp_genesis 678 if (this.options.stripLoadedTextTags)
mp-wp_genesis 679 text = text.stripTags();
mp-wp_genesis 680 this._controls.editor.value = text;
mp-wp_genesis 681 this._controls.editor.disabled = false;
mp-wp_genesis 682 this.postProcessEditField();
mp-wp_genesis 683 }.bind(this),
mp-wp_genesis 684 onFailure: this._boundFailureHandler
mp-wp_genesis 685 });
mp-wp_genesis 686 new Ajax.Request(this.options.loadTextURL, options);
mp-wp_genesis 687 },
mp-wp_genesis 688 postProcessEditField: function() {
mp-wp_genesis 689 var fpc = this.options.fieldPostCreation;
mp-wp_genesis 690 if (fpc)
mp-wp_genesis 691 $(this._controls.editor)['focus' == fpc ? 'focus' : 'activate']();
mp-wp_genesis 692 },
mp-wp_genesis 693 prepareOptions: function() {
mp-wp_genesis 694 this.options = Object.clone(Ajax.InPlaceEditor.DefaultOptions);
mp-wp_genesis 695 Object.extend(this.options, Ajax.InPlaceEditor.DefaultCallbacks);
mp-wp_genesis 696 [this._extraDefaultOptions].flatten().compact().each(function(defs) {
mp-wp_genesis 697 Object.extend(this.options, defs);
mp-wp_genesis 698 }.bind(this));
mp-wp_genesis 699 },
mp-wp_genesis 700 prepareSubmission: function() {
mp-wp_genesis 701 this._saving = true;
mp-wp_genesis 702 this.removeForm();
mp-wp_genesis 703 this.leaveHover();
mp-wp_genesis 704 this.showSaving();
mp-wp_genesis 705 },
mp-wp_genesis 706 registerListeners: function() {
mp-wp_genesis 707 this._listeners = { };
mp-wp_genesis 708 var listener;
mp-wp_genesis 709 $H(Ajax.InPlaceEditor.Listeners).each(function(pair) {
mp-wp_genesis 710 listener = this[pair.value].bind(this);
mp-wp_genesis 711 this._listeners[pair.key] = listener;
mp-wp_genesis 712 if (!this.options.externalControlOnly)
mp-wp_genesis 713 this.element.observe(pair.key, listener);
mp-wp_genesis 714 if (this.options.externalControl)
mp-wp_genesis 715 this.options.externalControl.observe(pair.key, listener);
mp-wp_genesis 716 }.bind(this));
mp-wp_genesis 717 },
mp-wp_genesis 718 removeForm: function() {
mp-wp_genesis 719 if (!this._form) return;
mp-wp_genesis 720 this._form.remove();
mp-wp_genesis 721 this._form = null;
mp-wp_genesis 722 this._controls = { };
mp-wp_genesis 723 },
mp-wp_genesis 724 showSaving: function() {
mp-wp_genesis 725 this._oldInnerHTML = this.element.innerHTML;
mp-wp_genesis 726 this.element.innerHTML = this.options.savingText;
mp-wp_genesis 727 this.element.addClassName(this.options.savingClassName);
mp-wp_genesis 728 this.element.style.backgroundColor = this._originalBackground;
mp-wp_genesis 729 this.element.show();
mp-wp_genesis 730 },
mp-wp_genesis 731 triggerCallback: function(cbName, arg) {
mp-wp_genesis 732 if ('function' == typeof this.options[cbName]) {
mp-wp_genesis 733 this.options[cbName](this, arg);
mp-wp_genesis 734 }
mp-wp_genesis 735 },
mp-wp_genesis 736 unregisterListeners: function() {
mp-wp_genesis 737 $H(this._listeners).each(function(pair) {
mp-wp_genesis 738 if (!this.options.externalControlOnly)
mp-wp_genesis 739 this.element.stopObserving(pair.key, pair.value);
mp-wp_genesis 740 if (this.options.externalControl)
mp-wp_genesis 741 this.options.externalControl.stopObserving(pair.key, pair.value);
mp-wp_genesis 742 }.bind(this));
mp-wp_genesis 743 },
mp-wp_genesis 744 wrapUp: function(transport) {
mp-wp_genesis 745 this.leaveEditMode();
mp-wp_genesis 746 // Can't use triggerCallback due to backward compatibility: requires
mp-wp_genesis 747 // binding + direct element
mp-wp_genesis 748 this._boundComplete(transport, this.element);
mp-wp_genesis 749 }
mp-wp_genesis 750 });
mp-wp_genesis 751
mp-wp_genesis 752 Object.extend(Ajax.InPlaceEditor.prototype, {
mp-wp_genesis 753 dispose: Ajax.InPlaceEditor.prototype.destroy
mp-wp_genesis 754 });
mp-wp_genesis 755
mp-wp_genesis 756 Ajax.InPlaceCollectionEditor = Class.create(Ajax.InPlaceEditor, {
mp-wp_genesis 757 initialize: function($super, element, url, options) {
mp-wp_genesis 758 this._extraDefaultOptions = Ajax.InPlaceCollectionEditor.DefaultOptions;
mp-wp_genesis 759 $super(element, url, options);
mp-wp_genesis 760 },
mp-wp_genesis 761
mp-wp_genesis 762 createEditField: function() {
mp-wp_genesis 763 var list = document.createElement('select');
mp-wp_genesis 764 list.name = this.options.paramName;
mp-wp_genesis 765 list.size = 1;
mp-wp_genesis 766 this._controls.editor = list;
mp-wp_genesis 767 this._collection = this.options.collection || [];
mp-wp_genesis 768 if (this.options.loadCollectionURL)
mp-wp_genesis 769 this.loadCollection();
mp-wp_genesis 770 else
mp-wp_genesis 771 this.checkForExternalText();
mp-wp_genesis 772 this._form.appendChild(this._controls.editor);
mp-wp_genesis 773 },
mp-wp_genesis 774
mp-wp_genesis 775 loadCollection: function() {
mp-wp_genesis 776 this._form.addClassName(this.options.loadingClassName);
mp-wp_genesis 777 this.showLoadingText(this.options.loadingCollectionText);
mp-wp_genesis 778 var options = Object.extend({ method: 'get' }, this.options.ajaxOptions);
mp-wp_genesis 779 Object.extend(options, {
mp-wp_genesis 780 parameters: 'editorId=' + encodeURIComponent(this.element.id),
mp-wp_genesis 781 onComplete: Prototype.emptyFunction,
mp-wp_genesis 782 onSuccess: function(transport) {
mp-wp_genesis 783 var js = transport.responseText.strip();
mp-wp_genesis 784 if (!/^\[.*\]$/.test(js)) // TODO: improve sanity check
mp-wp_genesis 785 throw 'Server returned an invalid collection representation.';
mp-wp_genesis 786 this._collection = eval(js);
mp-wp_genesis 787 this.checkForExternalText();
mp-wp_genesis 788 }.bind(this),
mp-wp_genesis 789 onFailure: this.onFailure
mp-wp_genesis 790 });
mp-wp_genesis 791 new Ajax.Request(this.options.loadCollectionURL, options);
mp-wp_genesis 792 },
mp-wp_genesis 793
mp-wp_genesis 794 showLoadingText: function(text) {
mp-wp_genesis 795 this._controls.editor.disabled = true;
mp-wp_genesis 796 var tempOption = this._controls.editor.firstChild;
mp-wp_genesis 797 if (!tempOption) {
mp-wp_genesis 798 tempOption = document.createElement('option');
mp-wp_genesis 799 tempOption.value = '';
mp-wp_genesis 800 this._controls.editor.appendChild(tempOption);
mp-wp_genesis 801 tempOption.selected = true;
mp-wp_genesis 802 }
mp-wp_genesis 803 tempOption.update((text || '').stripScripts().stripTags());
mp-wp_genesis 804 },
mp-wp_genesis 805
mp-wp_genesis 806 checkForExternalText: function() {
mp-wp_genesis 807 this._text = this.getText();
mp-wp_genesis 808 if (this.options.loadTextURL)
mp-wp_genesis 809 this.loadExternalText();
mp-wp_genesis 810 else
mp-wp_genesis 811 this.buildOptionList();
mp-wp_genesis 812 },
mp-wp_genesis 813
mp-wp_genesis 814 loadExternalText: function() {
mp-wp_genesis 815 this.showLoadingText(this.options.loadingText);
mp-wp_genesis 816 var options = Object.extend({ method: 'get' }, this.options.ajaxOptions);
mp-wp_genesis 817 Object.extend(options, {
mp-wp_genesis 818 parameters: 'editorId=' + encodeURIComponent(this.element.id),
mp-wp_genesis 819 onComplete: Prototype.emptyFunction,
mp-wp_genesis 820 onSuccess: function(transport) {
mp-wp_genesis 821 this._text = transport.responseText.strip();
mp-wp_genesis 822 this.buildOptionList();
mp-wp_genesis 823 }.bind(this),
mp-wp_genesis 824 onFailure: this.onFailure
mp-wp_genesis 825 });
mp-wp_genesis 826 new Ajax.Request(this.options.loadTextURL, options);
mp-wp_genesis 827 },
mp-wp_genesis 828
mp-wp_genesis 829 buildOptionList: function() {
mp-wp_genesis 830 this._form.removeClassName(this.options.loadingClassName);
mp-wp_genesis 831 this._collection = this._collection.map(function(entry) {
mp-wp_genesis 832 return 2 === entry.length ? entry : [entry, entry].flatten();
mp-wp_genesis 833 });
mp-wp_genesis 834 var marker = ('value' in this.options) ? this.options.value : this._text;
mp-wp_genesis 835 var textFound = this._collection.any(function(entry) {
mp-wp_genesis 836 return entry[0] == marker;
mp-wp_genesis 837 }.bind(this));
mp-wp_genesis 838 this._controls.editor.update('');
mp-wp_genesis 839 var option;
mp-wp_genesis 840 this._collection.each(function(entry, index) {
mp-wp_genesis 841 option = document.createElement('option');
mp-wp_genesis 842 option.value = entry[0];
mp-wp_genesis 843 option.selected = textFound ? entry[0] == marker : 0 == index;
mp-wp_genesis 844 option.appendChild(document.createTextNode(entry[1]));
mp-wp_genesis 845 this._controls.editor.appendChild(option);
mp-wp_genesis 846 }.bind(this));
mp-wp_genesis 847 this._controls.editor.disabled = false;
mp-wp_genesis 848 Field.scrollFreeActivate(this._controls.editor);
mp-wp_genesis 849 }
mp-wp_genesis 850 });
mp-wp_genesis 851
mp-wp_genesis 852 //**** DEPRECATION LAYER FOR InPlace[Collection]Editor! ****
mp-wp_genesis 853 //**** This only exists for a while, in order to let ****
mp-wp_genesis 854 //**** users adapt to the new API. Read up on the new ****
mp-wp_genesis 855 //**** API and convert your code to it ASAP! ****
mp-wp_genesis 856
mp-wp_genesis 857 Ajax.InPlaceEditor.prototype.initialize.dealWithDeprecatedOptions = function(options) {
mp-wp_genesis 858 if (!options) return;
mp-wp_genesis 859 function fallback(name, expr) {
mp-wp_genesis 860 if (name in options || expr === undefined) return;
mp-wp_genesis 861 options[name] = expr;
mp-wp_genesis 862 };
mp-wp_genesis 863 fallback('cancelControl', (options.cancelLink ? 'link' : (options.cancelButton ? 'button' :
mp-wp_genesis 864 options.cancelLink == options.cancelButton == false ? false : undefined)));
mp-wp_genesis 865 fallback('okControl', (options.okLink ? 'link' : (options.okButton ? 'button' :
mp-wp_genesis 866 options.okLink == options.okButton == false ? false : undefined)));
mp-wp_genesis 867 fallback('highlightColor', options.highlightcolor);
mp-wp_genesis 868 fallback('highlightEndColor', options.highlightendcolor);
mp-wp_genesis 869 };
mp-wp_genesis 870
mp-wp_genesis 871 Object.extend(Ajax.InPlaceEditor, {
mp-wp_genesis 872 DefaultOptions: {
mp-wp_genesis 873 ajaxOptions: { },
mp-wp_genesis 874 autoRows: 3, // Use when multi-line w/ rows == 1
mp-wp_genesis 875 cancelControl: 'link', // 'link'|'button'|false
mp-wp_genesis 876 cancelText: 'cancel',
mp-wp_genesis 877 clickToEditText: 'Click to edit',
mp-wp_genesis 878 externalControl: null, // id|elt
mp-wp_genesis 879 externalControlOnly: false,
mp-wp_genesis 880 fieldPostCreation: 'activate', // 'activate'|'focus'|false
mp-wp_genesis 881 formClassName: 'inplaceeditor-form',
mp-wp_genesis 882 formId: null, // id|elt
mp-wp_genesis 883 highlightColor: '#ffff99',
mp-wp_genesis 884 highlightEndColor: '#ffffff',
mp-wp_genesis 885 hoverClassName: '',
mp-wp_genesis 886 htmlResponse: true,
mp-wp_genesis 887 loadingClassName: 'inplaceeditor-loading',
mp-wp_genesis 888 loadingText: 'Loading...',
mp-wp_genesis 889 okControl: 'button', // 'link'|'button'|false
mp-wp_genesis 890 okText: 'ok',
mp-wp_genesis 891 paramName: 'value',
mp-wp_genesis 892 rows: 1, // If 1 and multi-line, uses autoRows
mp-wp_genesis 893 savingClassName: 'inplaceeditor-saving',
mp-wp_genesis 894 savingText: 'Saving...',
mp-wp_genesis 895 size: 0,
mp-wp_genesis 896 stripLoadedTextTags: false,
mp-wp_genesis 897 submitOnBlur: false,
mp-wp_genesis 898 textAfterControls: '',
mp-wp_genesis 899 textBeforeControls: '',
mp-wp_genesis 900 textBetweenControls: ''
mp-wp_genesis 901 },
mp-wp_genesis 902 DefaultCallbacks: {
mp-wp_genesis 903 callback: function(form) {
mp-wp_genesis 904 return Form.serialize(form);
mp-wp_genesis 905 },
mp-wp_genesis 906 onComplete: function(transport, element) {
mp-wp_genesis 907 // For backward compatibility, this one is bound to the IPE, and passes
mp-wp_genesis 908 // the element directly. It was too often customized, so we don't break it.
mp-wp_genesis 909 new Effect.Highlight(element, {
mp-wp_genesis 910 startcolor: this.options.highlightColor, keepBackgroundImage: true });
mp-wp_genesis 911 },
mp-wp_genesis 912 onEnterEditMode: null,
mp-wp_genesis 913 onEnterHover: function(ipe) {
mp-wp_genesis 914 ipe.element.style.backgroundColor = ipe.options.highlightColor;
mp-wp_genesis 915 if (ipe._effect)
mp-wp_genesis 916 ipe._effect.cancel();
mp-wp_genesis 917 },
mp-wp_genesis 918 onFailure: function(transport, ipe) {
mp-wp_genesis 919 alert('Error communication with the server: ' + transport.responseText.stripTags());
mp-wp_genesis 920 },
mp-wp_genesis 921 onFormCustomization: null, // Takes the IPE and its generated form, after editor, before controls.
mp-wp_genesis 922 onLeaveEditMode: null,
mp-wp_genesis 923 onLeaveHover: function(ipe) {
mp-wp_genesis 924 ipe._effect = new Effect.Highlight(ipe.element, {
mp-wp_genesis 925 startcolor: ipe.options.highlightColor, endcolor: ipe.options.highlightEndColor,
mp-wp_genesis 926 restorecolor: ipe._originalBackground, keepBackgroundImage: true
mp-wp_genesis 927 });
mp-wp_genesis 928 }
mp-wp_genesis 929 },
mp-wp_genesis 930 Listeners: {
mp-wp_genesis 931 click: 'enterEditMode',
mp-wp_genesis 932 keydown: 'checkForEscapeOrReturn',
mp-wp_genesis 933 mouseover: 'enterHover',
mp-wp_genesis 934 mouseout: 'leaveHover'
mp-wp_genesis 935 }
mp-wp_genesis 936 });
mp-wp_genesis 937
mp-wp_genesis 938 Ajax.InPlaceCollectionEditor.DefaultOptions = {
mp-wp_genesis 939 loadingCollectionText: 'Loading options...'
mp-wp_genesis 940 };
mp-wp_genesis 941
mp-wp_genesis 942 // Delayed observer, like Form.Element.Observer,
mp-wp_genesis 943 // but waits for delay after last key input
mp-wp_genesis 944 // Ideal for live-search fields
mp-wp_genesis 945
mp-wp_genesis 946 Form.Element.DelayedObserver = Class.create({
mp-wp_genesis 947 initialize: function(element, delay, callback) {
mp-wp_genesis 948 this.delay = delay || 0.5;
mp-wp_genesis 949 this.element = $(element);
mp-wp_genesis 950 this.callback = callback;
mp-wp_genesis 951 this.timer = null;
mp-wp_genesis 952 this.lastValue = $F(this.element);
mp-wp_genesis 953 Event.observe(this.element,'keyup',this.delayedListener.bindAsEventListener(this));
mp-wp_genesis 954 },
mp-wp_genesis 955 delayedListener: function(event) {
mp-wp_genesis 956 if(this.lastValue == $F(this.element)) return;
mp-wp_genesis 957 if(this.timer) clearTimeout(this.timer);
mp-wp_genesis 958 this.timer = setTimeout(this.onTimerEvent.bind(this), this.delay * 1000);
mp-wp_genesis 959 this.lastValue = $F(this.element);
mp-wp_genesis 960 },
mp-wp_genesis 961 onTimerEvent: function() {
mp-wp_genesis 962 this.timer = null;
mp-wp_genesis 963 this.callback(this.element, $F(this.element));
mp-wp_genesis 964 }
mp-wp_genesis 965 });