📄 prototype.js
字号:
/* Prototype JavaScript framework, version 1.5.1.1 * (c) 2005-2007 Sam Stephenson * * Prototype is freely distributable under the terms of an MIT-style license. * For details, see the Prototype web site: http://www.prototypejs.org/ */*--------------------------------------------------------------------------*//*** @classDescription Declares the version of the prototype library.*/var Prototype = { /** * Version of the library. */ Version: '1.5.1.1', Browser: { IE: !!(window.attachEvent && !window.opera), Opera: !!window.opera, WebKit: navigator.userAgent.indexOf('AppleWebKit/') > -1, Gecko: navigator.userAgent.indexOf('Gecko') > -1 && navigator.userAgent.indexOf('KHTML') == -1 }, BrowserFeatures: { XPath: !!document.evaluate, ElementExtensions: !!window.HTMLElement, SpecificElementExtensions: (document.createElement('div').__proto__ !== document.createElement('form').__proto__) }, /** * RegExp used to identify scripts. */ ScriptFragment: '<script[^>]*>([\\S\\s]*?)<\/script>', JSONFilter: /^\/\*-secure-([\s\S]*)\*\/\s*$/, /** * Empty function object. */ emptyFunction: function() { }, /** * Function to echo back the specified parameter. * @param {Object} x Parameter to echo. * @return {Object} Returns the specified parameter. */ K: function(x) { return x }}/** * @classDescription Declares a class. * @constructor */var Class = { create: function() { return function() { this.initialize.apply(this, arguments); } }}var Abstract = new Object();/** * Implements inheritance by copying the properties and methods from one element to another. * @alias Object.extend() * @param {Object} destination Destination object to inherit the properties and methods. * @param {Object} source Source object to inherit properties and methods from. * @return {Object} Returns the destination object with the properties and methods from the source object. * @extends {Object} */Object.extend = function(destination, source) { for (var property in source) { destination[property] = source[property]; } return destination;}Object.extend(Object, { inspect: function(object) { try { if (object === undefined) return 'undefined'; if (object === null) return 'null'; return object.inspect ? object.inspect() : object.toString(); } catch (e) { if (e instanceof RangeError) return '...'; throw e; } }, toJSON: function(object) { var type = typeof object; switch(type) { case 'undefined': case 'function': case 'unknown': return; case 'boolean': return object.toString(); } if (object === null) return 'null'; if (object.toJSON) return object.toJSON(); if (object.ownerDocument === document) return; var results = []; for (var property in object) { var value = Object.toJSON(object[property]); if (value !== undefined) results.push(property.toJSON() + ': ' + value); } return '{' + results.join(', ') + '}'; }, keys: function(object) { var keys = []; for (var property in object) keys.push(property); return keys; }, values: function(object) { var values = []; for (var property in object) values.push(object[property]); return values; }, clone: function(object) { return Object.extend({}, object); }});/** * Binds an instance of the function to the object that owns the function. * @alias Function.bind() * @param {Function} function Function to bind to its owner object. * @param {Object, Array} ... Arguments to use when calling the function. * @return {Object} Returns the function pre-bound to its owner object with the specified arguments. * @extends {Function} */Function.prototype.bind = function() { var __method = this, args = $A(arguments), object = args.shift(); return function() { return __method.apply(object, args.concat($A(arguments))); }}/** * Binds an instance of a function to the object that owns the function as an event listener. * @alias Function.bindAsEventListener() * @param {Object} object Function to bind to its owner object. * @return {Object} Returns the function pre-bound to its owner object with the current event object as its argument. * @extends {Function} */Function.prototype.bindAsEventListener = function(object) { var __method = this, args = $A(arguments), object = args.shift(); return function(event) { return __method.apply(object, [event || window.event].concat(args)); }}Object.extend(Number.prototype, { toColorPart: function() { return this.toPaddedString(2, 16); },/** * Returns the next number in an iteration. * @alias Number.succ() * @return {Number} Returns the next number in an iteration. * @extends {Number} */ succ: function() { return this + 1; },/** * Calls the iterator function, repeatedly passing the current index as the index argument. * @alias Number.times() * @param {Object} iterator Iterator function to call. * @extends {Number} */ times: function(iterator) { $R(0, this, true).each(iterator); return this; }, toPaddedString: function(length, radix) { var string = this.toString(radix || 10); return '0'.times(length - string.length) + string; }, toJSON: function() { return isFinite(this) ? this.toString() : 'null'; }});Date.prototype.toJSON = function() { return '"' + this.getFullYear() + '-' + (this.getMonth() + 1).toPaddedString(2) + '-' + this.getDate().toPaddedString(2) + 'T' + this.getHours().toPaddedString(2) + ':' + this.getMinutes().toPaddedString(2) + ':' + this.getSeconds().toPaddedString(2) + '"';};/** * Tries the specified function calls until one of them is successful. * @alias Try.these() * @param {Function, Array} Function call(s) to try. * @return {String, Number} Returns the value of the first successful function call. */var Try = { these: function() { var returnValue; for (var i = 0, length = arguments.length; i < length; i++) { var lambda = arguments[i]; try { returnValue = lambda(); break; } catch (e) {} } return returnValue; }}/*--------------------------------------------------------------------------*//** * @classDescription Creates a PeriodicalExecuter object to call a function repeatedly at the specified interval. */var PeriodicalExecuter = Class.create();PeriodicalExecuter.prototype = { /** * Initializes the PeriodicalExecutor object. * @alias PeriodicalExecutor.initialize() * @param {Function} callback Function to be called. * @param {Object} frequency Interval (in seconds) to call the function at. */ initialize: function(callback, frequency) { this.callback = callback; this.frequency = frequency; this.currentlyExecuting = false; this.registerCallback(); }, /** * Registers a callback function. * @alias PeriodicalExecutor.registerCallback() */ registerCallback: function() { this.timer = setInterval(this.onTimerEvent.bind(this), this.frequency * 1000); }, /** * Stops execution of the timer event. * @alias PeriodicalExecutor.stop */ stop: function() { if (!this.timer) return; clearInterval(this.timer); this.timer = null; }, /** * Calls the function at on the timer event. * @alias PeriodicalExecutor.onTimerEvent */ onTimerEvent: function() { if (!this.currentlyExecuting) { try { this.currentlyExecuting = true; this.callback(this); } finally { this.currentlyExecuting = false; } } }}Object.extend(String, { interpret: function(value) { return value == null ? '' : String(value); }, specialChar: { '\b': '\\b', '\t': '\\t', '\n': '\\n', '\f': '\\f', '\r': '\\r', '\\': '\\\\' }});Object.extend(String.prototype, { /** * Returns the string with every occurence of a given pattern replaced by either a regular string, the returned value of a function or a Template string. The pattern can be a string or a regular expression. * @alias String.gsub * @param {String} pattern Pattern to replace. * @param {String} replacement String to replace the pattern with. * @return {String} Returns the string with every occurence of a given pattern replaced. * @extends {String} */ gsub: function(pattern, replacement) { var result = '', source = this, match; replacement = arguments.callee.prepareReplacement(replacement); while (source.length > 0) { if (match = source.match(pattern)) { result += source.slice(0, match.index); result += String.interpret(replacement(match)); source = source.slice(match.index + match[0].length); } else { result += source, source = ''; } } return result; }, /** * Returns a string with the first count occurrences of pattern replaced by either a regular string, the returned value of a function or a Template string. pattern can be a string or a regular expression. * @alias String.sub * @param {String} pattern Pattern to search for. * @param {String} replacement Replacement string. * @param {Number} count Number of occurrences to replace. * @return {String} Returns a string with the first count occurrences of pattern replaced by either a regular string, the returned value of a function or a Template string. * @extends {String} */ sub: function(pattern, replacement, count) { replacement = this.gsub.prepareReplacement(replacement); count = count === undefined ? 1 : count; return this.gsub(pattern, function(match) { if (--count < 0) return match[0]; return replacement(match); }); }, /** * Allows iterating over every occurrence of the given pattern (which can be a string or a regular expression). * @alias String.scan * @param {String} pattern Pattern to search for. * @param {Iterator} iterator Iterator to use. * @return {String} Returns the original string. * @extends {String} */ scan: function(pattern, iterator) { this.gsub(pattern, iterator); return this; }, /** * Truncates a string to the given length and appends a suffix to it (indicating that it is only an excerpt). * @alias String.truncate * @param {Object} length Length to truncate the string. * @param {Object} truncation Text to append to the string. * @return {String} Returns the truncated string. * @extends {String} */ truncate: function(length, truncation) { length = length || 30; truncation = truncation === undefined ? '...' : truncation; return this.length > length ? this.slice(0, length - truncation.length) + truncation : this; }, /** * Strips all leading and trailing whitespace from a string. * @alias String.strip * @return {String} Returns the string with the whitespace stripped. * @extends {String} */ strip: function() { return this.replace(/^\s+/, '').replace(/\s+$/, ''); }, /** * Removes the HTML or XML tags from a string. * @alias String.stripTags * @return {String} Returns the string with any HTML or XML tags removed. * @extends {String} */ stripTags: function() { return this.replace(/<\/?[^>]+>/gi, ''); }, /** * Removes any <script></script> blocks from a string. * @alias String.stripScripts() * @return {String} Returns the string with any <script></script> blocks removed. * @extends {String} */ stripScripts: function() { return this.replace(new RegExp(Prototype.ScriptFragment, 'img'), ''); }, /** * Extracts any <script></script> blocks from a string and returns them as an array. * @alias String.extractScripts * @return {Array} Returns an array of any <script></script> that the string contains. * @extends {String} */ extractScripts: function() { var matchAll = new RegExp(Prototype.ScriptFragment, 'img'); var matchOne = new RegExp(Prototype.ScriptFragment, 'im'); return (this.match(matchAll) || []).map(function(scriptTag) { return (scriptTag.match(matchOne) || ['', ''])[1]; }); }, /** * Evaluates the <script></script> blocks found in a string. * @alias String.evalScripts * @return {String, Number, Object, Boolean, Array} Returns the result of the script. * @extends {String} */ evalScripts: function() { return this.extractScripts().map(function(script) { return eval(script) }); },
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -