免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 5197 | 回复: 8
打印 上一主题 下一主题

[类] Ajax js类 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-12-10 04:44 |只看该作者 |倒序浏览
  1. /*  Prototype JavaScript framework, version 1.3.1
  2. *  (c) 2005 Sam Stephenson <sam@conio.net>
  3. *
  4. *  THIS FILE IS AUTOMATICALLY GENERATED. When sending patches, please diff
  5. *  against the source tree, available from the Prototype darcs repository.
  6. *
  7. *  Prototype is freely distributable under the terms of an MIT-style license.
  8. *
  9. *  For details, see the Prototype web site: http://prototype.conio.net/
  10. *
  11. /*--------------------------------------------------------------------------*/

  12. var Prototype = {
  13.   Version: '1.3.1',
  14.   emptyFunction: function() {}
  15. }

  16. var Class = {
  17.   create: function() {
  18.     return function() {
  19.       this.initialize.apply(this, arguments);
  20.     }
  21.   }
  22. }

  23. var Abstract = new Object();

  24. Object.extend = function(destination, source) {
  25.   for (property in source) {
  26.     destination[property] = source[property];
  27.   }
  28.   return destination;
  29. }

  30. Object.prototype.extend = function(object) {
  31.   return Object.extend.apply(this, [this, object]);
  32. }

  33. Function.prototype.bind = function(object) {
  34.   var __method = this;
  35.   return function() {
  36.     __method.apply(object, arguments);
  37.   }
  38. }

  39. Function.prototype.bindAsEventListener = function(object) {
  40.   var __method = this;
  41.   return function(event) {
  42.     __method.call(object, event || window.event);
  43.   }
  44. }

  45. Number.prototype.toColorPart = function() {
  46.   var digits = this.toString(16);
  47.   if (this < 16) return '0' + digits;
  48.   return digits;
  49. }

  50. var Try = {
  51.   these: function() {
  52.     var returnValue;

  53.     for (var i = 0; i < arguments.length; i++) {
  54.       var lambda = arguments[i];
  55.       try {
  56.         returnValue = lambda();
  57.         break;
  58.       } catch (e) {}
  59.     }

  60.     return returnValue;
  61.   }
  62. }

  63. /*--------------------------------------------------------------------------*/

  64. var PeriodicalExecuter = Class.create();
  65. PeriodicalExecuter.prototype = {
  66.   initialize: function(callback, frequency) {
  67.     this.callback = callback;
  68.     this.frequency = frequency;
  69.     this.currentlyExecuting = false;

  70.     this.registerCallback();
  71.   },

  72.   registerCallback: function() {
  73.     setInterval(this.onTimerEvent.bind(this), this.frequency * 1000);
  74.   },

  75.   onTimerEvent: function() {
  76.     if (!this.currentlyExecuting) {
  77.       try {
  78.         this.currentlyExecuting = true;
  79.         this.callback();
  80.       } finally {
  81.         this.currentlyExecuting = false;
  82.       }
  83.     }
  84.   }
  85. }

  86. /*--------------------------------------------------------------------------*/

  87. function $() {
  88.   var elements = new Array();

  89.   for (var i = 0; i < arguments.length; i++) {
  90.     var element = arguments[i];
  91.     if (typeof element == 'string')
  92.       element = document.getElementById(element);

  93.     if (arguments.length == 1)
  94.       return element;

  95.     elements.push(element);
  96.   }

  97.   return elements;
  98. }

  99. if (!Array.prototype.push) {
  100.   Array.prototype.push = function() {
  101.                 var startLength = this.length;
  102.                 for (var i = 0; i < arguments.length; i++)
  103.       this[startLength + i] = arguments[i];
  104.           return this.length;
  105.   }
  106. }

  107. if (!Function.prototype.apply) {
  108.   // Based on code from http://www.youngpup.net/
  109.   Function.prototype.apply = function(object, parameters) {
  110.     var parameterStrings = new Array();
  111.     if (!object)     object = window;
  112.     if (!parameters) parameters = new Array();
  113.    
  114.     for (var i = 0; i < parameters.length; i++)
  115.       parameterStrings[i] = 'parameters[' + i + ']';
  116.    
  117.     object.__apply__ = this;
  118.     var result = eval('object.__apply__(' +
  119.       parameterStrings.join(', ') + ')');
  120.     object.__apply__ = null;
  121.    
  122.     return result;
  123.   }
  124. }

  125. String.prototype.extend({
  126.   stripTags: function() {
  127.     return this.replace(/<\/?[^>]+>/gi, '');
  128.   },

  129.   escapeHTML: function() {
  130.     var div = document.createElement('div');
  131.     var text = document.createTextNode(this);
  132.     div.appendChild(text);
  133.     return div.innerHTML;
  134.   },

  135.   unescapeHTML: function() {
  136.     var div = document.createElement('div');
  137.     div.innerHTML = this.stripTags();
  138.     return div.childNodes[0].nodeValue;
  139.   }
  140. });

  141. var Ajax = {
  142.   getTransport: function() {
  143.     return Try.these(
  144.       function() {return new ActiveXObject('Msxml2.XMLHTTP')},
  145.       function() {return new ActiveXObject('Microsoft.XMLHTTP')},
  146.       function() {return new XMLHttpRequest()}
  147.     ) || false;
  148.   }
  149. }

  150. Ajax.Base = function() {};
  151. Ajax.Base.prototype = {
  152.   setOptions: function(options) {
  153.     this.options = {
  154.       method:       'post',
  155.       asynchronous: true,
  156.       parameters:   ''
  157.     }.extend(options || {});
  158.   },

  159.   responseIsSuccess: function() {
  160.     return this.transport.status == undefined
  161.         || this.transport.status == 0
  162.         || (this.transport.status >= 200 && this.transport.status < 300);
  163.   },

  164.   responseIsFailure: function() {
  165.     return !this.responseIsSuccess();
  166.   }
  167. }

  168. Ajax.Request = Class.create();
  169. Ajax.Request.Events =
  170.   ['Uninitialized', 'Loading', 'Loaded', 'Interactive', 'Complete'];

  171. Ajax.Request.prototype = (new Ajax.Base()).extend({
  172.   initialize: function(url, options) {
  173.     this.transport = Ajax.getTransport();
  174.     this.setOptions(options);
  175.     this.request(url);
  176.   },

  177.   request: function(url) {
  178.     var parameters = this.options.parameters || '';
  179.     if (parameters.length > 0) parameters += '&_=';

  180.     try {
  181.       if (this.options.method == 'get')
  182.         url += '?' + parameters;

  183.       this.transport.open(this.options.method, url,
  184.         this.options.asynchronous);

  185.       if (this.options.asynchronous) {
  186.         this.transport.onreadystatechange = this.onStateChange.bind(this);
  187.         setTimeout((function() {this.respondToReadyState(1)}).bind(this), 10);
  188.       }

  189.       this.setRequestHeaders();

  190.       var body = this.options.postBody ? this.options.postBody : parameters;
  191.       this.transport.send(this.options.method == 'post' ? body : null);

  192.     } catch (e) {
  193.     }
  194.   },

  195.   setRequestHeaders: function() {
  196.     var requestHeaders =
  197.       ['X-Requested-With', 'XMLHttpRequest',
  198.        'X-Prototype-Version', Prototype.Version];

  199.     if (this.options.method == 'post') {
  200.       requestHeaders.push('Content-type',
  201.         'application/x-www-form-urlencoded');

  202.       /* Force "Connection: close" for Mozilla browsers to work around
  203.        * a bug where XMLHttpReqeuest sends an incorrect Content-length
  204.        * header. See Mozilla Bugzilla #246651.
  205.        */
  206.       if (this.transport.overrideMimeType)
  207.         requestHeaders.push('Connection', 'close');
  208.     }

  209.     if (this.options.requestHeaders)
  210.       requestHeaders.push.apply(requestHeaders, this.options.requestHeaders);

  211.     for (var i = 0; i < requestHeaders.length; i += 2)
  212.       this.transport.setRequestHeader(requestHeaders[i], requestHeaders[i+1]);
  213.   },

  214.   onStateChange: function() {
  215.     var readyState = this.transport.readyState;
  216.     if (readyState != 1)
  217.       this.respondToReadyState(this.transport.readyState);
  218.   },

  219.   respondToReadyState: function(readyState) {
  220.     var event = Ajax.Request.Events[readyState];

  221.     if (event == 'Complete')
  222.       (this.options['on' + this.transport.status]
  223.        || this.options['on' + (this.responseIsSuccess() ? 'Success' : 'Failure')]
  224.        || Prototype.emptyFunction)(this.transport);

  225.     (this.options['on' + event] || Prototype.emptyFunction)(this.transport);

  226.     /* Avoid memory leak in MSIE: clean up the oncomplete event handler */
  227.     if (event == 'Complete')
  228.       this.transport.onreadystatechange = Prototype.emptyFunction;
  229.   }
  230. });

