javascripts/right-src.js in right-rails-0.4.3 vs javascripts/right-src.js in right-rails-0.4.4

- old
+ new

@@ -10,11 +10,11 @@ * The framework description object * * Copyright (C) 2008-2009 Nikolay V. Nemshilov aka St. <nemshilov#gma-ilc-om> */ var RightJS = { - version: "1.5.2", + version: "1.5.3", modules: ["core", "form", "cookie", "xhr", "fx"] }; /** * this object will contain info about the current browser @@ -846,11 +846,11 @@ * Some of the functionality inspired by * - Prototype (http://prototypejs.org) Copyright (C) Sam Stephenson * The trim function taken from work of Steven Levithan * - http://blog.stevenlevithan.com/archives/faster-trim-javascript * - * Copyright (C) 2008-2009 Nikolay V. Nemshilov aka St. <nemshilov#gma-il> + * Copyright (C) 2008-2010 Nikolay V. Nemshilov aka St. <nemshilov#gma-il> */ $ext(String.prototype, { /** * checks if the string is an empty string * @@ -893,22 +893,19 @@ * @param mixed option. If it equals true the scrips will be executed, * if a function the scripts will be passed in it * @return String without scripts */ stripScripts: function(option) { - var scripts = ''; - var text = this.replace(/<script[^>]*>([\s\S]*?)<\/script>/img, function(match, source) { - scripts += source.trim() + "\n"; + var scripts = '', text = this.replace(/<script[^>]*>([\s\S]*?)<\/script>/img, function(match, source) { + scripts += source + "\n"; return ''; }); if (option === true) $eval(scripts); else if (isFunction(option)) option(scripts, text); - else if (isNumber(option)) - $eval.bind(scripts).delay(options); return text; }, /** @@ -1445,11 +1442,11 @@ * * Credits: * The naming principle is inspired by * - Prototype (http://prototypejs.org) Copyright (C) Sam Stephenson * - * Copyright (C) 2008-2009 Nikolay V. Nemshilov aka St. <nemshilov#gma-il> + * Copyright (C) 2008-2010 Nikolay V. Nemshilov aka St. <nemshilov#gma-il> */ var Observer = new Class({ include: Options, /** @@ -1461,20 +1458,20 @@ this.setOptions(options); Observer.createShortcuts(this, Class.findSet(this, 'events')); }, /** - * starts observing an event + * binds an event listener * * USAGE: - * observe(String event, Function callback[, arguments, ...]); - * observe(String event, String method_name[, arguments, ...]); - * observe(Object events_hash); + * on(String event, Function callback[, arguments, ...]); + * on(String event, String method_name[, arguments, ...]); + * on(Object events_hash); * * @return Observer self */ - observe: function() { + on: function() { var args = $A(arguments), event = args.shift(); if (isString(event)) { if (!defined(this.$listeners)) this.$listeners = []; @@ -1527,11 +1524,11 @@ * USAGE: * observes(String event) * observes(Function callback) * observes(String event, Function callback) * - * @retun Observer self + * @retun boolean check result */ observes: function(event, callback) { if (this.$listeners) { if (!isString(event)) { callback = event; event = null; } if (isString(callback)) callback = this[callback]; @@ -1622,26 +1619,24 @@ * @param Array list of event names * @return Object extended object */ createShortcuts: function(object, names) { (names || []).each(function(name) { - var shortcuts = {}, method_name = name.replace(/:/g, '_').camelize(); - shortcuts[method_name] = function() { - return this.fire.apply(this, [name].concat($A(arguments))); - }; - shortcuts['on'+method_name.capitalize()] = function() { - return this.on.apply(this, [name].concat($A(arguments))); - }; - $ext(object, shortcuts, true); + var method_name = 'on'+name.replace(/:/g, '_').camelize().capitalize(); + if (!defined(object[method_name])) { + object[method_name] = function() { + return this.on.apply(this, [name].concat($A(arguments))); + }; + } }); return object; } } }); -$alias(Observer.prototype, { on: 'observe' }); +$alias(Observer.prototype, { observe: 'on' }); /** * iterators in-callbacks break exception * * Copyright (C) 2009 Nikolay V. Nemshilov aka St. <nemshilov#gma-il> @@ -1657,47 +1652,42 @@ * * Credits: * The additional method names are inspired by * - Prototype (http://prototypejs.org) Copyright (C) Sam Stephenson * - * Copyright (C) 2008-2009 Nikolay V. Nemshilov aka St. <nemshilov#gma-ilc-om> + * Copyright (C) 2008-2010 Nikolay V. Nemshilov aka St. <nemshilov#gma-ilc-om> */ var Event = new Class(Event, { extend: { /** * extends a native object with additional functionality * * @param Event event + * @param Element bounding element * @return Event same event but extended */ - ext: function(event) { + ext: function(event, bound_element) { if (!event.stop) { $ext(event, this.Methods, true); if (Browser.IE) { // faking the which button - if (event.type == 'click' || event.type == 'dblclick') { - event.which = 1; - } else if (event.type == 'contextmenu') { - event.which = 3; - } else { - event.which = event.button == 2 ? 3 : event.button == 4 ? 2 : 1; - } + event.which = event.button == 2 ? 3 : event.button == 4 ? 2 : 1; // faking the mouse position var scrolls = window.scrolls(); event.pageX = event.clientX + scrolls.x; event.pageY = event.clientY + scrolls.y; - - - // faking the relatedTarget - event.relatedTarget = event.type == 'mouseover' ? event.fromEvent : - event.type == 'mouseout' ? event.toEvent : null; - + // faking the target property - event.target = event.srcElement; + event.target = event.srcElement || bound_element; + + // faking the relatedTarget, currentTarget and other targets + event.relatedTarget = event[(event.target == event.fromElement ? 'to' : 'from') + 'Element']; + event.currentTarget = bound_element; + event.eventPhase = 3; // bubbling phase } } // Safari bug fix if (event.target && event.target.nodeType == 3) @@ -1729,24 +1719,10 @@ if (Browser.Gecko && name === 'mousewheel') name = 'DOMMouseScroll'; if (Browser.Konqueror && name === 'contextmenu') name = 'rightclick'; return name; }, - /** - * Registers some additional event extendsions - * - * @param Object methods - * @return void - */ - addMethods: function(methods) { - $ext(this.Methods, methods); - - try { // extending the events prototype - $ext(Event.parent.prototype, methods, true); - } catch(e) {}; - }, - // the additional methods registry Methods: {} }, /** @@ -1759,12 +1735,27 @@ initialize: function(name, options) { return new Event.Custom(Event.cleanName(name), options); } }); + +/** + * Registers some additional event extendsions + * + * @param Object methods + * @return void + */ +Event.addMethods = Event.include = function(methods) { + $ext(this.Methods, methods); + + try { // extending the events prototype + $ext(Event.parent.prototype, methods, true); + } catch(e) {}; +}; + // hooking up the standard extendsions -Event.addMethods({ +Event.include({ stopPropagation: function() { this.cancelBubble = true; }, preventDefault: function() { @@ -1797,11 +1788,11 @@ }; /** * The DOM Element unit handling * - * Copyright (C) 2008-2009 Nikolay V. Nemshilov aka St. <nemshilov#gma-ilc-om> + * Copyright (C) 2008-2010 Nikolay V. Nemshilov aka St. <nemshilov#gma-ilc-om> */ self.Element = (function(old_Element) { var new_Element = function(tag, options) { var element = document.createElement(tag), options = options || {}; @@ -1843,22 +1834,22 @@ /** * registeres the methods on the custom element methods list * will add them to prototype and will generate a non extensive static mirror * * USAGE: - * Element.addMethods({ + * Element.include({ * foo: function(bar) {} * }); * * $(element).foo(bar); * Element.foo(element, bar); * * @param Object new methods list * @param Boolean flag if the method should keep the existing methods alive * @return Element the global Element object */ - addMethods: function(methods, dont_overwrite) { + include: function(methods, dont_overwrite) { $ext(this.Methods, methods, dont_overwrite); try { // busting up the basic element prototypes $ext(HTMLElement.prototype, methods, dont_overwrite); } catch(e) { @@ -1868,13 +1859,16 @@ } return this; }, - Methods: {} // DO NOT Extend this object manually unless you really need it, use Element#addMethods + Methods: {} // DO NOT Extend this object manually unless you really need it, use Element#include }); +// the old interface alias, NOTE will be nuked +Element.addMethods = Element.include; + /** * The DOM Element unit structures handling module * * NOTE: all the methods will process and return only the Element nodes * all the textual nodes will be skipped @@ -1888,24 +1882,24 @@ * The naming principle and most of the names are taken from * - Prototype (http://prototypejs.org) Copyright (C) Sam Stephenson * The insertions system implementation is inspired by * - MooTools (http://mootools.net) Copyright (C) Valerio Proietti * - * Copyright (C) 2008 Nikolay V. Nemshilov aka St. <nemshilov#gma-ilc-om> + * Copyright (C) 2008-2010 Nikolay V. Nemshilov aka St. <nemshilov#gma-ilc-om> */ -Element.addMethods({ +Element.include({ parent: function(css_rule) { return css_rule ? this.parents(css_rule)[0] : $(this.parentNode); }, parents: function(css_rule) { return this.rCollect('parentNode', css_rule); }, subNodes: function(css_rule) { - var first_child = this.firstChild; - return first_child ? (first_child.tagName ? [$(first_child)] : [] + var first_child = $(this.firstChild); + return first_child ? (first_child.tagName && (!css_rule || first_child.match(css_rule)) ? [first_child] : [] ).concat(this.rCollect.call(first_child, 'nextSibling', css_rule)) : []; }, siblings: function(css_rule) { return this.prevSiblings(css_rule).reverse().concat(this.nextSiblings(css_rule)); @@ -1960,12 +1954,12 @@ } } else { var scripts, insertions = Element.insertions; position = isString(position) ? position.toLowerCase() : 'bottom'; - if (isString(content)) { - content = content.stripScripts(function(s) { scripts = s; }); + if (isString(content) || isNumber(content)) { + content = (''+content).stripScripts(function(s) { scripts = s; }); } insertions[position](this, content.tagName ? content : insertions.createFragment.call( (position === 'bottom' || position === 'top' || !this.parentNode) ? @@ -2004,13 +1998,13 @@ * * @param mixed content (a String, an Element or a list of elements) * @return Element self */ update: function(content) { - if (isString(content)) { + if (isString(content) || isNumber(content)) { var scripts; - this.innerHTML = content.stripScripts(function(s) { scripts = s; }); + this.innerHTML = (''+content).stripScripts(function(s) { scripts = s; }); if (scripts) $eval(scripts); } else { this.clean().insert(content); } return this; @@ -2159,13 +2153,13 @@ * Some of the functionality is inspired by * - Prototype (http://prototypejs.org) Copyright (C) Sam Stephenson * - MooTools (http://mootools.net) Copyright (C) Valerio Proietti * - Dojo (www.dojotoolkit.org) Copyright (C) The Dojo Foundation * - * Copyright (C) 2008-2009 Nikolay V. Nemshilov aka St. <nemshilov#gma-ilc-om> + * Copyright (C) 2008-2010 Nikolay V. Nemshilov aka St. <nemshilov#gma-ilc-om> */ -Element.addMethods({ +Element.include({ /** * assigns styles out of the hash to the element * * NOTE: the style keys might be camelized or dasherized, both cases should work * @@ -2327,22 +2321,22 @@ * * Credits: * Most of the naming system in the module inspired by * - Prototype (http://prototypejs.org) Copyright (C) Sam Stephenson * - * Copyright (C) 2008 Nikolay V. Nemshilov aka St. <nemshilov#gma-il> + * Copyright (C) 2008-2010 Nikolay V. Nemshilov aka St. <nemshilov#gma-il> */ -Element.addMethods({ +Element.include({ /** * sets the element attributes * * @param String attr name or Object attributes hash * @param mixed attribute value * @return Element self */ set: function(hash, value) { - if (value) { var val = {}; val[hash] = value; hash = val; } + if (typeof(hash) === 'string') { var val = {}; val[hash] = value; hash = val; } for (var key in hash) { // some attributes are not available as properties if (this[key] === undefined) { this.setAttribute(key, ''+hash[key]); @@ -2424,13 +2418,15 @@ * @param String optional effect name * @param Object the optional effect options * @return Element self */ show: function(effect, options) { - // setting 'block' for the divs and 'inline' for the other elements hidden on the css-level - var value = this.tagName == 'DIV' ? 'block' : 'inline'; - this.style.display = this._$pd == 'none' ? value : this._$pd || value; + if (this.getStyle('display') == 'none') { + // setting 'block' for the divs and 'inline' for the other elements hidden on the css-level + var value = this.tagName == 'DIV' ? 'block' : 'inline'; + this.style.display = this._$pd == 'none' ? value : this._$pd || value; + } return this; }, /** * toggles the visibility state of the element @@ -2458,13 +2454,13 @@ /** * this module contains the Element's part of functionality * responsible for the dimensions and positions getting/setting * - * Copyright (C) 2008-2009 Nikolay V. Nemshilov aka St. <nemshilov#gma-il> + * Copyright (C) 2008-2010 Nikolay V. Nemshilov aka St. <nemshilov#gma-il> */ -Element.addMethods({ +Element.include({ /** * Returns the element sizes as a hash * * @return Object {x: NNN, y: NNN} */ @@ -2617,13 +2613,13 @@ }); /** * DOM Element events handling methods * - * Copyright (C) 2008-2009 Nikolay V. Nemshilov aka St. <nemshilov#gma-il> + * Copyright (C) 2008-2010 Nikolay V. Nemshilov aka St. <nemshilov#gma-il> */ -Element.addMethods((function() { +Element.include((function() { var observer = Observer.create({}, $w('click rightclick contextmenu mousedown mouseup mouseover mouseout mousemove keypress keydown keyup') ); // @@ -2634,11 +2630,11 @@ // observer.observe = observer.on = eval('({f:'+ observer.observe.toString().replace(/(\$listeners\.push\((\w+?)\);)/, '$1'+ '$2.e=Event.cleanName($2.e);$2.n=Event.realName($2.e);'+ - '$2.w=function(){var a=$A(arguments),e=($2.r&&$2.r!=="stopEvent")?a.shift():Event.ext(a[0]);'+ + '$2.w=function(){var a=$A(arguments),e=($2.r&&$2.r!=="stopEvent")?a.shift():Event.ext(a[0],this);'+ 'return $2.f.apply(this,a.concat($2.a))};'+( self.attachEvent ? '$2.w=$2.w.bind(this);this.attachEvent("on"+$2.n,$2.w);' : 'this.addEventListener($2.n,$2.w,false);' @@ -2667,23 +2663,25 @@ observer.stopEvent = function(e) { e.stop(); }; $ext(window, observer); $ext(document, observer); + Observer.createShortcuts(window, $w('blur focus scroll')); + return observer; })()); /** * The DOM elements selection handling * * NOTE: this module is just a wrap over the native CSS-selectors feature * see the olds/css.js file for the manual selector code * - * Copyright (C) 2008-2009 Nikolay V. Nemshilov aka St. <nemshilov#gma-ilc-om> + * Copyright (C) 2008-2010 Nikolay V. Nemshilov aka St. <nemshilov#gma-ilc-om> */ -Element.addMethods((function() { +Element.include((function() { /** * Native css-selectors include the current element into the search context * and as we actually search only inside of the element we add it's tag * as a scope for the search */ @@ -2813,11 +2811,11 @@ * * Copyright (C) 2009 Nikolay V. Nemshilov aka St. <nemshilov#gma-ilc-om> */ [window, document].each(function(object) { Observer.createShortcuts(object, ['ready']); - var ready = object.ready.bind(object); + var ready = object.fire.bind(object, 'ready'); // IE and Konqueror browsers if (document.readyState !== undefined) { (function() { ['loaded','complete'].includes(document.readyState) ? ready() : arguments.callee.delay(50); @@ -2833,11 +2831,11 @@ * * Credits: * The basic principles of the module are inspired by * - Prototype (http://prototypejs.org) Copyright (C) Sam Stephenson * - * Copyright (C) 2009 Nikolay V. Nemshilov aka St. <nemshilov#gma-ilc-om> + * Copyright (C) 2009-2010 Nikolay V. Nemshilov aka St. <nemshilov#gma-ilc-om> */ var Form = function(options) { var options = options || {}, remote = options.remote, form = new Element('form', Object.without(options, 'remote')); @@ -2863,20 +2861,21 @@ * Extends the form functionality * * @param Object methods hash * @return void */ - addMethods: function(methods, dont_overwrite) { + include: function(methods, dont_overwrite) { $ext(Form.Methods, methods, dont_overwrite); try { // trying to extend the form element prototype $ext(HTMLFormElement.prototype, methods, dont_overwrite); } catch(e) {} } }); -Form.addMethods({ +Form.addMethods = Form.include; +Form.include({ /** * returns the form elements as an array of extended units * * @return Array of elements */ @@ -2961,22 +2960,22 @@ return Object.toQueryString(this.values()); } }); // creating the shortcuts -Form.addMethods(Observer.createShortcuts({}, $w('submit reset focus')), true); +Form.include(Observer.createShortcuts({}, $w('submit reset focus')), true); /** * there is the form-elements additional methods container * * Credits: * The basic ideas are taken from * - Prototype (http://prototypejs.org) Copyright (C) Sam Stephenson * - * Copyright (C) 2009 Nikolay V. Nemshilov aka St. <nemshilov#gma-ilc-om> + * Copyright (C) 2009-2010 Nikolay V. Nemshilov aka St. <nemshilov#gma-ilc-om> */ (function() { // trying to get the input element classes list try { var input_classes = [HTMLInputElement, HTMLSelectElement, HTMLTextAreaElement, HTMLButtonElement]; } catch(e) { var input_classes = []; } @@ -3004,11 +3003,11 @@ * Extends the Form.Element methods * * @param Object methods list * @return void */ - addMethods: function(methods, dont_overwrite) { + include: function(methods, dont_overwrite) { $ext(this.Methods, methods, dont_overwrite); // extending the input element prototypes input_classes.each(function(klass) { $ext(klass.prototype, methods, dont_overwrite); @@ -3023,12 +3022,12 @@ _focus: 'focus', _select: 'select' }); }); })(); - -Form.Element.addMethods({ +Form.Element.addMethods = Form.Element.include; +Form.Element.include({ /** * uniform access to the element values * * @return String element value */ @@ -3117,11 +3116,11 @@ return this; } }); // creating the common event shortcuts -Form.Element.addMethods(Observer.createShortcuts({}, $w('disable enable focus blur change')), true); +Form.Element.include(Observer.createShortcuts({}, $w('disable enable focus blur change')), true); /** * this module handles the work with cookies * @@ -3443,19 +3442,24 @@ } }, // sanitizes the json-response texts sanitizedJSON: function() { - // checking the JSON response formatting - if (!(/^[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]*$/).test(this.text.replace(/\\./g, '@').replace(/"[^"\\\n\r]*"/g, ''))) { - if (this.secureJSON) { - throw "JSON parse error: "+this.text; - } else { - return null; + try { + return JSON.parse(this.text); + } catch(e) { + // manual json consistancy check + if (self.JSON || !(/^[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]*$/).test(this.text.replace(/\\./g, '@').replace(/"[^"\\\n\r]*"/g, ''))) { + if (this.secureJSON) { + throw "JSON parse error: "+this.text; + } else { + return null; + } } } + // the fallback JSON extraction return eval("("+this.text+")"); }, // initializes the request callbacks initCallbacks: function() { @@ -3518,13 +3522,13 @@ * Credits: * Some of the functionality inspired by * - Prototype (http://prototypejs.org) Copyright (C) Sam Stephenson * - jQuery (http://jquery.com) Copyright (C) John Resig * - * Copyright (C) 2009 Nikolay V. Nemshilov aka St. <nemshilov#gma-ilc-om> + * Copyright (C) 2009-2010 Nikolay V. Nemshilov aka St. <nemshilov#gma-ilc-om> */ -Form.addMethods({ +Form.include({ /** * sends the form via xhr request * * @params Options xhr request options * @return Form this @@ -3574,13 +3578,13 @@ * this module contains the Element unit XHR related extensions * * Credits: * - jQuery (http://jquery.com) Copyright (C) John Resig * - * Copyright (C) 2008-2009 Nikolay V. Nemshilov aka St. <nemshilov#gma-il> + * Copyright (C) 2008-2010 Nikolay V. Nemshilov aka St. <nemshilov#gma-il> */ -Element.addMethods({ +Element.include({ /** * performs an Xhr request to the given url * and updates the element internals with the responseText * * @param String url address @@ -4311,12 +4315,12 @@ * * Credits: * Some ideas are inspired by * - MooTools (http://mootools.net) Copyright (C) Valerio Proietti * - * Copyright (C) 2008-2009 Nikolay V. Nemshilov aka St. <nemshilov#gma-ilc-om> + * Copyright (C) 2008-2010 Nikolay V. Nemshilov aka St. <nemshilov#gma-ilc-om> */ -Element.addMethods((function(methods) { +Element.include((function(methods) { var old_hide = methods.hide, old_show = methods.show, old_scroll = methods.scrollTo; return { \ No newline at end of file