MooTools 1.2 Custom Backwards-Compatibility Library
| In Brief | A backwards compatibility library for Mootools 1.2, in addition to those provided by MooTools. Should fix most remaining backwards-compatibility problems.... more |
| Language | JavaScript |
# 's
1/*
2MooTools 1.2 Custom Backwards-Compatibility Library
3By David Isaacson
4Portions from Mootools 1.2 by the MooTools production team (http://mootools.net/developers/)
5Copyright (c) 2006-2007 Valerio Proietti (http://mad4milk.net/)
6Copyright (c) 2008 Siafoo.net
7
8Load after Mootools Core and More and both compatibility files
9*/
10
11// This is the definition from Mootools 1.2, with error handling
12// to prevent an issue in IE where calling .item on an XML (non-HTML)
13// element raises an error.
14//
15// We're using the 1.2 version in the first place because the compat version throws *other* weird errors sometimes
16// Note that this will prevent you from using the $A(iterable, start, length) syntax allowed but undocumented (?) in Mootools 1.1
17function $A(iterable){
18 var item
19 try{
20 item = iterable.item
21 }
22 catch(e){
23 item = true
24 }
25
26 if (item){
27 var array = [];
28 for (var i = 0, l = iterable.length; i < l; i++) array[i] = iterable[i];
29 return array;
30 }
31 return Array.prototype.slice.call(iterable);
32}
33
34
35function $extend(original, extended){
36 if (!extended) {extended=original; original=this;} // This line added
37 for (var key in (extended || {})) original[key] = extended[key];
38 return original;
39}
40
41Drag.Base = Drag
42
43Element.implement({
44
45 getValue: function(){
46 return this.get('value')
47 },
48
49 // Very slightly modified from mootools
50 toQueryString: function(){
51 var queryString = [];
52 this.getElements('input, select, textarea').each(function(el){
53 if (!el.name || el.disabled) return;
54 var value = (el.tagName.toLowerCase() == 'select') ? Element.getSelected(el).map(function(opt){
55 return opt.value;
56 }) : ((el.type == 'radio' || el.type == 'checkbox') && !el.checked) ? null : el.value;
57 $splat(value).each(function(val){
58 /*if (val)*/ queryString.push(el.name + '=' + encodeURIComponent(val));
59 });
60 });
61 return queryString.join('&');
62 }
63})
64
65Elements.implement({
66 // I would actually consider this a bug
67 // Also I'm sure there's a more consistant way than this to implement it
68 empty: function(){
69 this.each(function(element){
70 element.empty()
71 })
72 },
73
74 remove: function(){
75 this.each(function(element){
76 element.remove()
77 })
78 }
79})
80
81// Class.prototype.extend in the compat library doesn't actually work for some reason. So...
82// Note that this will hide Function.extend from use in certain cases
83Function.prototype.extend = function(properties){
84 if (this.prototype){
85 // Assume its a class
86 properties.Extends = this;
87 return new Class(properties);
88 }
89 for (var property in properties) this[property] = properties[property];
90 return this;
91}
92
93Hash.implement({
94 remove: function(key){
95 return this.erase(key)
96 }
97})
98
99Hash.Cookie.implement({
100
101 remove: function(key){
102 var value = this.hash.erase(key)
103 if (this.options.autoSave) this.save();
104 return value
105 }
106})
107
108
109// Completely broken in mootools-core-compat.js
110XHR.implement({
111
112 initialize: function(options){
113 this.parent(options)
114 this.transport = this.xhr
115 }
116})
117
118var Ajax = new Class({
119 Extends: XHR,
120
121 initialize: function(url, options){
122 this.url = url
123 this.parent(options)
124 },
125
126 success: function(text, xml){
127 // This version processes scripts *after* the update element is updated, like Mootools 1.1's Ajax class
128 // Partially from Remote.Ajax.success
129 response = this.response;
130 response.html = text.stripScripts(function(script){
131 response.javascript = script;
132 })
133 if (this.options.update) $(this.options.update).empty().set('html', response.html);
134 if (this.options.evalScripts) $exec(response.javascript);
135 this.onSuccess(text, xml);
136 }
137})
138
139/* For further information, read http://www.siafoo.net/article/62 */
A backwards compatibility library for Mootools 1.2, in addition to those provided by MooTools. Should fix most remaining backwards-compatibility problems.
Load it after all four Core and More base files and compatibility files.
For more information about upgrading to Mootools 1.2, including a list of backwards-compatibility problems that this doesn't solve and their solutions, read Upgrading to MooTools 1.2 - Tips, Tricks, and Backwards Compatibility
Changelog
2009-03-23: AJAX class: scripts are now only processed once, after the element has been updated
Add a Comment