复制代码

[ 本帖最后由 HonestQiao 于 2005-12-13 14:30 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2005-12-10 04:45 |只看该作者

  1. Ajax.Updater = Class.create();
  2. Ajax.Updater.ScriptFragment = '(?:<script.*?>)((\n|.)*?)(?:<\/script>)';

  3. Ajax.Updater.prototype.extend(Ajax.Request.prototype).extend({
  4.   initialize: function(container, url, options) {
  5.     this.containers = {
  6.       success: container.success ? $(container.success) : $(container),
  7.       failure: container.failure ? $(container.failure) :
  8.         (container.success ? null : $(container))
  9.     }

  10.     this.transport = Ajax.getTransport();
  11.     this.setOptions(options);

  12.     var onComplete = this.options.onComplete || Prototype.emptyFunction;
  13.     this.options.onComplete = (function() {
  14.       this.updateContent();
  15.       onComplete(this.transport);
  16.     }).bind(this);

  17.     this.request(url);
  18.   },

  19.   updateContent: function() {
  20.     var receiver = this.responseIsSuccess() ?
  21.       this.containers.success : this.containers.failure;

  22.     var match    = new RegExp(Ajax.Updater.ScriptFragment, 'img');
  23.     var response = this.transport.responseText.replace(match, '');
  24.     var scripts  = this.transport.responseText.match(match);

  25.     if (receiver) {
  26.       if (this.options.insertion) {
  27.         new this.options.insertion(receiver, response);
  28.       } else {
  29.         receiver.innerHTML = response;
  30.       }
  31.     }

  32.     if (this.responseIsSuccess()) {
  33.       if (this.onComplete)
  34.         setTimeout((function() {this.onComplete(
  35.           this.transport)}).bind(this), 10);
  36.     }

  37.     if (this.options.evalScripts && scripts) {
  38.       match = new RegExp(Ajax.Updater.ScriptFragment, 'im');
  39.       setTimeout((function() {
  40.         for (var i = 0; i < scripts.length; i++)
  41.           eval(scripts[i].match(match)[1]);
  42.       }).bind(this), 10);
  43.     }
  44.   }
  45. });

  46. Ajax.PeriodicalUpdater = Class.create();
  47. Ajax.PeriodicalUpdater.prototype = (new Ajax.Base()).extend({
  48.   initialize: function(container, url, options) {
  49.     this.setOptions(options);
  50.     this.onComplete = this.options.onComplete;

  51.     this.frequency = (this.options.frequency || 2);
  52.     this.decay = 1;

  53.     this.updater = {};
  54.     this.container = container;
  55.     this.url = url;

  56.     this.start();
  57.   },

  58.   start: function() {
  59.     this.options.onComplete = this.updateComplete.bind(this);
  60.     this.onTimerEvent();
  61.   },

  62.   stop: function() {
  63.     this.updater.onComplete = undefined;
  64.     clearTimeout(this.timer);
  65.     (this.onComplete || Ajax.emptyFunction).apply(this, arguments);
  66.   },

  67.   updateComplete: function(request) {
  68.     if (this.options.decay) {
  69.       this.decay = (request.responseText == this.lastText ?
  70.         this.decay * this.options.decay : 1);

  71.       this.lastText = request.responseText;
  72.     }
  73.     this.timer = setTimeout(this.onTimerEvent.bind(this),
  74.       this.decay * this.frequency * 1000);
  75.   },

  76.   onTimerEvent: function() {
  77.     this.updater = new Ajax.Updater(this.container, this.url, this.options);
  78.   }
  79. });

  80. document.getElementsByClassName = function(className) {
  81.   var children = document.getElementsByTagName('*') || document.all;
  82.   var elements = new Array();
  83.   
  84.   for (var i = 0; i < children.length; i++) {
  85.     var child = children[i];
  86.     var classNames = child.className.split(' ');
  87.     for (var j = 0; j < classNames.length; j++) {
  88.       if (classNames[j] == className) {
  89.         elements.push(child);
  90.         break;
  91.       }
  92.     }
  93.   }
  94.   
  95.   return elements;
  96. }

  97. /*--------------------------------------------------------------------------*/

  98. if (!window.Element) {
  99.   var Element = new Object();
  100. }

  101. Object.extend(Element, {
  102.   toggle: function() {
  103.     for (var i = 0; i < arguments.length; i++) {
  104.       var element = $(arguments[i]);
  105.       element.style.display =
  106.         (element.style.display == 'none' ? '' : 'none');
  107.     }
  108.   },

  109.   hide: function() {
  110.     for (var i = 0; i < arguments.length; i++) {
  111.       var element = $(arguments[i]);
  112.       element.style.display = 'none';
  113.     }
  114.   },

  115.   show: function() {
  116.     for (var i = 0; i < arguments.length; i++) {
  117.       var element = $(arguments[i]);
  118.       element.style.display = '';
  119.     }
  120.   },

  121.   remove: function(element) {
  122.     element = $(element);
  123.     element.parentNode.removeChild(element);
  124.   },
  125.    
  126.   getHeight: function(element) {
  127.     element = $(element);
  128.     return element.offsetHeight;
  129.   },

  130.   hasClassName: function(element, className) {
  131.     element = $(element);
  132.     if (!element)
  133.       return;
  134.     var a = element.className.split(' ');
  135.     for (var i = 0; i < a.length; i++) {
  136.       if (a[i] == className)
  137.         return true;
  138.     }
  139.     return false;
  140.   },

  141.   addClassName: function(element, className) {
  142.     element = $(element);
  143.     Element.removeClassName(element, className);
  144.     element.className += ' ' + className;
  145.   },

  146.   removeClassName: function(element, className) {
  147.     element = $(element);
  148.     if (!element)
  149.       return;
  150.     var newClassName = '';
  151.     var a = element.className.split(' ');
  152.     for (var i = 0; i < a.length; i++) {
  153.       if (a[i] != className) {
  154.         if (i > 0)
  155.           newClassName += ' ';
  156.         newClassName += a[i];
  157.       }
  158.     }
  159.     element.className = newClassName;
  160.   },
  161.   
  162.   // removes whitespace-only text node children
  163.   cleanWhitespace: function(element) {
  164.     var element = $(element);
  165.     for (var i = 0; i < element.childNodes.length; i++) {
  166.       var node = element.childNodes[i];
  167.       if (node.nodeType == 3 && !/\S/.test(node.nodeValue))
  168.         Element.remove(node);
  169.     }
  170.   }
  171. });

  172. var Toggle = new Object();
  173. Toggle.display = Element.toggle;

  174. /*--------------------------------------------------------------------------*/

  175. Abstract.Insertion = function(adjacency) {
  176.   this.adjacency = adjacency;
  177. }

  178. Abstract.Insertion.prototype = {
  179.   initialize: function(element, content) {
  180.     this.element = $(element);
  181.     this.content = content;
  182.    
  183.     if (this.adjacency && this.element.insertAdjacentHTML) {
  184.       this.element.insertAdjacentHTML(this.adjacency, this.content);
  185.     } else {
  186.       this.range = this.element.ownerDocument.createRange();
  187.       if (this.initializeRange) this.initializeRange();
  188.       this.fragment = this.range.createContextualFragment(this.content);
  189.       this.insertContent();
  190.     }
  191.   }
  192. }

  193. var Insertion = new Object();

  194. Insertion.Before = Class.create();
  195. Insertion.Before.prototype = (new Abstract.Insertion('beforeBegin')).extend({
  196.   initializeRange: function() {
  197.     this.range.setStartBefore(this.element);
  198.   },
  199.   
  200.   insertContent: function() {
  201.     this.element.parentNode.insertBefore(this.fragment, this.element);
  202.   }
  203. });

  204. Insertion.Top = Class.create();
  205. Insertion.Top.prototype = (new Abstract.Insertion('afterBegin')).extend({
  206.   initializeRange: function() {
  207.     this.range.selectNodeContents(this.element);
  208.     this.range.collapse(true);
  209.   },
  210.   
  211.   insertContent: function() {  
  212.     this.element.insertBefore(this.fragment, this.element.firstChild);
  213.   }
  214. });

  215. Insertion.Bottom = Class.create();
  216. Insertion.Bottom.prototype = (new Abstract.Insertion('beforeEnd')).extend({
  217.   initializeRange: function() {
  218.     this.range.selectNodeContents(this.element);
  219.     this.range.collapse(this.element);
  220.   },
  221.   
  222.   insertContent: function() {
  223.     this.element.appendChild(this.fragment);
  224.   }
  225. });

  226. Insertion.After = Class.create();
  227. Insertion.After.prototype = (new Abstract.Insertion('afterEnd')).extend({
  228.   initializeRange: function() {
  229.     this.range.setStartAfter(this.element);
  230.   },
  231.   
  232.   insertContent: function() {
  233.     this.element.parentNode.insertBefore(this.fragment,
  234.       this.element.nextSibling);
  235.   }
  236. });

  237. var Field = {
  238.   clear: function() {
  239.     for (var i = 0; i < arguments.length; i++)
  240.       $(arguments[i]).value = '';
  241.   },

  242.   focus: function(element) {
  243.     $(element).focus();
  244.   },
  245.   
  246.   present: function() {
  247.     for (var i = 0; i < arguments.length; i++)
  248.       if ($(arguments[i]).value == '') return false;
  249.     return true;
  250.   },
  251.   
  252.   select: function(element) {
  253.     $(element).select();
  254.   },
  255.    
  256.   activate: function(element) {
  257.     $(element).focus();
  258.     $(element).select();
  259.   }
  260. }

  261. /*--------------------------------------------------------------------------*/

  262. var Form = {
  263.   serialize: function(form) {
  264.     var elements = Form.getElements($(form));
  265.     var queryComponents = new Array();
  266.    
  267.     for (var i = 0; i < elements.length; i++) {
  268.       var queryComponent = Form.Element.serialize(elements[i]);
  269.       if (queryComponent)
  270.         queryComponents.push(queryComponent);
  271.     }
  272.    
  273.     return queryComponents.join('&');
  274.   },
  275.   
  276.   getElements: function(form) {
  277.     var form = $(form);
  278.     var elements = new Array();

  279.     for (tagName in Form.Element.Serializers) {
  280.       var tagElements = form.getElementsByTagName(tagName);
  281.       for (var j = 0; j < tagElements.length; j++)
  282.         elements.push(tagElements[j]);
  283.     }
  284.     return elements;
  285.   },
  286.   
  287.   getInputs: function(form, typeName, name) {
  288.     var form = $(form);
  289.     var inputs = form.getElementsByTagName('input');
  290.    
  291.     if (!typeName && !name)
  292.       return inputs;
  293.       
  294.     var matchingInputs = new Array();
  295.     for (var i = 0; i < inputs.length; i++) {
  296.       var input = inputs[i];
  297.       if ((typeName && input.type != typeName) ||
  298.           (name && input.name != name))
  299.         continue;
  300.       matchingInputs.push(input);
  301.     }

  302.     return matchingInputs;
  303.   },

  304.   disable: function(form) {
  305.     var elements = Form.getElements(form);
  306.     for (var i = 0; i < elements.length; i++) {
  307.       var element = elements[i];
  308.       element.blur();
  309.       element.disabled = 'true';
  310.     }
  311.   },

  312.   enable: function(form) {
  313.     var elements = Form.getElements(form);
  314.     for (var i = 0; i < elements.length; i++) {
  315.       var element = elements[i];
  316.       element.disabled = '';
  317.     }
  318.   },


复制代码

论坛徽章:
0
3 [报告]
发表于 2005-12-10 04:46 |只看该作者

  1.   focusFirstElement: function(form) {
  2.     var form = $(form);
  3.     var elements = Form.getElements(form);
  4.     for (var i = 0; i < elements.length; i++) {
  5.       var element = elements[i];
  6.       if (element.type != 'hidden' && !element.disabled) {
  7.         Field.activate(element);
  8.         break;
  9.       }
  10.     }
  11.   },

  12.   reset: function(form) {
  13.     $(form).reset();
  14.   }
  15. }

  16. Form.Element = {
  17.   serialize: function(element) {
  18.     var element = $(element);
  19.     var method = element.tagName.toLowerCase();
  20.     var parameter = Form.Element.Serializers[method](element);
  21.    
  22.     if (parameter)
  23.       return encodeURIComponent(parameter[0]) + '=' +
  24.         encodeURIComponent(parameter[1]);                  
  25.   },
  26.   
  27.   getValue: function(element) {
  28.     var element = $(element);
  29.     var method = element.tagName.toLowerCase();
  30.     var parameter = Form.Element.Serializers[method](element);
  31.    
  32.     if (parameter)
  33.       return parameter[1];
  34.   }
  35. }

  36. Form.Element.Serializers = {
  37.   input: function(element) {
  38.     switch (element.type.toLowerCase()) {
  39.       case 'submit':
  40.       case 'hidden':
  41.       case 'password':
  42.       case 'text':
  43.         return Form.Element.Serializers.textarea(element);
  44.       case 'checkbox':  
  45.       case 'radio':
  46.         return Form.Element.Serializers.inputSelector(element);
  47.     }
  48.     return false;
  49.   },

  50.   inputSelector: function(element) {
  51.     if (element.checked)
  52.       return [element.name, element.value];
  53.   },

  54.   textarea: function(element) {
  55.     return [element.name, element.value];
  56.   },

  57.   select: function(element) {
  58.     var value = '';
  59.     if (element.type == 'select-one') {
  60.       var index = element.selectedIndex;
  61.       if (index >= 0)
  62.         value = element.options[index].value || element.options[index].text;
  63.     } else {
  64.       value = new Array();
  65.       for (var i = 0; i < element.length; i++) {
  66.         var opt = element.options[i];
  67.         if (opt.selected)
  68.           value.push(opt.value || opt.text);
  69.       }
  70.     }
  71.     return [element.name, value];
  72.   }
  73. }

  74. /*--------------------------------------------------------------------------*/

  75. var $F = Form.Element.getValue;

  76. /*--------------------------------------------------------------------------*/

  77. Abstract.TimedObserver = function() {}
  78. Abstract.TimedObserver.prototype = {
  79.   initialize: function(element, frequency, callback) {
  80.     this.frequency = frequency;
  81.     this.element   = $(element);
  82.     this.callback  = callback;
  83.    
  84.     this.lastValue = this.getValue();
  85.     this.registerCallback();
  86.   },
  87.   
  88.   registerCallback: function() {
  89.     setInterval(this.onTimerEvent.bind(this), this.frequency * 1000);
  90.   },
  91.   
  92.   onTimerEvent: function() {
  93.     var value = this.getValue();
  94.     if (this.lastValue != value) {
  95.       this.callback(this.element, value);
  96.       this.lastValue = value;
  97.     }
  98.   }
  99. }

  100. Form.Element.Observer = Class.create();
  101. Form.Element.Observer.prototype = (new Abstract.TimedObserver()).extend({
  102.   getValue: function() {
  103.     return Form.Element.getValue(this.element);
  104.   }
  105. });

  106. Form.Observer = Class.create();
  107. Form.Observer.prototype = (new Abstract.TimedObserver()).extend({
  108.   getValue: function() {
  109.     return Form.serialize(this.element);
  110.   }
  111. });

  112. /*--------------------------------------------------------------------------*/

  113. Abstract.EventObserver = function() {}
  114. Abstract.EventObserver.prototype = {
  115.   initialize: function(element, callback) {
  116.     this.element  = $(element);
  117.     this.callback = callback;
  118.    
  119.     this.lastValue = this.getValue();
  120.     if (this.element.tagName.toLowerCase() == 'form')
  121.       this.registerFormCallbacks();
  122.     else
  123.       this.registerCallback(this.element);
  124.   },
  125.   
  126.   onElementEvent: function() {
  127.     var value = this.getValue();
  128.     if (this.lastValue != value) {
  129.       this.callback(this.element, value);
  130.       this.lastValue = value;
  131.     }
  132.   },
  133.   
  134.   registerFormCallbacks: function() {
  135.     var elements = Form.getElements(this.element);
  136.     for (var i = 0; i < elements.length; i++)
  137.       this.registerCallback(elements[i]);
  138.   },
  139.   
  140.   registerCallback: function(element) {
  141.     if (element.type) {
  142.       switch (element.type.toLowerCase()) {
  143.         case 'checkbox':  
  144.         case 'radio':
  145.           element.target = this;
  146.           element.prev_onclick = element.onclick || Prototype.emptyFunction;
  147.           element.onclick = function() {
  148.             this.prev_onclick();
  149.             this.target.onElementEvent();
  150.           }
  151.           break;
  152.         case 'password':
  153.         case 'text':
  154.         case 'textarea':
  155.         case 'select-one':
  156.         case 'select-multiple':
  157.           element.target = this;
  158.           element.prev_onchange = element.onchange || Prototype.emptyFunction;
  159.           element.onchange = function() {
  160.             this.prev_onchange();
  161.             this.target.onElementEvent();
  162.           }
  163.           break;
  164.       }
  165.     }   
  166.   }
  167. }

  168. Form.Element.EventObserver = Class.create();
  169. Form.Element.EventObserver.prototype = (new Abstract.EventObserver()).extend({
  170.   getValue: function() {
  171.     return Form.Element.getValue(this.element);
  172.   }
  173. });

  174. Form.EventObserver = Class.create();
  175. Form.EventObserver.prototype = (new Abstract.EventObserver()).extend({
  176.   getValue: function() {
  177.     return Form.serialize(this.element);
  178.   }
  179. });


  180. if (!window.Event) {
  181.   var Event = new Object();
  182. }

  183. Object.extend(Event, {
  184.   KEY_BACKSPACE: 8,
  185.   KEY_TAB:       9,
  186.   KEY_RETURN:   13,
  187.   KEY_ESC:      27,
  188.   KEY_LEFT:     37,
  189.   KEY_UP:       38,
  190.   KEY_RIGHT:    39,
  191.   KEY_DOWN:     40,
  192.   KEY_DELETE:   46,

  193.   element: function(event) {
  194.     return event.target || event.srcElement;
  195.   },

  196.   isLeftClick: function(event) {
  197.     return (((event.which) && (event.which == 1)) ||
  198.             ((event.button) && (event.button == 1)));
  199.   },

  200.   pointerX: function(event) {
  201.     return event.pageX || (event.clientX +
  202.       (document.documentElement.scrollLeft || document.body.scrollLeft));
  203.   },

  204.   pointerY: function(event) {
  205.     return event.pageY || (event.clientY +
  206.       (document.documentElement.scrollTop || document.body.scrollTop));
  207.   },

  208.   stop: function(event) {
  209.     if (event.preventDefault) {
  210.       event.preventDefault();
  211.       event.stopPropagation();
  212.     } else {
  213.       event.returnValue = false;
  214.     }
  215.   },

  216.   // find the first node with the given tagName, starting from the
  217.   // node the event was triggered on; traverses the DOM upwards
  218.   findElement: function(event, tagName) {
  219.     var element = Event.element(event);
  220.     while (element.parentNode && (!element.tagName ||
  221.         (element.tagName.toUpperCase() != tagName.toUpperCase())))
  222.       element = element.parentNode;
  223.     return element;
  224.   },

  225.   observers: false,
  226.   
  227.   _observeAndCache: function(element, name, observer, useCapture) {
  228.     if (!this.observers) this.observers = [];
  229.     if (element.addEventListener) {
  230.       this.observers.push([element, name, observer, useCapture]);
  231.       element.addEventListener(name, observer, useCapture);
  232.     } else if (element.attachEvent) {
  233.       this.observers.push([element, name, observer, useCapture]);
  234.       element.attachEvent('on' + name, observer);
  235.     }
  236.   },
  237.   
  238.   unloadCache: function() {
  239.     if (!Event.observers) return;
  240.     for (var i = 0; i < Event.observers.length; i++) {
  241.       Event.stopObserving.apply(this, Event.observers[i]);
  242.       Event.observers[i][0] = null;
  243.     }
  244.     Event.observers = false;
  245.   },

  246.   observe: function(element, name, observer, useCapture) {
  247.     var element = $(element);
  248.     useCapture = useCapture || false;
  249.    
  250.     if (name == 'keypress' &&
  251.         ((navigator.appVersion.indexOf('AppleWebKit') > 0)
  252.         || element.attachEvent))
  253.       name = 'keydown';
  254.    
  255.     this._observeAndCache(element, name, observer, useCapture);
  256.   },

  257.   stopObserving: function(element, name, observer, useCapture) {
  258.     var element = $(element);
  259.     useCapture = useCapture || false;
  260.    
  261.     if (name == 'keypress' &&
  262.         ((navigator.appVersion.indexOf('AppleWebKit') > 0)
  263.         || element.detachEvent))
  264.       name = 'keydown';
  265.    
  266.     if (element.removeEventListener) {
  267.       element.removeEventListener(name, observer, useCapture);
  268.     } else if (element.detachEvent) {
  269.       element.detachEvent('on' + name, observer);
  270.     }
  271.   }
  272. });

  273. /* prevent memory leaks in IE */
  274. Event.observe(window, 'unload', Event.unloadCache, false);

  275. var Position = {

  276.   // set to true if needed, warning: firefox performance problems
  277.   // NOT neeeded for page scrolling, only if draggable contained in
  278.   // scrollable elements
  279.   includeScrollOffsets: false,

  280.   // must be called before calling withinIncludingScrolloffset, every time the
  281.   // page is scrolled
  282.   prepare: function() {
  283.     this.deltaX =  window.pageXOffset
  284.                 || document.documentElement.scrollLeft
  285.                 || document.body.scrollLeft
  286.                 || 0;
  287.     this.deltaY =  window.pageYOffset
  288.                 || document.documentElement.scrollTop
  289.                 || document.body.scrollTop
  290.                 || 0;
  291.   },
复制代码

[ 本帖最后由 csona 于 2005-12-10 04:47 编辑 ]

论坛徽章:
0
4 [报告]
发表于 2005-12-10 04:46 |只看该作者

  1.   realOffset: function(element) {
  2.     var valueT = 0, valueL = 0;
  3.     do {
  4.       valueT += element.scrollTop  || 0;
  5.       valueL += element.scrollLeft || 0;
  6.       element = element.parentNode;
  7.     } while (element);
  8.     return [valueL, valueT];
  9.   },

  10.   cumulativeOffset: function(element) {
  11.     var valueT = 0, valueL = 0;
  12.     do {
  13.       valueT += element.offsetTop  || 0;
  14.       valueL += element.offsetLeft || 0;
  15.       element = element.offsetParent;
  16.     } while (element);
  17.     return [valueL, valueT];
  18.   },

  19.   // caches x/y coordinate pair to use with overlap
  20.   within: function(element, x, y) {
  21.     if (this.includeScrollOffsets)
  22.       return this.withinIncludingScrolloffsets(element, x, y);
  23.     this.xcomp = x;
  24.     this.ycomp = y;
  25.     this.offset = this.cumulativeOffset(element);

  26.     return (y >= this.offset[1] &&
  27.             y <  this.offset[1] + element.offsetHeight &&
  28.             x >= this.offset[0] &&
  29.             x <  this.offset[0] + element.offsetWidth);
  30.   },

  31.   withinIncludingScrolloffsets: function(element, x, y) {
  32.     var offsetcache = this.realOffset(element);

  33.     this.xcomp = x + offsetcache[0] - this.deltaX;
  34.     this.ycomp = y + offsetcache[1] - this.deltaY;
  35.     this.offset = this.cumulativeOffset(element);

  36.     return (this.ycomp >= this.offset[1] &&
  37.             this.ycomp <  this.offset[1] + element.offsetHeight &&
  38.             this.xcomp >= this.offset[0] &&
  39.             this.xcomp <  this.offset[0] + element.offsetWidth);
  40.   },

  41.   // within must be called directly before
  42.   overlap: function(mode, element) {  
  43.     if (!mode) return 0;  
  44.     if (mode == 'vertical')
  45.       return ((this.offset[1] + element.offsetHeight) - this.ycomp) /
  46.         element.offsetHeight;
  47.     if (mode == 'horizontal')
  48.       return ((this.offset[0] + element.offsetWidth) - this.xcomp) /
  49.         element.offsetWidth;
  50.   },

  51.   clone: function(source, target) {
  52.     source = $(source);
  53.     target = $(target);
  54.     target.style.position = 'absolute';
  55.     var offsets = this.cumulativeOffset(source);
  56.     target.style.top    = offsets[1] + 'px';
  57.     target.style.left   = offsets[0] + 'px';
  58.     target.style.width  = source.offsetWidth + 'px';
  59.     target.style.height = source.offsetHeight + 'px';
  60.   }
  61. }
复制代码

论坛徽章:
1
技术图书徽章
日期:2013-12-05 23:25:45
5 [报告]
发表于 2005-12-10 12:16 |只看该作者
没有说明没有用处的哦

论坛徽章:
0
6 [报告]
发表于 2005-12-10 13:03 |只看该作者
楼主,如何用不知道,给个详细教程来.
另外,你代码这么长,打个包当附件吧.
代码一点注释也没有

论坛徽章:
0
7 [报告]
发表于 2005-12-10 13:44 |只看该作者
prototype.js 源码解读v1.3.1版本
http://openbsd.wordpress.com/200 ... %e7%89%88%e6%9c%ac/

论坛徽章:
0
8 [报告]
发表于 2005-12-10 15:57 |只看该作者
给个实例吧:http://instantdomainsearch.com/

这是我们公司的一个小项目

论坛徽章:
0
9 [报告]
发表于 2005-12-10 15:57 |只看该作者

回复 6楼 gydoesit 的帖子

我去作者的网站看看,有没有教程
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP