annotate .svn/pristine/e0/e016f7bcdcab31cab9c67782c8874b33b52e7b82.svn-base @ 1519:afce8026aaeb redmine-2.4-integration

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