/admin/thirdparty/history-js/vendor/right.js
JavaScript | 2666 lines | 1210 code | 330 blank | 1126 comment | 227 complexity | b736a81be95e4c0389dca3bf2bc54edd MD5 | raw file
Possible License(s): BSD-3-Clause, MIT, CC-BY-3.0, GPL-2.0, AGPL-1.0, LGPL-2.1
- /**
- * RightJS v2.2.3 - http://rightjs.org
- * Released under the terms of MIT license
- *
- * Copyright (C) 2008-2011 Nikolay Nemshilov
- */
- /**
- * The basic layout for RightJS builds
- *
- * Copyright (C) 2008-2011 Nikolay Nemshilov
- */
- var RightJS = (function(window, document, Object, Array, String, Function, Number, Math, undefined) {
- /**
- * The framework description object
- *
- * Copyright (C) 2008-2011 Nikolay Nemshilov
- */
- var RightJS = function(value) {
- return value; // <- a dummy method to emulate the safe-mode
- };
- RightJS.version = "2.2.3";
- RightJS.modules =["core", "dom", "form", "events", "xhr", "fx", "cookie"];
- /**
- * There are some util methods
- *
- * Credits:
- * Some of the functionality and names are inspired or copied from
- * - Prototype (http://prototypejs.org) Copyright (C) Sam Stephenson
- * - MooTools (http://mootools.net) Copyright (C) Valerio Proietti
- *
- * Copyright (C) 2008-2011 Nikolay V. Nemshilov
- */
- /**
- * Some top-level variables to shortify the things
- */
- var A_proto = Array.prototype,
- to_s = Object.prototype.toString, slice = A_proto.slice,
- HTML = document.documentElement, UID = 1, // !#server
- Wrappers_Cache = [], UID_KEY = 'uniqueNumber', // DON'T change the UID_KEY!
- /**
- * extends the first object with the keys and values of the second one
- *
- * NOTE: the third optional argument tells if the existing values
- * of the first object should _NOT_ get updated by the values of the second object
- *
- * @param oritinal Object destintation object
- * @param source Object source object
- * @param Boolean flag if the function should not overwrite intersecting values
- * @return Object extended destination object
- */
- $ext = RightJS.$ext = function(dest, source, dont_overwrite) {
- var src = source || {}, key;
- for (key in src) {
- if (!dont_overwrite || !(key in dest)) {
- dest[key] = src[key];
- }
- }
- return dest;
- },
- /** !#server
- * evals the given javascript text in the context of the current window
- *
- * @param String javascript
- * @return void
- */
- $eval = RightJS.$eval = function(text) {
- if (text) {
- if ('execScript' in window) {
- current_Document.win()._.execScript(text);
- } else {
- $E('script', {text: text}).insertTo(HTML);
- }
- }
- },
- /**
- * throws an exception to break iterations throw a callback
- *
- * @return void
- * @throws Break
- */
- $break = RightJS.$break = function() {
- throw new Break();
- },
- /**
- * generates aliases for the object properties
- *
- * @param object Object object
- * @param names Object aliases hash
- * @return Object the extended objects
- */
- $alias = RightJS.$alias = function(object, names) {
- for (var new_name in names) {
- object[new_name] = object[names[new_name]];
- }
- return object;
- },
- /**
- * checks if the given value or a reference points
- * to a really defined value
- *
- * NOTE: will return true for variables equal to null, false, 0, and so one.
- *
- * EXAMPLE:
- *
- * var smth = null;
- * defined(smth); <- will return true
- *
- * var obj = {};
- * defined(obj['smth']); <- will return false
- *
- * @param mixed value
- * @return boolean check result
- */
- defined = RightJS.defined = function(value) {
- return typeof(value) !== 'undefined';
- },
- /**
- * checks if the given value is a function
- *
- * @param mixed value
- * @return boolean check result
- */
- isFunction = RightJS.isFunction = function(value) {
- return typeof(value) === 'function';
- },
- /**
- * checks if the given value is a string
- *
- * @param mixed value
- * @return boolean check result
- */
- isString = RightJS.isString = function(value) {
- return typeof(value) === 'string';
- },
- /**
- * checks if the given value is a number
- *
- * @param mixed value to check
- * @return boolean check result
- */
- isNumber = RightJS.isNumber = function(value) {
- return typeof(value) === 'number';
- },
- /**
- * checks if the given value is a hash-like object
- *
- * @param mixed value
- * @return boolean check result
- */
- isHash = RightJS.isHash = function(value) {
- return to_s.call(value) === '[object Object]';
- },
- /**
- * checks if the given value is an array
- *
- * @param mixed value to check
- * @return boolean check result
- */
- isArray = RightJS.isArray = function(value) {
- return to_s.call(value) === '[object Array]';
- },
- /** !#server
- * checks if the given value is an element
- *
- * @param mixed value to check
- * @return boolean check result
- */
- isElement = RightJS.isElement = function(value) {
- return value != null && value.nodeType === 1;
- },
- /** !#server
- * checks if the given value is a DOM-node
- *
- * @param mixed value to check
- * @return boolean check result
- */
- isNode = RightJS.isNode = function(value) {
- return value != null && value.nodeType != null;
- },
- /** !#server
- * searches an element by id and/or extends it with the framework extentions
- *
- * @param String element id or Element to extend
- * @return Element or null
- */
- $ = RightJS.$ = function(object) {
- if (object instanceof Wrapper) {
- return object;
- } else if (typeof object === 'string') {
- object = document.getElementById(object);
- }
- return wrap(object);
- },
- /** !#server
- * Finds all the elements in the document by the given css_rule
- *
- * @param String element
- * @param Boolean raw search marker
- * @return Array search result
- */
- $$ = RightJS.$$ = function(css_rule, raw) {
- return current_Document.find(css_rule, raw);
- },
- /** !#server
- * shortcut to instance new elements
- *
- * @param String tag name
- * @param object options
- * @return Element instance
- */
- $E = RightJS.$E = function(tag_name, options) {
- return new Element(tag_name, options);
- },
- /**
- * shortcut, generates an array of words from a given string
- *
- * @param String string
- * @return Array of words
- */
- $w = RightJS.$w = function(string) {
- return string.trim().split(/\s+/);
- },
- /**
- * generates an unique id for an object
- *
- * @param Object object
- * @return Integer uniq id
- */
- $uid = RightJS.$uid = function(item) {
- return UID_KEY in item ? item[UID_KEY] : (item[UID_KEY] = UID++);
- },
- /**
- * converts any iterables into an array
- *
- * @param Object iterable
- * @return Array list
- */
- $A = RightJS.$A = function(it) {
- return slice.call(it, 0);
- };
- /** !#server
- * IE needs a patch for the $A function
- * because it doesn't handle all the cases
- */
- if (!A_proto.map) {
- $A = RightJS.$A = function(it) {
- try {
- return slice.call(it, 0);
- } catch(e) {
- for (var a=[], i=0, length = it.length; i < length; i++) {
- a[i] = it[i];
- }
- return a;
- }
- };
- }
- /** !#server
- * Internet Explorer needs some additional mumbo-jumbo in here
- */
- if (isHash(HTML)) {
- isHash = RightJS.isHash = function(value) {
- return to_s.call(value) === '[object Object]' &&
- value != null && value.hasOwnProperty != null;
- };
- }
- /**
- * Generating methods for native units extending
- */
- // adds a standard '.include' method to the native unit
- function extend_native(klass) {
- return $ext(klass, {
- Methods: {},
- include: function() {
- for (var i=0, l = arguments.length; i < l; i++) {
- if (isHash(arguments[i])) {
- $ext(klass.prototype, arguments[i]);
- $ext(klass.Methods, arguments[i]);
- }
- }
- }
- });
- }
- for (var i=0, natives = 'Array Function Number String Date RegExp'.split(' '); i < natives.length; i++) {
- RightJS[natives[i]] = extend_native(new Function('return '+ natives[i])());
- }
- // referring those two as well
- RightJS.Object = Object;
- RightJS.Math = Math;
- /**
- * Checks if the data is an array and if not,
- * then makes an array out of it
- *
- * @param mixed in data
- * @return Array data
- */
- function ensure_array(data) {
- return isArray(data) ? data : [data];
- }
- /**
- * The Object class extentions
- *
- * Credits:
- * Some functionality is inspired by
- * - Prototype (http://prototypejs.org) Copyright (C) Sam Stephenson
- *
- * Copyright (C) 2008-2011 Nikolay V. Nemshilov
- */
- $ext(Object, {
- /**
- * extracts the list of the attribute names of the given object
- *
- * @param Object object
- * @return Array keys list
- */
- keys: function(object) {
- var keys = [], key;
- for (key in object) {
- keys.push(key);
- }
- return keys;
- },
- /**
- * extracts the list of the attribute values of the given object
- *
- * @param Object object
- * @return Array values list
- */
- values: function(object) {
- var values = [], key;
- for (key in object) {
- values.push(object[key]);
- }
- return values;
- },
- /**
- * Calls the function with every key/value pair on the hash
- *
- * @param in Object the data hash
- * @param Function the callback
- * @param scope Object an optional scope
- * @return Object the original hash
- */
- each: function(object, callback, scope) {
- for (var key in object) {
- callback.call(scope, key, object[key]);
- }
- return object;
- },
- /**
- * checks if the object-hash has no keys
- *
- * @param Object object
- * @return check result
- */
- empty: function(object) {
- for (var key in object) { return false; }
- return true;
- },
- /**
- * A simple cloning method
- * NOTE: does not clone the things recoursively!
- *
- * @param Object object
- * @return Object clone
- */
- clone: function(object) {
- return Object.merge(object);
- },
- /**
- * returns a copy of the object which contains
- * all the same keys/values except the key-names
- * passed the the method arguments
- *
- * @param Object object
- * @param String key-name to exclude
- * .....
- * @return Object filtered copy
- */
- without: function() {
- var filter = $A(arguments), object = filter.shift(), copy = {}, key;
- for (key in object) {
- if (!filter.include(key)) {
- copy[key] = object[key];
- }
- }
- return copy;
- },
- /**
- * returns a copy of the object which contains all the
- * key/value pairs from the specified key-names list
- *
- * NOTE: if some key does not exists in the original object, it will be just skipped
- *
- * @param Object object
- * @param String key name to exclude
- * .....
- * @return Object filtered copy
- */
- only: function() {
- var filter = $A(arguments), object = filter.shift(), copy = {},
- i=0, length = filter.length;
- for (; i < length; i++) {
- if (filter[i] in object) {
- copy[filter[i]] = object[filter[i]];
- }
- }
- return copy;
- },
- /**
- * merges the given objects and returns the result
- *
- * NOTE this method _DO_NOT_ change the objects, it creates a new object
- * which conatins all the given ones.
- * if there is some keys introspections, the last object wins.
- * all non-object arguments will be omitted
- *
- * @param first Object object
- * @param second Object mixing
- * ......
- * @return Object merged object
- */
- merge: function() {
- var object = {}, i=0, args=arguments, key;
- for (l = args.length; i < l; i++) {
- if (isHash(args[i])) {
- for (key in args[i]) {
- object[key] = isHash(args[i][key]) && !(args[i][key] instanceof Class) ?
- Object.merge(key in object ? object[key] : {}, args[i][key]) : args[i][key];
- }
- }
- }
- return object;
- },
- /**
- * converts a hash-object into an equivalent url query string
- *
- * @param Object object
- * @return String query
- */
- toQueryString: function(object) {
- var tokens = [], key, value, encode = encodeURIComponent;
- for (key in object) {
- value = ensure_array(object[key]);
- for (var i=0, l = value.length; i < l; i++) {
- tokens.push(encode(key) +'='+ encode(value[i]));
- }
- }
- return tokens.join('&');
- }
- }, true);
- /**
- * here are the starndard Math object extends
- *
- * Credits:
- * The idea of random mehtod is taken from
- * - Ruby (http://www.ruby-lang.org) Copyright (C) Yukihiro Matsumoto
- *
- * Copyright (C) 2008-2010 Nikolay Nemshilov
- */
- var Math_old_random = Math.random;
- /**
- * the standard random method replacement, to make it more useful
- *
- * USE:
- * Math.random(); // original functionality, returns a float between 0 and 1
- * Math.random(10); // returns an integer between 0 and 10
- * Math.random(1,4); // returns an integer between 1 and 4
- *
- * @param min Integer minimum value if there's two arguments and maximum value if there's only one
- * @param max Integer maximum value
- * @return Float random between 0 and 1 if there's no arguments or an integer in the given range
- */
- Math.random = function(min, max) {
- if (arguments.length === 0) {
- return Math_old_random();
- } else if (arguments.length === 1) {
- max = min;
- min = 0;
- }
- return ~~(Math_old_random() * (max-min+1) + ~~min);
- };
- /**
- * The Array class extentions
- *
- * Credits:
- * Some of the functionality is inspired by
- * - Prototype (http://prototypejs.org) Copyright (C) Sam Stephenson
- * - Ruby (http://www.ruby-lang.org) Copyright (C) Yukihiro Matsumoto
- *
- * Copyright (C) 2008-2010 Nikolay Nemshilov
- */
- var original_sort = A_proto.sort,
- // JavaScript 1.6 methods recatching up or faking
- for_each = A_proto.forEach || function(callback, scope) {
- for (var i=0, l=this.length; i < l; i++) {
- callback.call(scope, this[i], i, this);
- }
- },
- filter = A_proto.filter || function(callback, scope) {
- for (var result=[], j=0, i=0, l=this.length; i < l; i++) {
- if (callback.call(scope, this[i], i, this)) {
- result[j++] = this[i];
- }
- }
- return result;
- },
- reject = function(callback, scope) {
- for (var result=[], j=0, i=0, l=this.length; i < l; i++) {
- if (!callback.call(scope, this[i], i, this)) {
- result[j++] = this[i];
- }
- }
- return result;
- },
- map = A_proto.map || function(callback, scope) {
- for (var result=[], i=0, l=this.length; i < l; i++) {
- result[i] = callback.call(scope, this[i], i, this);
- }
- return result;
- },
- some = A_proto.some || function(callback, scope) {
- for (var i=0, l=this.length; i < l; i++) {
- if (callback.call(scope, this[i], i, this)) {
- return true;
- }
- }
- return false;
- },
- every = A_proto.every || function(callback, scope) {
- for (var i=0, l=this.length; i < l; i++) {
- if (!callback.call(scope, this[i], i, this)) {
- return false;
- }
- }
- return true;
- },
- first = function(callback, scope) {
- for (var i=0, l=this.length; i < l; i++) {
- if (callback.call(scope, this[i], i, this)) {
- return this[i];
- }
- }
- return undefined;
- },
- last = function(callback, scope) {
- for (var i=this.length-1; i > -1; i--) {
- if (callback.call(scope, this[i], i, this)) {
- return this[i];
- }
- }
- return undefined;
- };
- //
- // RightJS callbacks magick preprocessing
- //
- // prepares a correct callback function
- function guess_callback(argsi, array) {
- var callback = argsi[0], args = slice.call(argsi, 1), scope = array, attr;
- if (typeof(callback) === 'string') {
- attr = callback;
- if (array.length !== 0 && typeof(array[0][attr]) === 'function') {
- callback = function(object) { return object[attr].apply(object, args); };
- } else {
- callback = function(object) { return object[attr]; };
- }
- } else {
- scope = args[0];
- }
- return [callback, scope];
- }
- // defining the manual break errors class
- function Break() {}
- // calls the given method with preprocessing the arguments
- function call_method(func, scope, args) {
- try {
- return func.apply(scope, guess_callback(args, scope));
- } catch(e) { if (!(e instanceof Break)) { throw(e); } }
- return undefined;
- }
- // checks the value as a boolean
- function boolean_check(i) {
- return !!i;
- }
- // default sorting callback
- function default_sort(a, b) {
- return a > b ? 1 : a < b ? -1 : 0;
- }
- Array.include({
- /**
- * IE fix
- * returns the index of the value in the array
- *
- * @param mixed value
- * @param Integer optional offset
- * @return Integer index or -1 if not found
- */
- indexOf: A_proto.indexOf || function(value, from) {
- for (var i=(from<0) ? Math.max(0, this.length+from) : from || 0, l=this.length; i < l; i++) {
- if (this[i] === value) {
- return i;
- }
- }
- return -1;
- },
- /**
- * IE fix
- * returns the last index of the value in the array
- *
- * @param mixed value
- * @return Integer index or -1 if not found
- */
- lastIndexOf: A_proto.lastIndexOf || function(value) {
- for (var i=this.length-1; i > -1; i--) {
- if (this[i] === value) {
- return i;
- }
- }
- return -1;
- },
- /**
- * returns the first element of the array
- *
- * @return mixed first element of the array
- */
- first: function() {
- return arguments.length ? call_method(first, this, arguments) : this[0];
- },
- /**
- * returns the last element of the array
- *
- * @return mixed last element of the array
- */
- last: function() {
- return arguments.length ? call_method(last, this, arguments) : this[this.length-1];
- },
- /**
- * returns a random item of the array
- *
- * @return mixed a random item
- */
- random: function() {
- return this.length === 0 ? undefined : this[Math.random(this.length-1)];
- },
- /**
- * returns the array size
- *
- * @return Integer the array size
- */
- size: function() {
- return this.length;
- },
- /**
- * cleans the array
- * @return Array this
- */
- clean: function() {
- this.length = 0;
- return this;
- },
- /**
- * checks if the array has no elements in it
- *
- * @return boolean check result
- */
- empty: function() {
- return this.length === 0;
- },
- /**
- * creates a copy of the given array
- *
- * @return Array copy of the array
- */
- clone: function() {
- return this.slice(0);
- },
- /**
- * calls the given callback function in the given scope for each element of the array
- *
- * @param Function callback
- * @param Object scope
- * @return Array this
- */
- each: function() {
- call_method(for_each, this, arguments);
- return this;
- },
- forEach: for_each,
- /**
- * creates a list of the array items converted in the given callback function
- *
- * @param Function callback
- * @param Object optional scope
- * @return Array collected
- */
- map: function() {
- return call_method(map, this, arguments);
- },
- /**
- * creates a list of the array items which are matched in the given callback function
- *
- * @param Function callback
- * @param Object optional scope
- * @return Array filtered copy
- */
- filter: function() {
- return call_method(filter, this, arguments);
- },
- /**
- * creates a list of the array items that are not matching the give callback function
- *
- * @param Function callback
- * @param Object optionl scope
- * @return Array filtered copy
- */
- reject: function() {
- return call_method(reject, this, arguments);
- },
- /**
- * checks if any of the array elements is logically true
- *
- * @param Function optional callback for checks
- * @param Object optional scope for the callback
- * @return boolean check result
- */
- some: function(value) {
- return call_method(some, this, value ? arguments : [boolean_check]);
- },
- /**
- * checks if all the array elements are logically true
- *
- * @param Function optional callback for checks
- * @param Object optional scope for the callback
- * @return Boolean check result
- */
- every: function(value) {
- return call_method(every, this, value ? arguments : [boolean_check]);
- },
- /**
- * applies the given lambda to each element in the array
- *
- * NOTE: changes the array by itself
- *
- * @param Function callback
- * @param Object optional scope
- * @return Array this
- */
- walk: function() {
- this.map.apply(this, arguments).forEach(function(value, i) { this[i] = value; }, this);
- return this;
- },
- /**
- * similar to the concat function but it adds only the values which are not on the list yet
- *
- * @param Array to merge
- * ....................
- * @return Array new merged
- */
- merge: function() {
- for (var copy = this.clone(), arg, i=0; i < arguments.length; i++) {
- arg = ensure_array(arguments[i]);
- for (var j=0; j < arg.length; j++) {
- if (copy.indexOf(arg[j]) == -1) {
- copy.push(arg[j]);
- }
- }
- }
- return copy;
- },
- /**
- * flats out complex array into a single dimension array
- *
- * @return Array flatten copy
- */
- flatten: function() {
- var copy = [];
- this.forEach(function(value) {
- if (isArray(value)) {
- copy = copy.concat(value.flatten());
- } else {
- copy.push(value);
- }
- });
- return copy;
- },
- /**
- * returns a copy of the array whithout any null or undefined values
- *
- * @return Array filtered version
- */
- compact: function() {
- return this.without(null, undefined);
- },
- /**
- * returns a copy of the array which contains only the unique values
- *
- * @return Array filtered copy
- */
- uniq: function() {
- return [].merge(this);
- },
- /**
- * checks if all of the given values
- * exists in the given array
- *
- * @param mixed value
- * ....
- * @return boolean check result
- */
- includes: function() {
- for (var i=0; i < arguments.length; i++) {
- if (this.indexOf(arguments[i]) === -1) {
- return false;
- }
- }
- return true;
- },
- /**
- * returns a copy of the array without the items passed as the arguments
- *
- * @param mixed value
- * ......
- * @return Array filtered copy
- */
- without: function() {
- var filter = slice.call(arguments);
- return this.filter(function(value) {
- return filter.indexOf(value) === -1;
- });
- },
- /**
- * Shuffles the array items in a random order
- *
- * @return Array shuffled version
- */
- shuffle: function() {
- var shuff = this.clone(), j, x, i = shuff.length;
- for (; i > 0; j = Math.random(i-1), x = shuff[--i], shuff[i] = shuff[j], shuff[j] = x) {}
- return shuff;
- },
- /**
- * Default sort fix for numeric values
- *
- * @param Function callback
- * @return Array self
- */
- sort: function(callback) {
- return original_sort.apply(this, (callback || !isNumber(this[0])) ? arguments : [default_sort]);
- },
- /**
- * sorts the array by running its items though a lambda or calling their attributes
- *
- * @param Function callback or attribute name
- * @param Object scope or attribute argument
- * @return Array sorted copy
- */
- sortBy: function() {
- var pair = guess_callback(arguments, this);
- return this.sort(function(a, b) {
- return default_sort(
- pair[0].call(pair[1], a),
- pair[0].call(pair[1], b)
- );
- });
- },
- /**
- * Returns the minimal value on the list
- *
- * @return Number minimal value
- */
- min: function() {
- return Math.min.apply(Math, this);
- },
- /**
- * Returns the maximal value
- *
- * @return Number maximal value
- */
- max: function() {
- return Math.max.apply(Math, this);
- },
- /**
- * Returns a summ of all the items on the list
- *
- * @return Number a summ of values on the list
- */
- sum: function() {
- for(var sum=0, i=0, l=this.length; i < l; sum += this[i++]) {}
- return sum;
- }
- });
- A_proto.include = A_proto.includes;
- /**
- * The String class extentions
- *
- * Credits:
- * 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-2011 Nikolay V. Nemshilov
- */
- String.include({
- /**
- * checks if the string is an empty string
- *
- * @return boolean check result
- */
- empty: function() {
- return this == '';
- },
- /**
- * checks if the string contains only white-spaces
- *
- * @return boolean check result
- */
- blank: function() {
- return this == false;
- },
- /**
- * removes trailing whitespaces
- *
- * @return String trimmed version
- */
- trim: String.prototype.trim || function() {
- var str = this.replace(/^\s\s*/, ''), i = str.length;
- while ((/\s/).test(str.charAt(--i))) {}
- return str.slice(0, i + 1);
- },
- /**
- * returns a copy of the string with all the tags removed
- * @return String without tags
- */
- stripTags: function() {
- return this.replace(/<\/?[^>]+>/ig, '');
- },
- /**
- * removes all the scripts declarations out of the string
- * @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 = '', 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);
- }
- return text;
- },
- /**
- * extracts all the scripts out of the string
- *
- * @return String the extracted stcripts
- */
- extractScripts: function() {
- var scripts = '';
- this.stripScripts(function(s) { scripts = s; });
- return scripts;
- },
- /**
- * evals all the scripts in the string
- *
- * @return String self (unchanged version with scripts still in their place)
- */
- evalScripts: function() {
- this.stripScripts(true);
- return this;
- },
- /**
- * converts underscored or dasherized string to a camelized one
- * @returns String camelized version
- */
- camelize: function() {
- return this.replace(/(\-|_)+(.)?/g, function(match, dash, chr) {
- return chr ? chr.toUpperCase() : '';
- });
- },
- /**
- * converts a camelized or dasherized string into an underscored one
- * @return String underscored version
- */
- underscored: function() {
- return this.replace(/([a-z\d])([A-Z]+)/g, '$1_$2').replace(/\-/g, '_').toLowerCase();
- },
- /**
- * returns a capitalised version of the string
- *
- * @return String captialised version
- */
- capitalize: function() {
- return this.charAt(0).toUpperCase() + this.substring(1).toLowerCase();
- },
- /**
- * checks if the string contains the given substring
- *
- * @param String string
- * @return boolean check result
- */
- includes: function(string) {
- return this.indexOf(string) != -1;
- },
- /**
- * checks if the string starts with the given substring
- *
- * @param String string
- * @param boolean ignore the letters case
- * @return boolean check result
- */
- startsWith: function(string, ignorecase) {
- return (ignorecase !== true ? this.indexOf(string) :
- this.toLowerCase().indexOf(string.toLowerCase())
- ) === 0;
- },
- /**
- * checks if the string ends with the given substring
- *
- * @param String substring
- * @param boolean ignore the letters case
- * @return boolean check result
- */
- endsWith: function(string, ignorecase) {
- return this.length - (
- ignorecase !== true ? this.lastIndexOf(string) :
- this.toLowerCase().lastIndexOf(string.toLowerCase())
- ) === string.length;
- },
- /**
- * converts the string to an integer value
- * @param Integer base
- * @return Integer or NaN
- */
- toInt: function(base) {
- return parseInt(this, base === undefined ? 10 : base);
- },
- /**
- * converts the string to a float value
- * @param boolean flat if the method should not use a flexible matching
- * @return Float or NaN
- */
- toFloat: function(strict) {
- return parseFloat(strict === true ? this :
- this.replace(',', '.').replace(/(\d)-(\d)/, '$1.$2'));
- }
- });
- String.prototype.include = String.prototype.includes;
- /**
- * The Function class extentions
- *
- * Credits:
- * Some of the functionality inspired by
- * - Prototype (http://prototypejs.org) Copyright (C) Sam Stephenson
- *
- * Copyright (C) 2008-2011 Nikolay V. Nemshilov
- */
- Function.include({
- /**
- * binds the function to be executed in the given scope
- *
- * @param Object scope
- * @param mixed optional curry (left) argument
- * ....
- * @return Function binded function
- */
- bind: function() {
- var args = $A(arguments), scope = args.shift(), func = this;
- return function() {
- return func.apply(scope,
- (args.length !== 0 || arguments.length !== 0) ?
- args.concat($A(arguments)) : args
- );
- };
- },
- /**
- * binds the function as an event listener to the given scope object
- *
- * @param Object scope
- * @param mixed optional curry (left) argument
- * .......
- * @return Function binded function
- */
- bindAsEventListener: function() {
- var args = $A(arguments), scope = args.shift(), func = this;
- return function(event) {
- return func.apply(scope, [event].concat(args).concat($A(arguments)));
- };
- },
- /**
- * allows you to put some curry in your cookery
- *
- * @param mixed value to curry
- * ....
- * @return Function curried function
- */
- curry: function() {
- return this.bind.apply(this, [this].concat($A(arguments)));
- },
- /**
- * The right side curry feature
- *
- * @param mixed value to curry
- * ....
- * @return Function curried function
- */
- rcurry: function() {
- var curry = $A(arguments), func = this;
- return function() {
- return func.apply(func, $A(arguments).concat(curry));
- };
- },
- /**
- * delays the function execution
- *
- * @param Integer delay ms
- * @param mixed value to curry
- * .....
- * @return Integer timeout marker
- */
- delay: function() {
- var args = $A(arguments), timeout = args.shift(),
- timer = new Number(setTimeout(this.bind.apply(this, [this].concat(args)), timeout));
- timer.cancel = function() { clearTimeout(this); };
- return timer;
- },
- /**
- * creates a periodical execution of the function with the given timeout
- *
- * @param Integer delay ms
- * @param mixed value to curry
- * ...
- * @return Ineger interval marker
- */
- periodical: function() {
- var args = $A(arguments), timeout = args.shift(),
- timer = new Number(setInterval(this.bind.apply(this, [this].concat(args)), timeout));
- timer.stop = function() { clearInterval(this); };
- return timer;
- },
- /**
- * Chains the given function after the current one
- *
- * @param Function the next function
- * @param mixed optional value to curry
- * ......
- * @return Function chained function
- */
- chain: function() {
- var args = $A(arguments), func = args.shift(), current = this;
- return function() {
- var result = current.apply(current, arguments);
- func.apply(func, args);
- return result;
- };
- }
- });
- /**
- * The Number class extentions
- *
- * Credits:
- * Some methods inspired by
- * - Ruby (http://www.ruby-lang.org) Copyright (C) Yukihiro Matsumoto
- *
- * Copyright (C) 2008-2010 Nikolay V. Nemshilov
- */
- Number.include({
- /**
- * executes the given callback the given number of times
- *
- * @param Function callback
- * @param Object optional callback execution scope
- * @return void
- */
- times: function(callback, scope) {
- for (var i=0; i < this; i++) {
- callback.call(scope, i);
- }
- return this;
- },
- upto: function(number, callback, scope) {
- for (var i=this+0; i <= number; i++) {
- callback.call(scope, i);
- }
- return this;
- },
- downto: function(number, callback, scope) {
- for (var i=this+0; i >= number; i--) {
- callback.call(scope, i);
- }
- return this;
- },
- abs: function() {
- return Math.abs(this);
- },
- round: function(size) {
- return size ? parseFloat(this.toFixed(size)) : Math.round(this);
- },
- ceil: function() {
- return Math.ceil(this);
- },
- floor: function() {
- return Math.floor(this);
- },
- min: function(value) {
- return this < value ? value : this + 0;
- },
- max: function(value) {
- return this > value ? value : this + 0;
- }
- });
- /**
- * The Regexp class extentions
- *
- * Credits:
- * Inspired by
- * - Prototype (http://prototypejs.org) Copyright (C) Sam Stephenson
- *
- * Copyright (C) 2008-2010 Nikolay V. Nemshilov
- */
- /**
- * Escapes the string for safely use as a regular expression
- *
- * @param String raw string
- * @return String escaped string
- */
- RegExp.escape = function(string) {
- return (''+string).replace(/([.*+?\^=!:${}()|\[\]\/\\])/g, '\\$1');
- };
- /**
- * The basic Class unit
- *
- * Credits:
- * The Class unit is inspired by its implementation in
- * - Prototype (http://prototypejs.org) Copyright (C) Sam Stephenson
- * - MooTools (http://mootools.net) Copyright (C) Valerio Proietti
- * - Ruby (http://www.ruby-lang.org) Copyright (C) Yukihiro Matsumoto
- *
- * Copyright (C) 2008-2011 Nikolay Nemshilov
- */
- var Class = RightJS.Class = function() {
- var args = $A(arguments).slice(0,2),
- props = args.pop() || {},
- parent = args.pop(),
- klass = arguments[2], // you can send your own klass as the third argument
- SKlass = function() {};
- // if the parent class only was specified
- if (!args.length && !isHash(props)) {
- parent = props; props = {};
- }
- // !#server:begin
- if (!klass && parent && (parent === Wrapper || parent.ancestors.include(Wrapper))) {
- klass = Wrapper_makeKlass();
- }
- // !#server:end
- // defining the basic klass function
- klass = $ext(klass || function() {
- Class_checkPrebind(this);
- return 'initialize' in this ?
- this.initialize.apply(this, arguments) :
- this;
- }, Class_Methods);
- // handling the inheritance
- parent = parent || Class;
- SKlass.prototype = parent.prototype;
- klass.prototype = new SKlass();
- klass.parent = parent;
- klass.prototype.constructor = klass;
- // collecting the list of ancestors
- klass.ancestors = [];
- while (parent) {
- klass.ancestors.push(parent);
- parent = parent.parent;
- }
- // handling the module injections
- ['extend', 'include'].each(function(name) {
- if (name in props) {
- klass[name].apply(klass, ensure_array(props[name]));
- }
- });
- return klass.include(props);
- },
- /**
- * Class utility methods
- *
- * Copyright (C) 2008-2011 Nikolay Nemshilov
- */
- Class_Methods = {
- /**
- * this method will extend the class-level with the given objects
- *
- * NOTE: this method _WILL_OVERWRITE_ the existing itercecting entries
- *
- * NOTE: this method _WILL_NOT_OVERWRITE_ the class prototype and
- * the class 'name' and 'parent' attributes. If one of those
- * exists in one of the received modeuls, the attribute will be
- * skipped
- *
- * @param Object module to extend
- * ....
- * @return Class the klass
- */
- extend: function() {
- $A(arguments).filter(isHash).each(function(module) {
- $ext(this, Class_clean_module(module, true));
- Class_handle_module_callbacks(this, module, true);
- }, this);
- return this;
- },
- /**
- * extends the class prototype with the given objects
- * NOTE: this method _WILL_OVERWRITE_ the existing itercecting entries
- * NOTE: this method _WILL_NOT_OVERWRITE_ the 'klass' attribute of the klass.prototype
- *
- * @param Object module to include
- * ....
- * @return Class the klass
- */
- include: function() {
- var klasses = [this].concat(this.ancestors);
- $A(arguments).filter(isHash).each(function(module) {
- Object.each(Class_clean_module(module, false), function(name, method) {
- // searching for the super-method
- for (var super_method, i=0, l = klasses.length; i < l; i++) {
- if (name in klasses[i].prototype) {
- super_method = klasses[i].prototype[name];
- break;
- }
- }
- this.prototype[name] = isFunction(method) && isFunction(super_method) ?
- function() {
- this.$super = super_method;
- return method.apply(this, arguments);
- } : method;
- }, this);
- Class_handle_module_callbacks(this, module, false);
- }, this);
- return this;
- }
- },
- Class_module_callback_names = $w(
- 'selfExtended self_extended selfIncluded self_included extend include'
- );
- // hooking up the class-methods to the root class
- $ext(Class, Class_Methods);
- Class.prototype.$super = undefined;
- function Class_clean_module(module, extend) {
- return Object.without.apply(Object, [module].concat(
- Class_module_callback_names.concat( extend ?
- $w('prototype parent ancestors') : ['constructor']
- )
- ));
- }
- function Class_handle_module_callbacks(klass, module, extend) {
- (module[Class_module_callback_names[extend ? 0 : 2]] ||
- module[Class_module_callback_names[extend ? 1 : 3]] ||
- function() {}
- ).call(module, klass);
- }
- /**
- * This method gets through a list of the object its class and all the ancestors
- * and finds a hash named after property, used for configuration purposes with
- * the Observer and Options modules
- *
- * NOTE: this method will look for capitalized and uppercased versions of the
- * property name
- *
- * @param Object a class instance
- * @param String property name
- * @return Object hash or null if nothing found
- */
- function Class_findSet(object, property) {
- var upcased = property.toUpperCase(),
- constructor = object.constructor,
- candidates = [object, constructor].concat(constructor.ancestors || []),
- i = 0;
- for (l = candidates.length; i < l; i++) {
- if (upcased in candidates[i]) {
- return candidates[i][upcased];
- } else if (property in candidates[i]) {
- return candidates[i][property];
- }
- }
- return null;
- }
- /**
- * Handles the 'prebind' feature for Class instances
- *
- * @param Class instance
- * @return void
- */
- function Class_checkPrebind(object) {
- if ('prebind' in object && isArray(object.prebind)) {
- object.prebind.each(function(method) {
- object[method] = object[method].bind(object);
- });
- }
- }
- /**
- * This is a simple mix-in module to be included in other classes
- *
- * Basically it privdes the <tt>setOptions</tt> method which processes
- * an instance options assigment and merging with the default options
- *
- * Credits:
- * The idea of the module is inspired by
- * - MooTools (http://mootools.net) Copyright (C) Valerio Proietti
- *
- * Copyright (C) 2008-2011 Nikolay V. Nemshilov
- */
- var Options = RightJS.Options = {
- /**
- * assigns the options by merging them with the default ones
- *
- * @param Object options
- * @return Object current instance
- */
- setOptions: function(opts) {
- var options = this.options = $ext($ext({},
- Object.clone(Class_findSet(this, 'Options'))), opts
- ), match, key;
- // hooking up the observer options
- if (isFunction(this.on)) {
- for (key in options) {
- if ((match = key.match(/on([A-Z][A-Za-z]+)/))) {
- this.on(match[1].toLowerCase(), options[key]);
- delete(options[key]);
- }
- }
- }
- return this;
- },
- /**
- * Cuts of an options hash from the end of the arguments list
- * assigns them using the #setOptions method and then
- * returns the list of other arguments as an Array instance
- *
- * @param mixed iterable
- * @return Array of the arguments
- */
- cutOptions: function(in_args) {
- var args = $A(in_args);
- this.setOptions(isHash(args.last()) ? args.pop() : {});
- return args;
- }
- };
- /**
- * standard Observer class.
- *
- * Might be used as a usual class or as a builder over another objects
- *
- * Credits:
- * The naming principle is inspired by
- * - Prototype (http://prototypejs.org) Copyright (C) Sam Stephenson
- *
- * Copyright (C) 2008-2011 Nikolay Nemshilov
- */
- var Observer = RightJS.Observer = new Class({
- include: Options,
- /**
- * general constructor
- *
- * @param Object options
- */
- initialize: function(options) {
- this.setOptions(options);
- Observer_createShortcuts(this, Class_findSet(this, 'Events'));
- return this;
- },
- /**
- * binds an event listener
- *
- * USAGE:
- * on(String event, Function callback[, arguments, ...]);
- * on(String event, String method_name[, arguments, ...]);
- * on(Object events_hash);
- *
- * @return Observer self
- */
- on: function() {
- Observer_on(this, arguments, function(h) { return h; });
- return this;
- },
- /**
- * checks if the observer observes given event and/or callback
- *
- * USAGE:
- * observes(String event)
- * observes(Function callback)
- * observes(String event, Function callback)
- *
- * @retun boolean check result
- */
- observes: function(event, callback) {
- if (!isString(event)) { callback = event; event = null; }
- if (isString(callback)) { callback = callback in this ? this[callback] : null; }
- return (this.$listeners || []).some(function(i) {
- return (event && callback) ? i.e === event && i.f === callback :
- event ? i.e === event : i.f === callback;
- });
- },
- /**
- * stops observing an event or/and function
- *
- * USAGE:
- * stopObserving(String event)
- * stopObserving(Function callback)
- * stopObserving(String event, Function callback)
- *
- * @return Observer self
- */
- stopObserving: function(event, callback) {
- Observer_stopObserving(this, event, callback, function() {});
- return this;
- },
- /**
- * returns the listeners list for the event
- *
- * NOTE: if no event was specified the method will return _all_
- * event listeners for _all_ the events
- *
- * @param String event name
- * @return Array of listeners
- */
- listeners: function(event) {
- return (this.$listeners || []).filter(function(i) {
- return !event || i.e === event;
- }).map(function(i) { return i.f; }).uniq();
- },
- /**
- * initiates the event handling
- *
- * @param String event name
- * @param mixed optional argument
- * ........
- * @return Observer self
- */
- fire: function() {
- var args = $A(arguments), event = args.shift();
- (this.$listeners || []).each(function(i) {
- if (i.e === event) {
- i.f.apply(this, i.a.concat(args));
- }
- }, this);
- return this;
- }
- }),
- /**
- * adds an observer functionality to any object
- *
- * @param Object object
- * @param Array optional events list to build shortcuts
- * @return Object extended object
- */
- Observer_create = Observer.create = function(object, events) {
- $ext(object, Object.without(Observer.prototype, 'initialize', 'setOptions'), true);
- return Observer_createShortcuts(object, events || Class_findSet(object, 'Events'));
- },
- /**
- * builds shortcut methods to wire/fire events on the object
- *
- * @param Object object to extend
- * @param Array list of event names
- * @return Object extended object
- */
- Observer_createShortcuts = Observer.createShortcuts = function(object, names) {
- (names || []).each(function(name) {
- var method_name = 'on'+name.replace(/(^|_|:)([a-z])/g,
- function(match, pre, chr) { return chr.toUpperCase(); }
- );
- if (!(method_name in object)) {
- object[method_name] = function() {
- return this.on.apply(this, [name].concat($A(arguments)));
- };
- }
- });
- return object;
- };
- function Observer_on(object, o_args, preprocess) {
- var args = slice.call(o_args, 2),
- event = o_args[0],
- callback = o_args[1],
- name = false;
- if (isString(event)) {
- switch (typeof callback) {
- case "string":
- name = callback;
- callback = callback in object ? object[callback] : function() {};
- case "function":
- ('$listeners' in object ? object.$listeners : (
- object.$listeners = []
- )).push(preprocess({
- e: event, f: callback, a: args, r: name || false, t: object
- }));
- break;
- default:
- if (isArray(callback)) {
- for (var i=0; i < callback.length; i++) {
- object.on.apply(object, [event].concat(
- ensure_array(callback[i])
- ).concat(args));
- }
- }
- }
- } else {
- // assuming it's a hash of key-value pairs
- args = slice.call(o_args, 1);
- for (name in event) {
- object.on.apply(object, [name].concat(
- ensure_array(event[name])
- ).concat(args));
- }
- }
- }
- function Observer_stopObserving(object, event, callback, preprocess) {
- if (isHash(event)) {
- for (var key in event) {
- object.stopObserving(key, event[key]);
- }
- } else {
- if (!isString(event)) { callback = event; event = null; }
- if (isString(callback)){ callback = object[callback]; }
- object.$listeners = (object.$listeners || []).filter(function(i) {
- var result = (event && callback) ?
- (i.e !== event || i.f !== callback) :
- (event ? i.e !== event : i.f !== callback);
- if (!result) { preprocess(i); }
- return result;
- });
- }
- }
- /**
- * this object will contain info about the current browser
- *
- * Copyright (C) 2008-2011 Nikolay V. Nemshilov
- */
- var agent = navigator.userAgent,
- Browser_Opera = 'opera' in window,
- Browser_IE = 'attachEvent' in window && !Browser_Opera,
- Browser = RightJS.Browser = {
- IE: Browser_IE,
- Opera: Browser_Opera,
- WebKit: agent.include('AppleWebKit/'),
- Gecko: agent.include('Gecko') && !agent.include('KHTML'),
- MobileSafari: /Apple.*Mobile.*Safari/.test(agent),
- Konqueror: agent.include('Konqueror'),
- // internal marker for the browsers which require the olds module
- OLD: !document.querySelector,
- // internal marker for IE browsers version <= 8
- IE8L: false
- },
- IE8_OR_LESS = false,
- IE_OPACITY = !('opacity' in HTML.style) && ('filter' in HTML.style);
- try {
- // checking if that an IE version <= 8
- document.createElement('<input/>');
- Browser.OLD = Browser.IE8L = IE8_OR_LESS = true;
- } catch(e) {}
- /**
- * The dom-wrapper main unit
- *
- * This unit is basically for the internal use
- * so that we could control the common functionality
- * among all the wrappers
- *
- * Copyright (C) 2010-2011 Nikolay Nemshilov
- */
- var Wrapper = RightJS.Wrapper = new Class({
- // predefining the property in the prototype
- _: undefined,
- /**
- * Default constructor
- *
- * @param mixed raw dom unit
- * @return void
- */
- initialize: function(raw_object) {
- this._ = raw_object;
- }
- });
- // exposing the cache so it could be manupulated externally
- Wrapper.Cache = Wrappers_Cache;
- // instantiating the actual class object for a wrapper
- function Wrapper_makeKlass() {
- /**
- * Default wrappers Klass function
- *
- * @param mixed the raw object
- * @param Object options
- * @return void
- */
- return function(object, options) {
- Class_checkPrebind(this);
- this.initialize.apply(this, arguments); // <- there might be a different number of args in a subclass
- var item = this._, uid = UID_KEY in item ? item[UID_KEY] :
- // NOTE we use positive indexes for dom-elements and negative for everything else
- (item[UID_KEY] = (item.nodeType === 1 ? 1 : -1) * UID++);
- Wrappers_Cache[uid] = this;
- };
- }
- /**
- * Element's own Klass function
- * we need that because it does some dynamic typecasting mumbo jumbo
- * plus we would like to optimize some stuff here and there
- *
- * @param raw dom element or the tag name
- * @param Object options
- * @return Element instance
- */
- function Element_Klass(element, options) {
- Element_initialize(this, element, options);
- var inst = this, raw = inst._, cast = Wrapper.Cast(raw),
- uid = UID_KEY in raw ? raw[UID_KEY] : (raw[UID_KEY] = UID++);
- if (cast !== undefined) {
- inst = new cast(raw, options);
- if ('$listeners' in this) {
- inst.$listeners = this.$listeners;
- }
- }
- Wrappers_Cache[uid] = inst;
- return inst;
- }
- // searches for a suitable class for dynamic typecasting
- Wrapper.Cast = function(unit) {
- return unit.tagName in Element_wrappers ? Element_wrappers[unit.tagName] : undefined;
- };
- /**
- * Event's own Klass function, we don't need to check
- * nothing in here, don't need to hit the wrappers cache and so one
- *
- * @param raw dom-event or a string event-name
- * @param bounding element or an object with options
- * @return void
- */
- function Event_Klass(event, bound_element) {
- if (typeof(event) === 'string') {
- event = $ext({type: event}, bound_element);
- this.stopped = event.bubbles === false;
- if (isHash(bound_element)) {
- $ext(this, bound_element);
- }
- }
- this._ = event;
- this.type = event.type;
- this.which = event.which;
- this.keyCode = event.keyCode;
- this.target = wrap(
- // Webkit throws events on textual nodes as well, gotta fix that
- event.target != null && 'nodeType' in event.target && event.target.nodeType === 3 ?
- event.target.parentNode : event.target
- );
- this.currentTarget = wrap(event.currentTarget);
- this.relatedTarget = wrap(event.relatedTarget);
- this.pageX = event.pageX;
- this.pageY = event.pageY;
- // making old IE attrs looks like w3c standards
- if (IE8_OR_LESS && 'srcElement' in event) {
- this.which = event.button === 2 ? 3 : event.button === 4 ? 2 : 1;
- this.target = wrap(event.srcElement) || bound_element;
- this.relatedTarget = this.target._ === event.fromElement ? wrap(event.toElement) : this.target;
- this.currentTarget = bound_element;
- var scrolls = this.target.win().scrolls();
- this.pageX = event.clientX + scrolls.x;
- this.pageY = event.clientY + scrolls.y;
- }
- }
- /**
- * Private quick wrapping function, unlike `$`
- * it doesn't search by ID and handle double-wrapps
- * just pure dom-wrapping functionality
- *
- * @param raw dom unit
- * @return Wrapper dom-wrapper
- */
- function wrap(object) {
- if (object != null) {
- var wrapper = UID_KEY in object ? Wrappers_Cache[object[UID_KEY]] : undefined;
- if (wrapper !== undefined) {
- return wrapper;
- } else if (object.nodeType === 1) {
- return new Element(object);
- } else if (object.nodeType === 9) {
- return new Document(object);
- } else if (object.window == object) {
- return new Window(object);
- } else if (isElement(object.target) || isElement(object.srcElement)) {
- return new Event(object);
- }
- }
- return object;
- }
- /**
- * A simple document wrapper
- *
- * Copyright (C) 2010-2011 Nikolay Nemshilov
- */
- var Document = RightJS.Document = new Class(Wrapper, {
- // returns the window reference
- win: function() {
- return wrap(this._.defaultView || this._.parentWindow);
- }
- }),
- // a common local wrapped document reference
- current_Document = wrap(document);
- /**
- * the window object extensions
- *
- * Copyright (C) 2008-2011 Nikolay Nemshilov
- */
- var Window = RightJS.Window = new Class(Wrapper, {
- /**
- * Selfreference to have a common interface with the rest of the wrappers
- * in case of events handling
- *
- * @return Window
- */
- win: function() {
- return this;
- },
- /**
- * returns the inner-size of the window
- *
- * @return Object x: d+, y: d+
- */
- size: function() {
- var win = this._, html = win.document.documentElement;
- return win.innerWidth ? {x: win.innerWidth, y: win.innerHeight} :
- {x: html.clientWidth, y: html.clientHeight};
- },
- /**
- * returns the scrolls for the window
- *
- * @return Object x: d+, y: d+
- */
- scrolls: function() {
- var win = this._, doc = win.document, body = doc.body, html = doc.documentElement;
- return (win.pageXOffset || win.pageYOffset) ? {x: win.pageXOffset, y: win.pageYOffset} :
- (body && (body.scrollLeft || body.scrollTop)) ? {x: body.scrollLeft, y: body.scrollTop} :
- {x: html.scrollLeft, y: html.scrollTop};
- },
- /**
- * overloading the native scrollTo method to support hashes and element references
- *
- * @param mixed number left position, a hash position, element or a string element id
- * @param number top position
- * @param Object fx options
- * @return window self
- */
- scrollTo: function(left, top, fx_options) {
- var left_pos = left, top_pos = top,
- element = isNumber(left) ? null : $(left);
- if(element instanceof Element) {
- left = element.position();
- }
- if (isHash(left)) {
- top_pos = left.y;
- left_pos = left.x;
- }
- // checking if a smooth scroll was requested
- if (isHash(fx_options = fx_options || top) && RightJS.Fx) {
- new Fx.Scroll(this, fx_options).start({x: left_pos, y: top_pos});
- } else {
- this._.scrollTo(left_pos, top_pos);
- }
- return this;
- }
- });
- /**
- * represents some additional functionality for the Event class
- *
- * NOTE: there more additional functionality for the Event class in the rightjs-goods project
- *
- * Credits:
- * The additional method names are inspired by
- * - Prototype (http://prototypejs.org) Copyright (C) Sam Stephenson
- *
- * Copyright (C) 2008-2011 Nikolay Nemshilov
- */
- var Event = RightJS.Event = new Class(Wrapper, {
- // predefining the keys to spped up the assignments
- type: null,
- which: null,
- keyCode: null,
- target: null,
- currentTarget: null,
- relatedTarget: null,
- pageX: null,
- pageY: null,
- /**
- * the class constructor
- *
- * @param raw dom-event
- * @param HTMLElement the bound element
- * @return void
- */
- initialize: Event_Klass, // the actual initialization happens in the Klass function
- /**
- * Stops the event bubbling process
- *
- * @return RightJS.Event this
- */
- stopPropagation: function() {
- if (this._.stopPropagation) {
- this._.stopPropagation();
- } else {
- this._.cancelBubble = true;
- }
- this.stopped = true;
- return this;
- },
- /**
- * Prevents the default browser action on the event
- *
- * @return RightJS.Event this
- */
- preventDefault: function() {
- if (this._.preventDefault) {
- this._.preventDefault();
- } else {
- this._.returnValue = false;
- }
- return this;
- },
- /**
- * Fully stops the event
- *
- * @return RightJS.Event this
- */
- stop: function() {
- return this.stopPropagation().preventDefault();
- },
- /**
- * Returns the event position
- *
- * @return Object {x: ..., y: ...}
- */
- position: function() {
- return {x: this.pageX, y: this.pageY};
- },
- /**
- * Returns the event's offset relative to the target element
- *
- * @return Object {x: ..., y: ...} or null
- */
- offset: function() {
- if(this.target instanceof Element) {
- var element_position = this.target.position();
- return {
- x: this.pageX - element_position.x,
- y: this.pageY - element_position.y
- };
- }
- // triggered outside browser window (at toolbar etc.)
- return null;
- },
- /**
- * Finds the element between the event target
- * and the boundary element that matches the
- * css-rule
- *
- * @param String css-rule
- * @return Element element or null
- */
- find: function(css_rule) {
- if (this.target instanceof Wrapper && this.currentTarget instanceof Wrapper) {
- var target = this.target._,
- search = this.currentTarget.find(css_rule, true);
- while (target) {
- if (search.indexOf(target) !== -1) {
- return wrap(target);
- }
- target = target.parentNode;
- }
- }
- return undefined;
- }
- }, Event_Klass),
- Event_delegation_shortcuts = [];
- /**
- * The DOM Element unit handling
- *
- * Copyright (C) 2008-2011 Nikolay Nemshilov
- */
- var Element = RightJS.Element = new Class(Wrapper, {
- /**
- * constructor
- *
- * NOTE: this constructor will dynamically typecast
- * the wrappers depending on the element tag-name
- *
- * @param String element tag name or an HTMLElement instance
- * @param Object options
- * @return Element element
- */
- initialize: function(element, options) {
- Element_initialize(this, element, options);
- }
- }, Element_Klass),
- Element_wrappers = Element.Wrappers = {},
- elements_cache = {},
- /**
- * bulds dom-elements
- *
- * @param String element tag name
- * @param Object options
- * @return HTMLElement
- */
- make_element = function (tag, options) {
- return (tag in elements_cache ? elements_cache[tag] : (
- elements_cache[tag] = document.createElement(tag)
- )).cloneNode(false);
- };
- //
- // IE 6,7,8 (not 9!) browsers have a bug with checkbox and radio input elements
- // it doesn't place the 'checked' property correctly, plus there are some issues
- // with clonned SELECT objects, so we are replaceing the elements maker in here
- //
- if (IE8_OR_LESS) {
- make_element = function(tag, options) {
- if (tag === 'input' && options !== undefined) {
- tag = '<input name="'+ options.name +
- '" type='+ options.type +
- (options.checked ? ' checked' : '') +
- '/>';
- }
- return document.createElement(tag);
- };
- }
- /**
- * Basic element's constructor
- *
- * @param Element wrapper instance
- * @param mixed raw dom element of a string tag name
- * @param Object options
- * @return void
- */
- function Element_initialize(inst, element, options) {
- if (typeof element === 'string') {
- inst._ = make_element(element, options);
- if (options !== undefined) {
- for (var key in options) {
- switch (key) {
- case 'id': inst._.id = options[key]; break;
- case 'html': inst._.innerHTML = options[key]; break;
- case 'class': inst._.className = options[key]; break;
- case 'on': inst.on(options[key]); break;
- default: inst.set(key, options[key]);
- }
- }
- }
- } else {
- inst._ = element;
- }
- }
- /**
- * 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
- *
- * NOTE: if a css-rule was specified then the result of the method
- * will be filtered/adjusted depends on the rule
- *
- * Credits:
- * 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-2011 Nikolay Nemshilov
- */
- Element.include({
- parent: function(css_rule) {
- var parent = this._.parentNode, parent_type = parent && parent.nodeType;
- return css_rule ? this.parents(css_rule)[0] :
- (parent_type === 1 || parent_type === 9) ? // <- IE6 sometimes has a fragment node in there
- wrap(parent) : null;
- },
- parents: function(css_rule) {
- return recursively_collect(this, 'parentNode', css_rule);
- },
- children: function(css_rule) {
- return this.find(css_rule).filter(function(element) {
- return element._.parentNode === this._;
- }, this);
- },
- siblings: function(css_rule) {
- return this.prevSiblings(css_rule).reverse().concat(this.nextSiblings(css_rule));
- },
- nextSiblings: function(css_rule) {
- return recursively_collect(this, 'nextSibling', css_rule);
- },
- prevSiblings: function(css_rule) {
- return recursively_collect(this, 'previousSibling', css_rule);
- },
- next: function(css_rule) {
- return !css_rule && this._.nextElementSibling !== undefined ?
- wrap(this._.nextElementSibling) : this.nextSiblings(css_rule)[0];
- },
- prev: function(css_rule) {
- return !css_rule && this._.previousElementSibling !== undefined ?
- wrap(this._.previousElementSibling) : this.prevSiblings(css_rule)[0];
- },
- /**
- * removes the elemnt out of this parent node
- *
- * @return Element self
- */
- remove: function() {
- var element = this._, parent = element.parentNode;
- if (parent) {
- parent.removeChild(element);
- }
- return this;
- },
- /**
- * handles the elements insertion functionality
- *
- * The content might be one of the following data
- *
- * o) an element instance
- * o) a String (all the scripts will be parsed out and executed)
- * o) a list of Elements
- * o) a hash like {position: content}
- *
- * @param mixed data to insert
- * @param String position to insert top/bottom/before/after/instead
- * @return Element self
- */
- insert: function(content, position) {
- var scripts = null, element = this._;
- position = position === undefined ? 'bottom' : position;
- if (typeof(content) !== 'object') {
- scripts = content = (''+content);
- } else if (content instanceof Element) {
- content = content._;
- }
- Element_insertions[position](element,
- content.nodeType === undefined ?
- Element_createFragment(
- (position === 'bottom' || position === 'top') ?
- element : element.parentNode, content
- ) : content
- );
- if (scripts !== null) { scripts.evalScripts(); }
- return this;
- },
- /**
- * Inserts the element inside the given one at the given position
- *
- * @param mixed destination element reference
- * @param String optional position
- * @return Element this
- */
- insertTo: function(element, position) {
- $(element).insert(this, position);
- return this;
- },
- /**
- * A shortcut to uppend several units into the element
- *
- * @param mixed data
- * ..................
- * @return Element this
- */
- append: function(first) {
- return this.insert(isString(first) ? $A(arguments).join('') : arguments);
- },
- /**
- * updates the content of the element by the given content
- *
- * @param mixed content (a String, an Element or a list of elements)
- * @return Element self
- */
- update: function(content) {
- if (typeof(content) !== 'object') {
- content = '' + content;
- try {
- this._.innerHTML = content;
- } catch(e) {
- return this.clean().insert(content);
- }
- content.evalScripts();
- return this;
- } else {
- return this.clean().insert(content);
- }
- },
- /**
- * Works with the Element's innerHTML property
- * This method works both ways! if a content is provided
- * then it will be assigned, otherwise will return
- * the innerHTML property
- *
- * @param String html content
- * @return String html content or Element this
- */
- html: function(content) {
- return content === undefined ? this._.innerHTML : this.update(content);
- },
- /**
- * Works with the Element's innerHTML property as a text
- * when set something, it will appear as is with everything quoted
- * when get, will return a string without any tags in it
- *
- * @param String text content
- * @return String text content or Element this
- */
- text: function(text) {
- return text === undefined ? (this._.textContent || this._.innerText) :
- this.update(this.doc()._.createTextNode(text));
- },
- /**
- * replaces the current element by the given content
- *
- * @param mixed content (a String, an Element or a list of elements)
- * @return Element self
- */
- replace: function(content) {
- return this.insert(content, 'instead');
- },
- /**
- * wraps the element with the given element
- *
- * @param Element wrapper
- * @return Element self
- */
- wrap: function(wrapper) {
- var element = this._, parent = element.parentNode;
- if (parent) {
- wrapper = $(wrapper)._;
- parent.replaceChild(wrapper, element);
- wrapper.appendChild(element);
- }
- return this;
- },
- /**
- * removes all the child nodes out of the element
- *
- * @return Element self
- */
- clean: function() {
- while (this._.firstChild) {
- this._.removeChild(this._.firstChild);
- }
- return this;
- },
- /**
- * checks if the element has no child nodes
- *
- * @return boolean check result
- */
- empty: function() {
- return this.html().blank();
- },
- /**
- * Creates a clean clone of the element without any events attached to it
- *
- * @return Element new clone
- */
- clone: function() {
- return new Element(this._.cloneNode(true));
- },
- /**
- * Returns an index of the element among the other child elements
- *
- * NOTE: doesn't count the textual nodes!
- *
- * @return Integer index
- */
- index: function() {
- var node = this._,
- sibling = node.parentNode.firstChild,
- index = 0;
- while (sibling !== node) {
- if (sibling.nodeType === 1) { // counting elements only
- index ++;
- }
- sibling = sibling.nextSibling;
- }
- return index;
- }
- });
- /**
- * Recursively collects the target element's related nodes
- *
- * @param Element context
- * @param name String pointer attribute name
- * @param rule String optional css-atom rule
- * @return Array found elements
- */
- function recursively_collect(where, attr, css_rule) {
- var node = where._, result = [], i=0, no_rule = !css_rule;
- while ((node = node[attr])) {
- if (node.nodeType === 1 && (no_rule || wrap(node).match(css_rule))) {
- result[i++] = wrap(node);
- }
- }
- return result;
- }
- // list of insertions handling functions
- // NOTE: each of the methods will be called in the contects of the current element
- var Element_insertions = {
- bottom: function(target, content) {
- target.appendChild(content);
- },
- top: function(target, content) {
- if (target.firstChild !== null) {
- target.insertBefore(content, target.firstChild);
- } else {
- target.appendChild(content);
- }
- },
- after: function(target, content) {
- var parent = target.parentNode, sibling = target.nextSibling;
- if (sibling !== null) {
- parent.insertBefore(content, sibling);
- } else {
- parent.appendChild(content);
- }
- },
- before: function(target, content) {
- target.parentNode.insertBefore(content, target);
- },
- instead: function(target, content) {
- target.parentNode.replaceChild(content, target);
- }
- },
- // the element insertion wrappers list
- Element_wraps = {
- TBODY: ['<TABLE>',