dom/tests/mochitest/ajax/scriptaculous/src/controls.js

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

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

mercurial