dom/tests/mochitest/ajax/mochikit/MochiKit/Controls.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/tests/mochitest/ajax/mochikit/MochiKit/Controls.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,1388 @@
     1.4 +/***
     1.5 +Copyright (c) 2005 Thomas Fuchs (http://script.aculo.us, http://mir.aculo.us)
     1.6 +          (c) 2005 Ivan Krstic (http://blogs.law.harvard.edu/ivan)
     1.7 +          (c) 2005 Jon Tirsen (http://www.tirsen.com)
     1.8 +Contributors:
     1.9 +    Richard Livsey
    1.10 +    Rahul Bhargava
    1.11 +    Rob Wills
    1.12 +    Mochi-ized By Thomas Herve (_firstname_@nimail.org)
    1.13 +
    1.14 +See scriptaculous.js for full license.
    1.15 +
    1.16 +Autocompleter.Base handles all the autocompletion functionality
    1.17 +that's independent of the data source for autocompletion. This
    1.18 +includes drawing the autocompletion menu, observing keyboard
    1.19 +and mouse events, and similar.
    1.20 +
    1.21 +Specific autocompleters need to provide, at the very least,
    1.22 +a getUpdatedChoices function that will be invoked every time
    1.23 +the text inside the monitored textbox changes. This method
    1.24 +should get the text for which to provide autocompletion by
    1.25 +invoking this.getToken(), NOT by directly accessing
    1.26 +this.element.value. This is to allow incremental tokenized
    1.27 +autocompletion. Specific auto-completion logic (AJAX, etc)
    1.28 +belongs in getUpdatedChoices.
    1.29 +
    1.30 +Tokenized incremental autocompletion is enabled automatically
    1.31 +when an autocompleter is instantiated with the 'tokens' option
    1.32 +in the options parameter, e.g.:
    1.33 +new Ajax.Autocompleter('id','upd', '/url/', { tokens: ',' });
    1.34 +will incrementally autocomplete with a comma as the token.
    1.35 +Additionally, ',' in the above example can be replaced with
    1.36 +a token array, e.g. { tokens: [',', '\n'] } which
    1.37 +enables autocompletion on multiple tokens. This is most
    1.38 +useful when one of the tokens is \n (a newline), as it
    1.39 +allows smart autocompletion after linebreaks.
    1.40 +
    1.41 +***/
    1.42 +
    1.43 +MochiKit.Base.update(MochiKit.Base, {
    1.44 +    ScriptFragment: '(?:<script.*?>)((\n|\r|.)*?)(?:<\/script>)',
    1.45 +
    1.46 +/** @id MochiKit.Base.stripScripts */
    1.47 +    stripScripts: function (str) {
    1.48 +        return str.replace(new RegExp(MochiKit.Base.ScriptFragment, 'img'), '');
    1.49 +    },
    1.50 +
    1.51 +/** @id MochiKit.Base.stripTags */
    1.52 +    stripTags: function(str) {
    1.53 +        return str.replace(/<\/?[^>]+>/gi, '');
    1.54 +    },
    1.55 +
    1.56 +/** @id MochiKit.Base.extractScripts */
    1.57 +    extractScripts: function (str) {
    1.58 +        var matchAll = new RegExp(MochiKit.Base.ScriptFragment, 'img');
    1.59 +        var matchOne = new RegExp(MochiKit.Base.ScriptFragment, 'im');
    1.60 +        return MochiKit.Base.map(function (scriptTag) {
    1.61 +            return (scriptTag.match(matchOne) || ['', ''])[1];
    1.62 +        }, str.match(matchAll) || []);
    1.63 +    },
    1.64 +
    1.65 +/** @id MochiKit.Base.evalScripts */
    1.66 +    evalScripts: function (str) {
    1.67 +        return MochiKit.Base.map(function (scr) {
    1.68 +            eval(scr);
    1.69 +        }, MochiKit.Base.extractScripts(str));
    1.70 +    }
    1.71 +});
    1.72 +
    1.73 +MochiKit.Form = {
    1.74 +
    1.75 +/** @id MochiKit.Form.serialize */
    1.76 +    serialize: function (form) {
    1.77 +        var elements = MochiKit.Form.getElements(form);
    1.78 +        var queryComponents = [];
    1.79 +
    1.80 +        for (var i = 0; i < elements.length; i++) {
    1.81 +            var queryComponent = MochiKit.Form.serializeElement(elements[i]);
    1.82 +            if (queryComponent) {
    1.83 +                queryComponents.push(queryComponent);
    1.84 +            }
    1.85 +        }
    1.86 +
    1.87 +        return queryComponents.join('&');
    1.88 +    },
    1.89 +
    1.90 +/** @id MochiKit.Form.getElements */
    1.91 +    getElements: function (form) {
    1.92 +        form = MochiKit.DOM.getElement(form);
    1.93 +        var elements = [];
    1.94 +
    1.95 +        for (var tagName in MochiKit.Form.Serializers) {
    1.96 +            var tagElements = form.getElementsByTagName(tagName);
    1.97 +            for (var j = 0; j < tagElements.length; j++) {
    1.98 +                elements.push(tagElements[j]);
    1.99 +            }
   1.100 +        }
   1.101 +        return elements;
   1.102 +    },
   1.103 +
   1.104 +/** @id MochiKit.Form.serializeElement */
   1.105 +    serializeElement: function (element) {
   1.106 +        element = MochiKit.DOM.getElement(element);
   1.107 +        var method = element.tagName.toLowerCase();
   1.108 +        var parameter = MochiKit.Form.Serializers[method](element);
   1.109 +
   1.110 +        if (parameter) {
   1.111 +            var key = encodeURIComponent(parameter[0]);
   1.112 +            if (key.length === 0) {
   1.113 +                return;
   1.114 +            }
   1.115 +
   1.116 +            if (!(parameter[1] instanceof Array)) {
   1.117 +                parameter[1] = [parameter[1]];
   1.118 +            }
   1.119 +
   1.120 +            return parameter[1].map(function (value) {
   1.121 +                return key + '=' + encodeURIComponent(value);
   1.122 +            }).join('&');
   1.123 +        }
   1.124 +    }
   1.125 +};
   1.126 +
   1.127 +MochiKit.Form.Serializers = {
   1.128 +
   1.129 +/** @id MochiKit.Form.Serializers.input */
   1.130 +    input: function (element) {
   1.131 +        switch (element.type.toLowerCase()) {
   1.132 +            case 'submit':
   1.133 +            case 'hidden':
   1.134 +            case 'password':
   1.135 +            case 'text':
   1.136 +                return MochiKit.Form.Serializers.textarea(element);
   1.137 +            case 'checkbox':
   1.138 +            case 'radio':
   1.139 +                return MochiKit.Form.Serializers.inputSelector(element);
   1.140 +        }
   1.141 +        return false;
   1.142 +    },
   1.143 +
   1.144 +/** @id MochiKit.Form.Serializers.inputSelector */
   1.145 +    inputSelector: function (element) {
   1.146 +        if (element.checked) {
   1.147 +            return [element.name, element.value];
   1.148 +        }
   1.149 +    },
   1.150 +
   1.151 +/** @id MochiKit.Form.Serializers.textarea */
   1.152 +    textarea: function (element) {
   1.153 +        return [element.name, element.value];
   1.154 +    },
   1.155 +
   1.156 +/** @id MochiKit.Form.Serializers.select */
   1.157 +    select: function (element) {
   1.158 +        return MochiKit.Form.Serializers[element.type == 'select-one' ?
   1.159 +        'selectOne' : 'selectMany'](element);
   1.160 +    },
   1.161 +
   1.162 +/** @id MochiKit.Form.Serializers.selectOne */
   1.163 +    selectOne: function (element) {
   1.164 +        var value = '', opt, index = element.selectedIndex;
   1.165 +        if (index >= 0) {
   1.166 +            opt = element.options[index];
   1.167 +            value = opt.value;
   1.168 +            if (!value && !('value' in opt)) {
   1.169 +                value = opt.text;
   1.170 +            }
   1.171 +        }
   1.172 +        return [element.name, value];
   1.173 +    },
   1.174 +
   1.175 +/** @id MochiKit.Form.Serializers.selectMany */
   1.176 +    selectMany: function (element) {
   1.177 +        var value = [];
   1.178 +        for (var i = 0; i < element.length; i++) {
   1.179 +            var opt = element.options[i];
   1.180 +            if (opt.selected) {
   1.181 +                var optValue = opt.value;
   1.182 +                if (!optValue && !('value' in opt)) {
   1.183 +                    optValue = opt.text;
   1.184 +                }
   1.185 +                value.push(optValue);
   1.186 +            }
   1.187 +        }
   1.188 +        return [element.name, value];
   1.189 +    }
   1.190 +};
   1.191 +
   1.192 +/** @id Ajax */
   1.193 +var Ajax = {
   1.194 +    activeRequestCount: 0
   1.195 +};
   1.196 +
   1.197 +Ajax.Responders = {
   1.198 +    responders: [],
   1.199 +
   1.200 +/** @id Ajax.Responders.register */
   1.201 +    register: function (responderToAdd) {
   1.202 +        if (MochiKit.Base.find(this.responders, responderToAdd) == -1) {
   1.203 +            this.responders.push(responderToAdd);
   1.204 +        }
   1.205 +    },
   1.206 +
   1.207 +/** @id Ajax.Responders.unregister */
   1.208 +    unregister: function (responderToRemove) {
   1.209 +        this.responders = this.responders.without(responderToRemove);
   1.210 +    },
   1.211 +
   1.212 +/** @id Ajax.Responders.dispatch */
   1.213 +    dispatch: function (callback, request, transport, json) {
   1.214 +        MochiKit.Iter.forEach(this.responders, function (responder) {
   1.215 +            if (responder[callback] &&
   1.216 +                typeof(responder[callback]) == 'function') {
   1.217 +                try {
   1.218 +                    responder[callback].apply(responder, [request, transport, json]);
   1.219 +                } catch (e) {}
   1.220 +            }
   1.221 +        });
   1.222 +    }
   1.223 +};
   1.224 +
   1.225 +Ajax.Responders.register({
   1.226 +
   1.227 +/** @id Ajax.Responders.onCreate */
   1.228 +    onCreate: function () {
   1.229 +        Ajax.activeRequestCount++;
   1.230 +    },
   1.231 +
   1.232 +/** @id Ajax.Responders.onComplete */
   1.233 +    onComplete: function () {
   1.234 +        Ajax.activeRequestCount--;
   1.235 +    }
   1.236 +});
   1.237 +
   1.238 +/** @id Ajax.Base */
   1.239 +Ajax.Base = function () {};
   1.240 +
   1.241 +Ajax.Base.prototype = {
   1.242 +
   1.243 +/** @id Ajax.Base.prototype.setOptions */
   1.244 +    setOptions: function (options) {
   1.245 +        this.options = {
   1.246 +            method: 'post',
   1.247 +            asynchronous: true,
   1.248 +            parameters:   ''
   1.249 +        }
   1.250 +        MochiKit.Base.update(this.options, options || {});
   1.251 +    },
   1.252 +
   1.253 +/** @id Ajax.Base.prototype.responseIsSuccess */
   1.254 +    responseIsSuccess: function () {
   1.255 +        return this.transport.status == undefined
   1.256 +            || this.transport.status === 0
   1.257 +            || (this.transport.status >= 200 && this.transport.status < 300);
   1.258 +    },
   1.259 +
   1.260 +/** @id Ajax.Base.prototype.responseIsFailure */
   1.261 +    responseIsFailure: function () {
   1.262 +        return !this.responseIsSuccess();
   1.263 +    }
   1.264 +};
   1.265 +
   1.266 +/** @id Ajax.Request */
   1.267 +Ajax.Request = function (url, options) {
   1.268 +    this.__init__(url, options);
   1.269 +};
   1.270 +
   1.271 +/** @id Ajax.Events */
   1.272 +Ajax.Request.Events = ['Uninitialized', 'Loading', 'Loaded',
   1.273 +                       'Interactive', 'Complete'];
   1.274 +
   1.275 +MochiKit.Base.update(Ajax.Request.prototype, Ajax.Base.prototype);
   1.276 +
   1.277 +MochiKit.Base.update(Ajax.Request.prototype, {
   1.278 +    __init__: function (url, options) {
   1.279 +        this.transport = MochiKit.Async.getXMLHttpRequest();
   1.280 +        this.setOptions(options);
   1.281 +        this.request(url);
   1.282 +    },
   1.283 +
   1.284 +/** @id Ajax.Request.prototype.request */
   1.285 +    request: function (url) {
   1.286 +        var parameters = this.options.parameters || '';
   1.287 +        if (parameters.length > 0){
   1.288 +            parameters += '&_=';
   1.289 +        }
   1.290 +
   1.291 +        try {
   1.292 +            this.url = url;
   1.293 +            if (this.options.method == 'get' && parameters.length > 0) {
   1.294 +                this.url += (this.url.match(/\?/) ? '&' : '?') + parameters;
   1.295 +            }
   1.296 +            Ajax.Responders.dispatch('onCreate', this, this.transport);
   1.297 +
   1.298 +            this.transport.open(this.options.method, this.url,
   1.299 +                                this.options.asynchronous);
   1.300 +
   1.301 +            if (this.options.asynchronous) {
   1.302 +                this.transport.onreadystatechange = MochiKit.Base.bind(this.onStateChange, this);
   1.303 +                setTimeout(MochiKit.Base.bind(function () {
   1.304 +                    this.respondToReadyState(1);
   1.305 +                }, this), 10);
   1.306 +            }
   1.307 +
   1.308 +            this.setRequestHeaders();
   1.309 +
   1.310 +            var body = this.options.postBody ? this.options.postBody : parameters;
   1.311 +            this.transport.send(this.options.method == 'post' ? body : null);
   1.312 +
   1.313 +        } catch (e) {
   1.314 +            this.dispatchException(e);
   1.315 +        }
   1.316 +    },
   1.317 +
   1.318 +/** @id Ajax.Request.prototype.setRequestHeaders */
   1.319 +    setRequestHeaders: function () {
   1.320 +        var requestHeaders = ['X-Requested-With', 'XMLHttpRequest'];
   1.321 +
   1.322 +        if (this.options.method == 'post') {
   1.323 +            requestHeaders.push('Content-type',
   1.324 +                                'application/x-www-form-urlencoded');
   1.325 +
   1.326 +            /* Force 'Connection: close' for Mozilla browsers to work around
   1.327 +             * a bug where XMLHttpRequest sends an incorrect Content-length
   1.328 +             * header. See Mozilla Bugzilla #246651.
   1.329 +             */
   1.330 +            if (this.transport.overrideMimeType) {
   1.331 +                requestHeaders.push('Connection', 'close');
   1.332 +            }
   1.333 +        }
   1.334 +
   1.335 +        if (this.options.requestHeaders) {
   1.336 +            requestHeaders.push.apply(requestHeaders, this.options.requestHeaders);
   1.337 +        }
   1.338 +
   1.339 +        for (var i = 0; i < requestHeaders.length; i += 2) {
   1.340 +            this.transport.setRequestHeader(requestHeaders[i], requestHeaders[i+1]);
   1.341 +        }
   1.342 +    },
   1.343 +
   1.344 +/** @id Ajax.Request.prototype.onStateChange */
   1.345 +    onStateChange: function () {
   1.346 +        var readyState = this.transport.readyState;
   1.347 +        if (readyState != 1) {
   1.348 +            this.respondToReadyState(this.transport.readyState);
   1.349 +        }
   1.350 +    },
   1.351 +
   1.352 +/** @id Ajax.Request.prototype.header */
   1.353 +    header: function (name) {
   1.354 +        try {
   1.355 +          return this.transport.getResponseHeader(name);
   1.356 +        } catch (e) {}
   1.357 +    },
   1.358 +
   1.359 +/** @id Ajax.Request.prototype.evalJSON */
   1.360 +    evalJSON: function () {
   1.361 +        try {
   1.362 +          return eval(this.header('X-JSON'));
   1.363 +        } catch (e) {}
   1.364 +    },
   1.365 +
   1.366 +/** @id Ajax.Request.prototype.evalResponse */
   1.367 +    evalResponse: function () {
   1.368 +        try {
   1.369 +          return eval(this.transport.responseText);
   1.370 +        } catch (e) {
   1.371 +          this.dispatchException(e);
   1.372 +        }
   1.373 +    },
   1.374 +
   1.375 +/** @id Ajax.Request.prototype.respondToReadyState */
   1.376 +    respondToReadyState: function (readyState) {
   1.377 +        var event = Ajax.Request.Events[readyState];
   1.378 +        var transport = this.transport, json = this.evalJSON();
   1.379 +
   1.380 +        if (event == 'Complete') {
   1.381 +            try {
   1.382 +                (this.options['on' + this.transport.status]
   1.383 +                || this.options['on' + (this.responseIsSuccess() ? 'Success' : 'Failure')]
   1.384 +                || MochiKit.Base.noop)(transport, json);
   1.385 +            } catch (e) {
   1.386 +                this.dispatchException(e);
   1.387 +            }
   1.388 +
   1.389 +            if ((this.header('Content-type') || '').match(/^text\/javascript/i)) {
   1.390 +                this.evalResponse();
   1.391 +            }
   1.392 +        }
   1.393 +
   1.394 +        try {
   1.395 +            (this.options['on' + event] || MochiKit.Base.noop)(transport, json);
   1.396 +            Ajax.Responders.dispatch('on' + event, this, transport, json);
   1.397 +        } catch (e) {
   1.398 +            this.dispatchException(e);
   1.399 +        }
   1.400 +
   1.401 +        /* Avoid memory leak in MSIE: clean up the oncomplete event handler */
   1.402 +        if (event == 'Complete') {
   1.403 +            this.transport.onreadystatechange = MochiKit.Base.noop;
   1.404 +        }
   1.405 +    },
   1.406 +
   1.407 +/** @id Ajax.Request.prototype.dispatchException */
   1.408 +    dispatchException: function (exception) {
   1.409 +        (this.options.onException || MochiKit.Base.noop)(this, exception);
   1.410 +        Ajax.Responders.dispatch('onException', this, exception);
   1.411 +    }
   1.412 +});
   1.413 +
   1.414 +/** @id Ajax.Updater */
   1.415 +Ajax.Updater = function (container, url, options) {
   1.416 +    this.__init__(container, url, options);
   1.417 +};
   1.418 +
   1.419 +MochiKit.Base.update(Ajax.Updater.prototype, Ajax.Request.prototype);
   1.420 +
   1.421 +MochiKit.Base.update(Ajax.Updater.prototype, {
   1.422 +    __init__: function (container, url, options) {
   1.423 +        this.containers = {
   1.424 +            success: container.success ? MochiKit.DOM.getElement(container.success) : MochiKit.DOM.getElement(container),
   1.425 +            failure: container.failure ? MochiKit.DOM.getElement(container.failure) :
   1.426 +                (container.success ? null : MochiKit.DOM.getElement(container))
   1.427 +        }
   1.428 +        this.transport = MochiKit.Async.getXMLHttpRequest();
   1.429 +        this.setOptions(options);
   1.430 +
   1.431 +        var onComplete = this.options.onComplete || MochiKit.Base.noop;
   1.432 +        this.options.onComplete = MochiKit.Base.bind(function (transport, object) {
   1.433 +            this.updateContent();
   1.434 +            onComplete(transport, object);
   1.435 +        }, this);
   1.436 +
   1.437 +        this.request(url);
   1.438 +    },
   1.439 +
   1.440 +/** @id Ajax.Updater.prototype.updateContent */
   1.441 +    updateContent: function () {
   1.442 +        var receiver = this.responseIsSuccess() ?
   1.443 +            this.containers.success : this.containers.failure;
   1.444 +        var response = this.transport.responseText;
   1.445 +
   1.446 +        if (!this.options.evalScripts) {
   1.447 +            response = MochiKit.Base.stripScripts(response);
   1.448 +        }
   1.449 +
   1.450 +        if (receiver) {
   1.451 +            if (this.options.insertion) {
   1.452 +                new this.options.insertion(receiver, response);
   1.453 +            } else {
   1.454 +                MochiKit.DOM.getElement(receiver).innerHTML =
   1.455 +                    MochiKit.Base.stripScripts(response);
   1.456 +                setTimeout(function () {
   1.457 +                    MochiKit.Base.evalScripts(response);
   1.458 +                }, 10);
   1.459 +            }
   1.460 +        }
   1.461 +
   1.462 +        if (this.responseIsSuccess()) {
   1.463 +            if (this.onComplete) {
   1.464 +                setTimeout(MochiKit.Base.bind(this.onComplete, this), 10);
   1.465 +            }
   1.466 +        }
   1.467 +    }
   1.468 +});
   1.469 +
   1.470 +/** @id Field */
   1.471 +var Field = {
   1.472 +
   1.473 +/** @id clear */
   1.474 +    clear: function () {
   1.475 +        for (var i = 0; i < arguments.length; i++) {
   1.476 +            MochiKit.DOM.getElement(arguments[i]).value = '';
   1.477 +        }
   1.478 +    },
   1.479 +
   1.480 +/** @id focus */
   1.481 +    focus: function (element) {
   1.482 +        MochiKit.DOM.getElement(element).focus();
   1.483 +    },
   1.484 +
   1.485 +/** @id present */
   1.486 +    present: function () {
   1.487 +        for (var i = 0; i < arguments.length; i++) {
   1.488 +            if (MochiKit.DOM.getElement(arguments[i]).value == '') {
   1.489 +                return false;
   1.490 +            }
   1.491 +        }
   1.492 +        return true;
   1.493 +    },
   1.494 +
   1.495 +/** @id select */
   1.496 +    select: function (element) {
   1.497 +        MochiKit.DOM.getElement(element).select();
   1.498 +    },
   1.499 +
   1.500 +/** @id activate */
   1.501 +    activate: function (element) {
   1.502 +        element = MochiKit.DOM.getElement(element);
   1.503 +        element.focus();
   1.504 +        if (element.select) {
   1.505 +            element.select();
   1.506 +        }
   1.507 +    },
   1.508 +
   1.509 +/** @id scrollFreeActivate */
   1.510 +    scrollFreeActivate: function (field) {
   1.511 +        setTimeout(function () {
   1.512 +            Field.activate(field);
   1.513 +        }, 1);
   1.514 +    }
   1.515 +};
   1.516 +
   1.517 +
   1.518 +/** @id Autocompleter */
   1.519 +var Autocompleter = {};
   1.520 +
   1.521 +/** @id Autocompleter.Base */
   1.522 +Autocompleter.Base = function () {};
   1.523 +
   1.524 +Autocompleter.Base.prototype = {
   1.525 +
   1.526 +/** @id Autocompleter.Base.prototype.baseInitialize */
   1.527 +    baseInitialize: function (element, update, options) {
   1.528 +        this.element = MochiKit.DOM.getElement(element);
   1.529 +        this.update = MochiKit.DOM.getElement(update);
   1.530 +        this.hasFocus = false;
   1.531 +        this.changed = false;
   1.532 +        this.active = false;
   1.533 +        this.index = 0;
   1.534 +        this.entryCount = 0;
   1.535 +
   1.536 +        if (this.setOptions) {
   1.537 +            this.setOptions(options);
   1.538 +        }
   1.539 +        else {
   1.540 +            this.options = options || {};
   1.541 +        }
   1.542 +
   1.543 +        this.options.paramName = this.options.paramName || this.element.name;
   1.544 +        this.options.tokens = this.options.tokens || [];
   1.545 +        this.options.frequency = this.options.frequency || 0.4;
   1.546 +        this.options.minChars = this.options.minChars || 1;
   1.547 +        this.options.onShow = this.options.onShow || function (element, update) {
   1.548 +                if (!update.style.position || update.style.position == 'absolute') {
   1.549 +                    update.style.position = 'absolute';
   1.550 +                    MochiKit.Position.clone(element, update, {
   1.551 +                        setHeight: false,
   1.552 +                        offsetTop: element.offsetHeight
   1.553 +                    });
   1.554 +                }
   1.555 +                MochiKit.Visual.appear(update, {duration:0.15});
   1.556 +            };
   1.557 +        this.options.onHide = this.options.onHide || function (element, update) {
   1.558 +                MochiKit.Visual.fade(update, {duration: 0.15});
   1.559 +            };
   1.560 +
   1.561 +        if (typeof(this.options.tokens) == 'string') {
   1.562 +            this.options.tokens = new Array(this.options.tokens);
   1.563 +        }
   1.564 +
   1.565 +        this.observer = null;
   1.566 +
   1.567 +        this.element.setAttribute('autocomplete', 'off');
   1.568 +
   1.569 +        MochiKit.Style.hideElement(this.update);
   1.570 +
   1.571 +        MochiKit.Signal.connect(this.element, 'onblur', this, this.onBlur);
   1.572 +        MochiKit.Signal.connect(this.element, 'onkeypress', this, this.onKeyPress, this);
   1.573 +    },
   1.574 +
   1.575 +/** @id Autocompleter.Base.prototype.show */
   1.576 +    show: function () {
   1.577 +        if (MochiKit.Style.getStyle(this.update, 'display') == 'none') {
   1.578 +            this.options.onShow(this.element, this.update);
   1.579 +        }
   1.580 +        if (!this.iefix && /MSIE/.test(navigator.userAgent &&
   1.581 +            (MochiKit.Style.getStyle(this.update, 'position') == 'absolute'))) {
   1.582 +            new Insertion.After(this.update,
   1.583 +             '<iframe id="' + this.update.id + '_iefix" '+
   1.584 +             'style="display:none;position:absolute;filter:progid:DXImageTransform.Microsoft.Alpha(opacity=0);" ' +
   1.585 +             'src="javascript:false;" frameborder="0" scrolling="no"></iframe>');
   1.586 +            this.iefix = MochiKit.DOM.getElement(this.update.id + '_iefix');
   1.587 +        }
   1.588 +        if (this.iefix) {
   1.589 +            setTimeout(MochiKit.Base.bind(this.fixIEOverlapping, this), 50);
   1.590 +        }
   1.591 +    },
   1.592 +
   1.593 +/** @id Autocompleter.Base.prototype.fixIEOverlapping */
   1.594 +    fixIEOverlapping: function () {
   1.595 +        MochiKit.Position.clone(this.update, this.iefix);
   1.596 +        this.iefix.style.zIndex = 1;
   1.597 +        this.update.style.zIndex = 2;
   1.598 +        MochiKit.Style.showElement(this.iefix);
   1.599 +    },
   1.600 +
   1.601 +/** @id Autocompleter.Base.prototype.hide */
   1.602 +    hide: function () {
   1.603 +        this.stopIndicator();
   1.604 +        if (MochiKit.Style.getStyle(this.update, 'display') != 'none') {
   1.605 +            this.options.onHide(this.element, this.update);
   1.606 +        }
   1.607 +        if (this.iefix) {
   1.608 +            MochiKit.Style.hideElement(this.iefix);
   1.609 +        }
   1.610 +    },
   1.611 +
   1.612 +/** @id Autocompleter.Base.prototype.startIndicator */
   1.613 +    startIndicator: function () {
   1.614 +        if (this.options.indicator) {
   1.615 +            MochiKit.Style.showElement(this.options.indicator);
   1.616 +        }
   1.617 +    },
   1.618 +
   1.619 +/** @id Autocompleter.Base.prototype.stopIndicator */
   1.620 +    stopIndicator: function () {
   1.621 +        if (this.options.indicator) {
   1.622 +            MochiKit.Style.hideElement(this.options.indicator);
   1.623 +        }
   1.624 +    },
   1.625 +
   1.626 +/** @id Autocompleter.Base.prototype.onKeyPress */
   1.627 +    onKeyPress: function (event) {
   1.628 +        if (this.active) {
   1.629 +            if (event.key().string == "KEY_TAB" || event.key().string == "KEY_RETURN") {
   1.630 +                 this.selectEntry();
   1.631 +                 MochiKit.Event.stop(event);
   1.632 +            } else if (event.key().string == "KEY_ESCAPE") {
   1.633 +                 this.hide();
   1.634 +                 this.active = false;
   1.635 +                 MochiKit.Event.stop(event);
   1.636 +                 return;
   1.637 +            } else if (event.key().string == "KEY_LEFT" || event.key().string == "KEY_RIGHT") {
   1.638 +                 return;
   1.639 +            } else if (event.key().string == "KEY_UP") {
   1.640 +                 this.markPrevious();
   1.641 +                 this.render();
   1.642 +                 if (/AppleWebKit'/.test(navigator.appVersion)) {
   1.643 +                     event.stop();
   1.644 +                 }
   1.645 +                 return;
   1.646 +            } else if (event.key().string == "KEY_DOWN") {
   1.647 +                 this.markNext();
   1.648 +                 this.render();
   1.649 +                 if (/AppleWebKit'/.test(navigator.appVersion)) {
   1.650 +                     event.stop();
   1.651 +                 }
   1.652 +                 return;
   1.653 +            }
   1.654 +        } else {
   1.655 +            if (event.key().string == "KEY_TAB" || event.key().string == "KEY_RETURN") {
   1.656 +                return;
   1.657 +            }
   1.658 +        }
   1.659 +
   1.660 +        this.changed = true;
   1.661 +        this.hasFocus = true;
   1.662 +
   1.663 +        if (this.observer) {
   1.664 +            clearTimeout(this.observer);
   1.665 +        }
   1.666 +        this.observer = setTimeout(MochiKit.Base.bind(this.onObserverEvent, this),
   1.667 +                                   this.options.frequency*1000);
   1.668 +    },
   1.669 +
   1.670 +/** @id Autocompleter.Base.prototype.findElement */
   1.671 +    findElement: function (event, tagName) {
   1.672 +        var element = event.target;
   1.673 +        while (element.parentNode && (!element.tagName ||
   1.674 +              (element.tagName.toUpperCase() != tagName.toUpperCase()))) {
   1.675 +            element = element.parentNode;
   1.676 +        }
   1.677 +        return element;
   1.678 +    },
   1.679 +
   1.680 +/** @id Autocompleter.Base.prototype.hover */
   1.681 +    onHover: function (event) {
   1.682 +        var element = this.findElement(event, 'LI');
   1.683 +        if (this.index != element.autocompleteIndex) {
   1.684 +            this.index = element.autocompleteIndex;
   1.685 +            this.render();
   1.686 +        }
   1.687 +        event.stop();
   1.688 +    },
   1.689 +
   1.690 +/** @id Autocompleter.Base.prototype.onClick */
   1.691 +    onClick: function (event) {
   1.692 +        var element = this.findElement(event, 'LI');
   1.693 +        this.index = element.autocompleteIndex;
   1.694 +        this.selectEntry();
   1.695 +        this.hide();
   1.696 +    },
   1.697 +
   1.698 +/** @id Autocompleter.Base.prototype.onBlur */
   1.699 +    onBlur: function (event) {
   1.700 +        // needed to make click events working
   1.701 +        setTimeout(MochiKit.Base.bind(this.hide, this), 250);
   1.702 +        this.hasFocus = false;
   1.703 +        this.active = false;
   1.704 +    },
   1.705 +
   1.706 +/** @id Autocompleter.Base.prototype.render */
   1.707 +    render: function () {
   1.708 +        if (this.entryCount > 0) {
   1.709 +            for (var i = 0; i < this.entryCount; i++) {
   1.710 +                this.index == i ?
   1.711 +                    MochiKit.DOM.addElementClass(this.getEntry(i), 'selected') :
   1.712 +                    MochiKit.DOM.removeElementClass(this.getEntry(i), 'selected');
   1.713 +            }
   1.714 +            if (this.hasFocus) {
   1.715 +                this.show();
   1.716 +                this.active = true;
   1.717 +            }
   1.718 +        } else {
   1.719 +            this.active = false;
   1.720 +            this.hide();
   1.721 +        }
   1.722 +    },
   1.723 +
   1.724 +/** @id Autocompleter.Base.prototype.markPrevious */
   1.725 +    markPrevious: function () {
   1.726 +        if (this.index > 0) {
   1.727 +            this.index--
   1.728 +        } else {
   1.729 +            this.index = this.entryCount-1;
   1.730 +        }
   1.731 +    },
   1.732 +
   1.733 +/** @id Autocompleter.Base.prototype.markNext */
   1.734 +    markNext: function () {
   1.735 +        if (this.index < this.entryCount-1) {
   1.736 +            this.index++
   1.737 +        } else {
   1.738 +            this.index = 0;
   1.739 +        }
   1.740 +    },
   1.741 +
   1.742 +/** @id Autocompleter.Base.prototype.getEntry */
   1.743 +    getEntry: function (index) {
   1.744 +        return this.update.firstChild.childNodes[index];
   1.745 +    },
   1.746 +
   1.747 +/** @id Autocompleter.Base.prototype.getCurrentEntry */
   1.748 +    getCurrentEntry: function () {
   1.749 +        return this.getEntry(this.index);
   1.750 +    },
   1.751 +
   1.752 +/** @id Autocompleter.Base.prototype.selectEntry */
   1.753 +    selectEntry: function () {
   1.754 +        this.active = false;
   1.755 +        this.updateElement(this.getCurrentEntry());
   1.756 +    },
   1.757 +
   1.758 +/** @id Autocompleter.Base.prototype.collectTextNodesIgnoreClass */
   1.759 +    collectTextNodesIgnoreClass: function (element, className) {
   1.760 +        return MochiKit.Base.flattenArray(MochiKit.Base.map(function (node) {
   1.761 +            if (node.nodeType == 3) {
   1.762 +                return node.nodeValue;
   1.763 +            } else if (node.hasChildNodes() && !MochiKit.DOM.hasElementClass(node, className)) {
   1.764 +                return this.collectTextNodesIgnoreClass(node, className);
   1.765 +            }
   1.766 +            return '';
   1.767 +        }, MochiKit.DOM.getElement(element).childNodes)).join('');
   1.768 +    },
   1.769 +
   1.770 +/** @id Autocompleter.Base.prototype.updateElement */
   1.771 +    updateElement: function (selectedElement) {
   1.772 +        if (this.options.updateElement) {
   1.773 +            this.options.updateElement(selectedElement);
   1.774 +            return;
   1.775 +        }
   1.776 +        var value = '';
   1.777 +        if (this.options.select) {
   1.778 +            var nodes = document.getElementsByClassName(this.options.select, selectedElement) || [];
   1.779 +            if (nodes.length > 0) {
   1.780 +                value = MochiKit.DOM.scrapeText(nodes[0]);
   1.781 +            }
   1.782 +        } else {
   1.783 +            value = this.collectTextNodesIgnoreClass(selectedElement, 'informal');
   1.784 +        }
   1.785 +        var lastTokenPos = this.findLastToken();
   1.786 +        if (lastTokenPos != -1) {
   1.787 +            var newValue = this.element.value.substr(0, lastTokenPos + 1);
   1.788 +            var whitespace = this.element.value.substr(lastTokenPos + 1).match(/^\s+/);
   1.789 +            if (whitespace) {
   1.790 +                newValue += whitespace[0];
   1.791 +            }
   1.792 +            this.element.value = newValue + value;
   1.793 +        } else {
   1.794 +            this.element.value = value;
   1.795 +        }
   1.796 +        this.element.focus();
   1.797 +
   1.798 +        if (this.options.afterUpdateElement) {
   1.799 +            this.options.afterUpdateElement(this.element, selectedElement);
   1.800 +        }
   1.801 +    },
   1.802 +
   1.803 +/** @id Autocompleter.Base.prototype.updateChoices */
   1.804 +    updateChoices: function (choices) {
   1.805 +        if (!this.changed && this.hasFocus) {
   1.806 +            this.update.innerHTML = choices;
   1.807 +            var d = MochiKit.DOM;
   1.808 +            d.removeEmptyTextNodes(this.update);
   1.809 +            d.removeEmptyTextNodes(this.update.firstChild);
   1.810 +
   1.811 +            if (this.update.firstChild && this.update.firstChild.childNodes) {
   1.812 +                this.entryCount = this.update.firstChild.childNodes.length;
   1.813 +                for (var i = 0; i < this.entryCount; i++) {
   1.814 +                    var entry = this.getEntry(i);
   1.815 +                    entry.autocompleteIndex = i;
   1.816 +                    this.addObservers(entry);
   1.817 +                }
   1.818 +            } else {
   1.819 +                this.entryCount = 0;
   1.820 +            }
   1.821 +
   1.822 +            this.stopIndicator();
   1.823 +
   1.824 +            this.index = 0;
   1.825 +            this.render();
   1.826 +        }
   1.827 +    },
   1.828 +
   1.829 +/** @id Autocompleter.Base.prototype.addObservers */
   1.830 +    addObservers: function (element) {
   1.831 +        MochiKit.Signal.connect(element, 'onmouseover', this, this.onHover);
   1.832 +        MochiKit.Signal.connect(element, 'onclick', this, this.onClick);
   1.833 +    },
   1.834 +
   1.835 +/** @id Autocompleter.Base.prototype.onObserverEvent */
   1.836 +    onObserverEvent: function () {
   1.837 +        this.changed = false;
   1.838 +        if (this.getToken().length >= this.options.minChars) {
   1.839 +            this.startIndicator();
   1.840 +            this.getUpdatedChoices();
   1.841 +        } else {
   1.842 +            this.active = false;
   1.843 +            this.hide();
   1.844 +        }
   1.845 +    },
   1.846 +
   1.847 +/** @id Autocompleter.Base.prototype.getToken */
   1.848 +    getToken: function () {
   1.849 +        var tokenPos = this.findLastToken();
   1.850 +        if (tokenPos != -1) {
   1.851 +            var ret = this.element.value.substr(tokenPos + 1).replace(/^\s+/,'').replace(/\s+$/,'');
   1.852 +        } else {
   1.853 +            var ret = this.element.value;
   1.854 +        }
   1.855 +        return /\n/.test(ret) ? '' : ret;
   1.856 +    },
   1.857 +
   1.858 +/** @id Autocompleter.Base.prototype.findLastToken */
   1.859 +    findLastToken: function () {
   1.860 +        var lastTokenPos = -1;
   1.861 +
   1.862 +        for (var i = 0; i < this.options.tokens.length; i++) {
   1.863 +            var thisTokenPos = this.element.value.lastIndexOf(this.options.tokens[i]);
   1.864 +            if (thisTokenPos > lastTokenPos) {
   1.865 +                lastTokenPos = thisTokenPos;
   1.866 +            }
   1.867 +        }
   1.868 +        return lastTokenPos;
   1.869 +    }
   1.870 +}
   1.871 +
   1.872 +/** @id Ajax.Autocompleter */
   1.873 +Ajax.Autocompleter = function (element, update, url, options) {
   1.874 +    this.__init__(element, update, url, options);
   1.875 +};
   1.876 +
   1.877 +MochiKit.Base.update(Ajax.Autocompleter.prototype, Autocompleter.Base.prototype);
   1.878 +
   1.879 +MochiKit.Base.update(Ajax.Autocompleter.prototype, {
   1.880 +    __init__: function (element, update, url, options) {
   1.881 +        this.baseInitialize(element, update, options);
   1.882 +        this.options.asynchronous = true;
   1.883 +        this.options.onComplete = MochiKit.Base.bind(this.onComplete, this);
   1.884 +        this.options.defaultParams = this.options.parameters || null;
   1.885 +        this.url = url;
   1.886 +    },
   1.887 +
   1.888 +/** @id Ajax.Autocompleter.prototype.getUpdatedChoices */
   1.889 +    getUpdatedChoices: function () {
   1.890 +        var entry = encodeURIComponent(this.options.paramName) + '=' +
   1.891 +            encodeURIComponent(this.getToken());
   1.892 +
   1.893 +        this.options.parameters = this.options.callback ?
   1.894 +            this.options.callback(this.element, entry) : entry;
   1.895 +
   1.896 +        if (this.options.defaultParams) {
   1.897 +            this.options.parameters += '&' + this.options.defaultParams;
   1.898 +        }
   1.899 +        new Ajax.Request(this.url, this.options);
   1.900 +    },
   1.901 +
   1.902 +/** @id Ajax.Autocompleter.prototype.onComplete */
   1.903 +    onComplete: function (request) {
   1.904 +        this.updateChoices(request.responseText);
   1.905 +    }
   1.906 +});
   1.907 +
   1.908 +/***
   1.909 +
   1.910 +The local array autocompleter. Used when you'd prefer to
   1.911 +inject an array of autocompletion options into the page, rather
   1.912 +than sending out Ajax queries, which can be quite slow sometimes.
   1.913 +
   1.914 +The constructor takes four parameters. The first two are, as usual,
   1.915 +the id of the monitored textbox, and id of the autocompletion menu.
   1.916 +The third is the array you want to autocomplete from, and the fourth
   1.917 +is the options block.
   1.918 +
   1.919 +Extra local autocompletion options:
   1.920 +- choices - How many autocompletion choices to offer
   1.921 +
   1.922 +- partialSearch - If false, the autocompleter will match entered
   1.923 +                                       text only at the beginning of strings in the
   1.924 +                                       autocomplete array. Defaults to true, which will
   1.925 +                                       match text at the beginning of any *word* in the
   1.926 +                                       strings in the autocomplete array. If you want to
   1.927 +                                       search anywhere in the string, additionally set
   1.928 +                                       the option fullSearch to true (default: off).
   1.929 +
   1.930 +- fullSsearch - Search anywhere in autocomplete array strings.
   1.931 +
   1.932 +- partialChars - How many characters to enter before triggering
   1.933 +                                    a partial match (unlike minChars, which defines
   1.934 +                                    how many characters are required to do any match
   1.935 +                                    at all). Defaults to 2.
   1.936 +
   1.937 +- ignoreCase - Whether to ignore case when autocompleting.
   1.938 +                                Defaults to true.
   1.939 +
   1.940 +It's possible to pass in a custom function as the 'selector'
   1.941 +option, if you prefer to write your own autocompletion logic.
   1.942 +In that case, the other options above will not apply unless
   1.943 +you support them.
   1.944 +
   1.945 +***/
   1.946 +
   1.947 +/** @id Autocompleter.Local */
   1.948 +Autocompleter.Local = function (element, update, array, options) {
   1.949 +    this.__init__(element, update, array, options);
   1.950 +};
   1.951 +
   1.952 +MochiKit.Base.update(Autocompleter.Local.prototype, Autocompleter.Base.prototype);
   1.953 +
   1.954 +MochiKit.Base.update(Autocompleter.Local.prototype, {
   1.955 +    __init__: function (element, update, array, options) {
   1.956 +        this.baseInitialize(element, update, options);
   1.957 +        this.options.array = array;
   1.958 +    },
   1.959 +
   1.960 +/** @id Autocompleter.Local.prototype.getUpdatedChoices */
   1.961 +    getUpdatedChoices: function () {
   1.962 +        this.updateChoices(this.options.selector(this));
   1.963 +    },
   1.964 +
   1.965 +/** @id Autocompleter.Local.prototype.setOptions */
   1.966 +    setOptions: function (options) {
   1.967 +        this.options = MochiKit.Base.update({
   1.968 +            choices: 10,
   1.969 +            partialSearch: true,
   1.970 +            partialChars: 2,
   1.971 +            ignoreCase: true,
   1.972 +            fullSearch: false,
   1.973 +            selector: function (instance) {
   1.974 +                var ret = [];  // Beginning matches
   1.975 +                var partial = [];  // Inside matches
   1.976 +                var entry = instance.getToken();
   1.977 +                var count = 0;
   1.978 +
   1.979 +                for (var i = 0; i < instance.options.array.length &&
   1.980 +                    ret.length < instance.options.choices ; i++) {
   1.981 +
   1.982 +                    var elem = instance.options.array[i];
   1.983 +                    var foundPos = instance.options.ignoreCase ?
   1.984 +                        elem.toLowerCase().indexOf(entry.toLowerCase()) :
   1.985 +                        elem.indexOf(entry);
   1.986 +
   1.987 +                    while (foundPos != -1) {
   1.988 +                        if (foundPos === 0 && elem.length != entry.length) {
   1.989 +                            ret.push('<li><strong>' + elem.substr(0, entry.length) + '</strong>' +
   1.990 +                                elem.substr(entry.length) + '</li>');
   1.991 +                            break;
   1.992 +                        } else if (entry.length >= instance.options.partialChars &&
   1.993 +                            instance.options.partialSearch && foundPos != -1) {
   1.994 +                            if (instance.options.fullSearch || /\s/.test(elem.substr(foundPos - 1, 1))) {
   1.995 +                                partial.push('<li>' + elem.substr(0, foundPos) + '<strong>' +
   1.996 +                                    elem.substr(foundPos, entry.length) + '</strong>' + elem.substr(
   1.997 +                                    foundPos + entry.length) + '</li>');
   1.998 +                                break;
   1.999 +                            }
  1.1000 +                        }
  1.1001 +
  1.1002 +                        foundPos = instance.options.ignoreCase ?
  1.1003 +                            elem.toLowerCase().indexOf(entry.toLowerCase(), foundPos + 1) :
  1.1004 +                            elem.indexOf(entry, foundPos + 1);
  1.1005 +
  1.1006 +                    }
  1.1007 +                }
  1.1008 +                if (partial.length) {
  1.1009 +                    ret = ret.concat(partial.slice(0, instance.options.choices - ret.length))
  1.1010 +                }
  1.1011 +                return '<ul>' + ret.join('') + '</ul>';
  1.1012 +            }
  1.1013 +        }, options || {});
  1.1014 +    }
  1.1015 +});
  1.1016 +
  1.1017 +/***
  1.1018 +
  1.1019 +AJAX in-place editor
  1.1020 +
  1.1021 +see documentation on http://wiki.script.aculo.us/scriptaculous/show/Ajax.InPlaceEditor
  1.1022 +
  1.1023 +Use this if you notice weird scrolling problems on some browsers,
  1.1024 +the DOM might be a bit confused when this gets called so do this
  1.1025 +waits 1 ms (with setTimeout) until it does the activation
  1.1026 +
  1.1027 +***/
  1.1028 +
  1.1029 +/** @id Ajax.InPlaceEditor */
  1.1030 +Ajax.InPlaceEditor = function (element, url, options) {
  1.1031 +    this.__init__(element, url, options);
  1.1032 +};
  1.1033 +
  1.1034 +/** @id Ajax.InPlaceEditor.defaultHighlightColor */
  1.1035 +Ajax.InPlaceEditor.defaultHighlightColor = '#FFFF99';
  1.1036 +
  1.1037 +Ajax.InPlaceEditor.prototype = {
  1.1038 +    __init__: function (element, url, options) {
  1.1039 +        this.url = url;
  1.1040 +        this.element = MochiKit.DOM.getElement(element);
  1.1041 +
  1.1042 +        this.options = MochiKit.Base.update({
  1.1043 +            okButton: true,
  1.1044 +            okText: 'ok',
  1.1045 +            cancelLink: true,
  1.1046 +            cancelText: 'cancel',
  1.1047 +            savingText: 'Saving...',
  1.1048 +            clickToEditText: 'Click to edit',
  1.1049 +            okText: 'ok',
  1.1050 +            rows: 1,
  1.1051 +            onComplete: function (transport, element) {
  1.1052 +                new MochiKit.Visual.Highlight(element, {startcolor: this.options.highlightcolor});
  1.1053 +            },
  1.1054 +            onFailure: function (transport) {
  1.1055 +                alert('Error communicating with the server: ' + MochiKit.Base.stripTags(transport.responseText));
  1.1056 +            },
  1.1057 +            callback: function (form) {
  1.1058 +                return MochiKit.DOM.formContents(form);
  1.1059 +            },
  1.1060 +            handleLineBreaks: true,
  1.1061 +            loadingText: 'Loading...',
  1.1062 +            savingClassName: 'inplaceeditor-saving',
  1.1063 +            loadingClassName: 'inplaceeditor-loading',
  1.1064 +            formClassName: 'inplaceeditor-form',
  1.1065 +            highlightcolor: Ajax.InPlaceEditor.defaultHighlightColor,
  1.1066 +            highlightendcolor: '#FFFFFF',
  1.1067 +            externalControl: null,
  1.1068 +            submitOnBlur: false,
  1.1069 +            ajaxOptions: {}
  1.1070 +        }, options || {});
  1.1071 +
  1.1072 +        if (!this.options.formId && this.element.id) {
  1.1073 +            this.options.formId = this.element.id + '-inplaceeditor';
  1.1074 +            if (MochiKit.DOM.getElement(this.options.formId)) {
  1.1075 +                // there's already a form with that name, don't specify an id
  1.1076 +                this.options.formId = null;
  1.1077 +            }
  1.1078 +        }
  1.1079 +
  1.1080 +        if (this.options.externalControl) {
  1.1081 +            this.options.externalControl = MochiKit.DOM.getElement(this.options.externalControl);
  1.1082 +        }
  1.1083 +
  1.1084 +        this.originalBackground = MochiKit.Style.getStyle(this.element, 'background-color');
  1.1085 +        if (!this.originalBackground) {
  1.1086 +            this.originalBackground = 'transparent';
  1.1087 +        }
  1.1088 +
  1.1089 +        this.element.title = this.options.clickToEditText;
  1.1090 +
  1.1091 +        this.onclickListener = MochiKit.Signal.connect(this.element, 'onclick', this, this.enterEditMode);
  1.1092 +        this.mouseoverListener = MochiKit.Signal.connect(this.element, 'onmouseover', this, this.enterHover);
  1.1093 +        this.mouseoutListener = MochiKit.Signal.connect(this.element, 'onmouseout', this, this.leaveHover);
  1.1094 +        if (this.options.externalControl) {
  1.1095 +            this.onclickListenerExternal = MochiKit.Signal.connect(this.options.externalControl,
  1.1096 +                'onclick', this, this.enterEditMode);
  1.1097 +            this.mouseoverListenerExternal = MochiKit.Signal.connect(this.options.externalControl,
  1.1098 +                'onmouseover', this, this.enterHover);
  1.1099 +            this.mouseoutListenerExternal = MochiKit.Signal.connect(this.options.externalControl,
  1.1100 +                'onmouseout', this, this.leaveHover);
  1.1101 +        }
  1.1102 +    },
  1.1103 +
  1.1104 +/** @id Ajax.InPlaceEditor.prototype.enterEditMode */
  1.1105 +    enterEditMode: function (evt) {
  1.1106 +        if (this.saving) {
  1.1107 +            return;
  1.1108 +        }
  1.1109 +        if (this.editing) {
  1.1110 +            return;
  1.1111 +        }
  1.1112 +        this.editing = true;
  1.1113 +        this.onEnterEditMode();
  1.1114 +        if (this.options.externalControl) {
  1.1115 +            MochiKit.Style.hideElement(this.options.externalControl);
  1.1116 +        }
  1.1117 +        MochiKit.Style.hideElement(this.element);
  1.1118 +        this.createForm();
  1.1119 +        this.element.parentNode.insertBefore(this.form, this.element);
  1.1120 +        Field.scrollFreeActivate(this.editField);
  1.1121 +        // stop the event to avoid a page refresh in Safari
  1.1122 +        if (evt) {
  1.1123 +            evt.stop();
  1.1124 +        }
  1.1125 +        return false;
  1.1126 +    },
  1.1127 +
  1.1128 +/** @id Ajax.InPlaceEditor.prototype.createForm */
  1.1129 +    createForm: function () {
  1.1130 +        this.form = document.createElement('form');
  1.1131 +        this.form.id = this.options.formId;
  1.1132 +        MochiKit.DOM.addElementClass(this.form, this.options.formClassName)
  1.1133 +        this.form.onsubmit = MochiKit.Base.bind(this.onSubmit, this);
  1.1134 +
  1.1135 +        this.createEditField();
  1.1136 +
  1.1137 +        if (this.options.textarea) {
  1.1138 +            var br = document.createElement('br');
  1.1139 +            this.form.appendChild(br);
  1.1140 +        }
  1.1141 +
  1.1142 +        if (this.options.okButton) {
  1.1143 +            okButton = document.createElement('input');
  1.1144 +            okButton.type = 'submit';
  1.1145 +            okButton.value = this.options.okText;
  1.1146 +            this.form.appendChild(okButton);
  1.1147 +        }
  1.1148 +
  1.1149 +        if (this.options.cancelLink) {
  1.1150 +            cancelLink = document.createElement('a');
  1.1151 +            cancelLink.href = '#';
  1.1152 +            cancelLink.appendChild(document.createTextNode(this.options.cancelText));
  1.1153 +            cancelLink.onclick = MochiKit.Base.bind(this.onclickCancel, this);
  1.1154 +            this.form.appendChild(cancelLink);
  1.1155 +        }
  1.1156 +    },
  1.1157 +
  1.1158 +/** @id Ajax.InPlaceEditor.prototype.hasHTMLLineBreaks */
  1.1159 +    hasHTMLLineBreaks: function (string) {
  1.1160 +        if (!this.options.handleLineBreaks) {
  1.1161 +            return false;
  1.1162 +        }
  1.1163 +        return string.match(/<br/i) || string.match(/<p>/i);
  1.1164 +    },
  1.1165 +
  1.1166 +/** @id Ajax.InPlaceEditor.prototype.convertHTMLLineBreaks */
  1.1167 +    convertHTMLLineBreaks: function (string) {
  1.1168 +        return string.replace(/<br>/gi, '\n').replace(/<br\/>/gi, '\n').replace(/<\/p>/gi, '\n').replace(/<p>/gi, '');
  1.1169 +    },
  1.1170 +
  1.1171 +/** @id Ajax.InPlaceEditor.prototype.createEditField */
  1.1172 +    createEditField: function () {
  1.1173 +        var text;
  1.1174 +        if (this.options.loadTextURL) {
  1.1175 +            text = this.options.loadingText;
  1.1176 +        } else {
  1.1177 +            text = this.getText();
  1.1178 +        }
  1.1179 +
  1.1180 +        var obj = this;
  1.1181 +
  1.1182 +        if (this.options.rows == 1 && !this.hasHTMLLineBreaks(text)) {
  1.1183 +            this.options.textarea = false;
  1.1184 +            var textField = document.createElement('input');
  1.1185 +            textField.obj = this;
  1.1186 +            textField.type = 'text';
  1.1187 +            textField.name = 'value';
  1.1188 +            textField.value = text;
  1.1189 +            textField.style.backgroundColor = this.options.highlightcolor;
  1.1190 +            var size = this.options.size || this.options.cols || 0;
  1.1191 +            if (size !== 0) {
  1.1192 +                textField.size = size;
  1.1193 +            }
  1.1194 +            if (this.options.submitOnBlur) {
  1.1195 +                textField.onblur = MochiKit.Base.bind(this.onSubmit, this);
  1.1196 +            }
  1.1197 +            this.editField = textField;
  1.1198 +        } else {
  1.1199 +            this.options.textarea = true;
  1.1200 +            var textArea = document.createElement('textarea');
  1.1201 +            textArea.obj = this;
  1.1202 +            textArea.name = 'value';
  1.1203 +            textArea.value = this.convertHTMLLineBreaks(text);
  1.1204 +            textArea.rows = this.options.rows;
  1.1205 +            textArea.cols = this.options.cols || 40;
  1.1206 +            if (this.options.submitOnBlur) {
  1.1207 +                textArea.onblur = MochiKit.Base.bind(this.onSubmit, this);
  1.1208 +            }
  1.1209 +            this.editField = textArea;
  1.1210 +        }
  1.1211 +
  1.1212 +        if (this.options.loadTextURL) {
  1.1213 +            this.loadExternalText();
  1.1214 +        }
  1.1215 +        this.form.appendChild(this.editField);
  1.1216 +    },
  1.1217 +
  1.1218 +/** @id Ajax.InPlaceEditor.prototype.getText */
  1.1219 +    getText: function () {
  1.1220 +        return this.element.innerHTML;
  1.1221 +    },
  1.1222 +
  1.1223 +/** @id Ajax.InPlaceEditor.prototype.loadExternalText */
  1.1224 +    loadExternalText: function () {
  1.1225 +        MochiKit.DOM.addElementClass(this.form, this.options.loadingClassName);
  1.1226 +        this.editField.disabled = true;
  1.1227 +        new Ajax.Request(
  1.1228 +            this.options.loadTextURL,
  1.1229 +            MochiKit.Base.update({
  1.1230 +                asynchronous: true,
  1.1231 +                onComplete: MochiKit.Base.bind(this.onLoadedExternalText, this)
  1.1232 +            }, this.options.ajaxOptions)
  1.1233 +        );
  1.1234 +    },
  1.1235 +
  1.1236 +/** @id Ajax.InPlaceEditor.prototype.onLoadedExternalText */
  1.1237 +    onLoadedExternalText: function (transport) {
  1.1238 +        MochiKit.DOM.removeElementClass(this.form, this.options.loadingClassName);
  1.1239 +        this.editField.disabled = false;
  1.1240 +        this.editField.value = MochiKit.Base.stripTags(transport);
  1.1241 +    },
  1.1242 +
  1.1243 +/** @id Ajax.InPlaceEditor.prototype.onclickCancel */
  1.1244 +    onclickCancel: function () {
  1.1245 +        this.onComplete();
  1.1246 +        this.leaveEditMode();
  1.1247 +        return false;
  1.1248 +    },
  1.1249 +
  1.1250 +/** @id Ajax.InPlaceEditor.prototype.onFailure */
  1.1251 +    onFailure: function (transport) {
  1.1252 +        this.options.onFailure(transport);
  1.1253 +        if (this.oldInnerHTML) {
  1.1254 +            this.element.innerHTML = this.oldInnerHTML;
  1.1255 +            this.oldInnerHTML = null;
  1.1256 +        }
  1.1257 +        return false;
  1.1258 +    },
  1.1259 +
  1.1260 +/** @id Ajax.InPlaceEditor.prototype.onSubmit */
  1.1261 +    onSubmit: function () {
  1.1262 +        // onLoading resets these so we need to save them away for the Ajax call
  1.1263 +        var form = this.form;
  1.1264 +        var value = this.editField.value;
  1.1265 +
  1.1266 +        // do this first, sometimes the ajax call returns before we get a
  1.1267 +        // chance to switch on Saving which means this will actually switch on
  1.1268 +        // Saving *after* we have left edit mode causing Saving to be
  1.1269 +        // displayed indefinitely
  1.1270 +        this.onLoading();
  1.1271 +
  1.1272 +        new Ajax.Updater(
  1.1273 +            {
  1.1274 +                success: this.element,
  1.1275 +                 // dont update on failure (this could be an option)
  1.1276 +                failure: null
  1.1277 +            },
  1.1278 +            this.url,
  1.1279 +            MochiKit.Base.update({
  1.1280 +                parameters: this.options.callback(form, value),
  1.1281 +                onComplete: MochiKit.Base.bind(this.onComplete, this),
  1.1282 +                onFailure: MochiKit.Base.bind(this.onFailure, this)
  1.1283 +            }, this.options.ajaxOptions)
  1.1284 +        );
  1.1285 +        // stop the event to avoid a page refresh in Safari
  1.1286 +        if (arguments.length > 1) {
  1.1287 +            arguments[0].stop();
  1.1288 +        }
  1.1289 +        return false;
  1.1290 +    },
  1.1291 +
  1.1292 +/** @id Ajax.InPlaceEditor.prototype.onLoading */
  1.1293 +    onLoading: function () {
  1.1294 +        this.saving = true;
  1.1295 +        this.removeForm();
  1.1296 +        this.leaveHover();
  1.1297 +        this.showSaving();
  1.1298 +    },
  1.1299 +
  1.1300 +/** @id Ajax.InPlaceEditor.prototype.onSaving */
  1.1301 +    showSaving: function () {
  1.1302 +        this.oldInnerHTML = this.element.innerHTML;
  1.1303 +        this.element.innerHTML = this.options.savingText;
  1.1304 +        MochiKit.DOM.addElementClass(this.element, this.options.savingClassName);
  1.1305 +        this.element.style.backgroundColor = this.originalBackground;
  1.1306 +        MochiKit.Style.showElement(this.element);
  1.1307 +    },
  1.1308 +
  1.1309 +/** @id Ajax.InPlaceEditor.prototype.removeForm */
  1.1310 +    removeForm: function () {
  1.1311 +        if (this.form) {
  1.1312 +            if (this.form.parentNode) {
  1.1313 +                MochiKit.DOM.removeElement(this.form);
  1.1314 +            }
  1.1315 +            this.form = null;
  1.1316 +        }
  1.1317 +    },
  1.1318 +
  1.1319 +/** @id Ajax.InPlaceEditor.prototype.enterHover */
  1.1320 +    enterHover: function () {
  1.1321 +        if (this.saving) {
  1.1322 +            return;
  1.1323 +        }
  1.1324 +        this.element.style.backgroundColor = this.options.highlightcolor;
  1.1325 +        if (this.effect) {
  1.1326 +            this.effect.cancel();
  1.1327 +        }
  1.1328 +        MochiKit.DOM.addElementClass(this.element, this.options.hoverClassName)
  1.1329 +    },
  1.1330 +
  1.1331 +/** @id Ajax.InPlaceEditor.prototype.leaveHover */
  1.1332 +    leaveHover: function () {
  1.1333 +        if (this.options.backgroundColor) {
  1.1334 +            this.element.style.backgroundColor = this.oldBackground;
  1.1335 +        }
  1.1336 +        MochiKit.DOM.removeElementClass(this.element, this.options.hoverClassName)
  1.1337 +        if (this.saving) {
  1.1338 +            return;
  1.1339 +        }
  1.1340 +        this.effect = new MochiKit.Visual.Highlight(this.element, {
  1.1341 +            startcolor: this.options.highlightcolor,
  1.1342 +            endcolor: this.options.highlightendcolor,
  1.1343 +            restorecolor: this.originalBackground
  1.1344 +        });
  1.1345 +    },
  1.1346 +
  1.1347 +/** @id Ajax.InPlaceEditor.prototype.leaveEditMode */
  1.1348 +    leaveEditMode: function () {
  1.1349 +        MochiKit.DOM.removeElementClass(this.element, this.options.savingClassName);
  1.1350 +        this.removeForm();
  1.1351 +        this.leaveHover();
  1.1352 +        this.element.style.backgroundColor = this.originalBackground;
  1.1353 +        MochiKit.Style.showElement(this.element);
  1.1354 +        if (this.options.externalControl) {
  1.1355 +            MochiKit.Style.showElement(this.options.externalControl);
  1.1356 +        }
  1.1357 +        this.editing = false;
  1.1358 +        this.saving = false;
  1.1359 +        this.oldInnerHTML = null;
  1.1360 +        this.onLeaveEditMode();
  1.1361 +    },
  1.1362 +
  1.1363 +/** @id Ajax.InPlaceEditor.prototype.onComplete */
  1.1364 +    onComplete: function (transport) {
  1.1365 +        this.leaveEditMode();
  1.1366 +        MochiKit.Base.bind(this.options.onComplete, this)(transport, this.element);
  1.1367 +    },
  1.1368 +
  1.1369 +/** @id Ajax.InPlaceEditor.prototype.onEnterEditMode */
  1.1370 +    onEnterEditMode: function () {},
  1.1371 +
  1.1372 +/** @id Ajax.InPlaceEditor.prototype.onLeaveEditMode */
  1.1373 +    onLeaveEditMode: function () {},
  1.1374 +
  1.1375 + /** @id Ajax.InPlaceEditor.prototype.dispose */
  1.1376 +    dispose: function () {
  1.1377 +        if (this.oldInnerHTML) {
  1.1378 +            this.element.innerHTML = this.oldInnerHTML;
  1.1379 +        }
  1.1380 +        this.leaveEditMode();
  1.1381 +        MochiKit.Signal.disconnect(this.onclickListener);
  1.1382 +        MochiKit.Signal.disconnect(this.mouseoverListener);
  1.1383 +        MochiKit.Signal.disconnect(this.mouseoutListener);
  1.1384 +        if (this.options.externalControl) {
  1.1385 +            MochiKit.Signal.disconnect(this.onclickListenerExternal);
  1.1386 +            MochiKit.Signal.disconnect(this.mouseoverListenerExternal);
  1.1387 +            MochiKit.Signal.disconnect(this.mouseoutListenerExternal);
  1.1388 +        }
  1.1389 +    }
  1.1390 +};
  1.1391 +

mercurial