Merge -r6634:HEAD from ../branches/dom.
* Make Element#update and Element#insert work for SELECT tags in IE and Opera. [Tobie Langel]
* Make Element#insert and Element#update better handle TABLE related elements in IE and Opera. Closes #7776, #8040, #7550, #7776, #7938. [Tobie Langel]
* Make Element#readAttribute('title') work in Opera. [Tobie Langel]
* Make Element#replace work with form elements in Firefox and Safari. Closes #8010, #7989. [dsl239, Tobie Langel]
* Add Element#wrap which wraps the element inside a new one. Closes #5732. [P. Vande, Tobie Langel]
* Make Element into a constructor: new Element(tagName, attributes). Add Element#writeAttribute which accepts a hash of attributes or a name/value pair. Closes #7476. [Mislav Marohnić, haraldmartin, Tobie Langel]
* Insertion overhaul: Add Element.insert(content[, position = 'Bottom']). Deprecate Insertion (kept for backwards compatibility). Make Ajax.Updater option.insertion accept both Insertion.Top or the now preferred 'Top'. Closes #7907. [Tobie Langel]
2007-05-12 05:01:56 +00:00
|
|
|
var $break = {};
|
2007-01-18 22:24:27 +00:00
|
|
|
|
|
|
|
var Enumerable = {
|
2007-07-09 18:55:58 +00:00
|
|
|
each: function(iterator, context) {
|
2007-01-18 22:24:27 +00:00
|
|
|
var index = 0;
|
2007-07-09 18:55:58 +00:00
|
|
|
iterator = iterator.bind(context);
|
2007-01-18 22:24:27 +00:00
|
|
|
try {
|
|
|
|
this._each(function(value) {
|
2007-01-18 22:59:23 +00:00
|
|
|
iterator(value, index++);
|
2007-01-18 22:24:27 +00:00
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
if (e != $break) throw e;
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2007-07-09 18:55:58 +00:00
|
|
|
eachSlice: function(number, iterator, context) {
|
|
|
|
iterator = iterator ? iterator.bind(context) : Prototype.K;
|
2007-01-18 22:24:27 +00:00
|
|
|
var index = -number, slices = [], array = this.toArray();
|
|
|
|
while ((index += number) < array.length)
|
|
|
|
slices.push(array.slice(index, index+number));
|
2007-07-09 18:55:58 +00:00
|
|
|
return slices.collect(iterator, context);
|
2007-01-18 22:24:27 +00:00
|
|
|
},
|
2007-07-09 18:55:58 +00:00
|
|
|
|
|
|
|
all: function(iterator, context) {
|
|
|
|
iterator = iterator ? iterator.bind(context) : Prototype.K;
|
2007-01-18 22:24:27 +00:00
|
|
|
var result = true;
|
|
|
|
this.each(function(value, index) {
|
2007-07-09 18:55:58 +00:00
|
|
|
result = result && !!iterator(value, index);
|
2007-01-18 22:24:27 +00:00
|
|
|
if (!result) throw $break;
|
|
|
|
});
|
|
|
|
return result;
|
|
|
|
},
|
2007-07-09 18:55:58 +00:00
|
|
|
|
|
|
|
any: function(iterator, context) {
|
|
|
|
iterator = iterator ? iterator.bind(context) : Prototype.K;
|
2007-01-18 22:24:27 +00:00
|
|
|
var result = false;
|
|
|
|
this.each(function(value, index) {
|
2007-07-09 18:55:58 +00:00
|
|
|
if (result = !!iterator(value, index))
|
2007-01-18 22:24:27 +00:00
|
|
|
throw $break;
|
|
|
|
});
|
|
|
|
return result;
|
|
|
|
},
|
2007-07-09 18:55:58 +00:00
|
|
|
|
|
|
|
collect: function(iterator, context) {
|
|
|
|
iterator = iterator ? iterator.bind(context) : Prototype.K;
|
2007-01-18 22:24:27 +00:00
|
|
|
var results = [];
|
|
|
|
this.each(function(value, index) {
|
2007-07-09 18:55:58 +00:00
|
|
|
results.push(iterator(value, index));
|
2007-01-18 22:24:27 +00:00
|
|
|
});
|
|
|
|
return results;
|
|
|
|
},
|
|
|
|
|
2007-07-09 18:55:58 +00:00
|
|
|
detect: function(iterator, context) {
|
|
|
|
iterator = iterator.bind(context);
|
2007-01-18 22:24:27 +00:00
|
|
|
var result;
|
|
|
|
this.each(function(value, index) {
|
|
|
|
if (iterator(value, index)) {
|
|
|
|
result = value;
|
|
|
|
throw $break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return result;
|
|
|
|
},
|
|
|
|
|
2007-07-09 18:55:58 +00:00
|
|
|
findAll: function(iterator, context) {
|
|
|
|
iterator = iterator.bind(context);
|
2007-01-18 22:24:27 +00:00
|
|
|
var results = [];
|
|
|
|
this.each(function(value, index) {
|
|
|
|
if (iterator(value, index))
|
|
|
|
results.push(value);
|
|
|
|
});
|
|
|
|
return results;
|
|
|
|
},
|
|
|
|
|
2007-07-09 18:55:58 +00:00
|
|
|
grep: function(pattern, iterator, context) {
|
|
|
|
iterator = iterator ? iterator.bind(context) : Prototype.K;
|
2007-01-18 22:24:27 +00:00
|
|
|
var results = [];
|
|
|
|
this.each(function(value, index) {
|
|
|
|
var stringValue = value.toString();
|
|
|
|
if (stringValue.match(pattern))
|
2007-07-09 18:55:58 +00:00
|
|
|
results.push(iterator(value, index));
|
|
|
|
});
|
2007-01-18 22:24:27 +00:00
|
|
|
return results;
|
|
|
|
},
|
|
|
|
|
|
|
|
include: function(object) {
|
2007-07-09 18:55:58 +00:00
|
|
|
if (typeof this.indexOf == 'function')
|
|
|
|
return this.indexOf(object) != -1;
|
|
|
|
|
2007-01-18 22:24:27 +00:00
|
|
|
var found = false;
|
|
|
|
this.each(function(value) {
|
2007-07-09 18:55:58 +00:00
|
|
|
if (value === object) {
|
2007-01-18 22:24:27 +00:00
|
|
|
found = true;
|
|
|
|
throw $break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return found;
|
|
|
|
},
|
|
|
|
|
|
|
|
inGroupsOf: function(number, fillWith) {
|
|
|
|
fillWith = fillWith === undefined ? null : fillWith;
|
|
|
|
return this.eachSlice(number, function(slice) {
|
|
|
|
while(slice.length < number) slice.push(fillWith);
|
|
|
|
return slice;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2007-07-09 18:55:58 +00:00
|
|
|
inject: function(memo, iterator, context) {
|
|
|
|
iterator = iterator.bind(context);
|
2007-01-18 22:24:27 +00:00
|
|
|
this.each(function(value, index) {
|
|
|
|
memo = iterator(memo, value, index);
|
|
|
|
});
|
|
|
|
return memo;
|
|
|
|
},
|
|
|
|
|
|
|
|
invoke: function(method) {
|
|
|
|
var args = $A(arguments).slice(1);
|
|
|
|
return this.map(function(value) {
|
|
|
|
return value[method].apply(value, args);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2007-07-09 18:55:58 +00:00
|
|
|
max: function(iterator, context) {
|
|
|
|
iterator = iterator ? iterator.bind(context) : Prototype.K;
|
2007-01-18 22:24:27 +00:00
|
|
|
var result;
|
|
|
|
this.each(function(value, index) {
|
2007-07-09 18:55:58 +00:00
|
|
|
value = iterator(value, index);
|
2007-01-18 22:24:27 +00:00
|
|
|
if (result == undefined || value >= result)
|
|
|
|
result = value;
|
|
|
|
});
|
|
|
|
return result;
|
|
|
|
},
|
|
|
|
|
2007-07-09 18:55:58 +00:00
|
|
|
min: function(iterator, context) {
|
|
|
|
iterator = iterator ? iterator.bind(context) : Prototype.K;
|
2007-01-18 22:24:27 +00:00
|
|
|
var result;
|
|
|
|
this.each(function(value, index) {
|
2007-07-09 18:55:58 +00:00
|
|
|
value = iterator(value, index);
|
2007-01-18 22:24:27 +00:00
|
|
|
if (result == undefined || value < result)
|
|
|
|
result = value;
|
|
|
|
});
|
|
|
|
return result;
|
|
|
|
},
|
|
|
|
|
2007-07-09 18:55:58 +00:00
|
|
|
partition: function(iterator, context) {
|
|
|
|
iterator = iterator ? iterator.bind(context) : Prototype.K;
|
2007-01-18 22:24:27 +00:00
|
|
|
var trues = [], falses = [];
|
|
|
|
this.each(function(value, index) {
|
2007-07-09 18:55:58 +00:00
|
|
|
(iterator(value, index) ?
|
2007-01-18 22:24:27 +00:00
|
|
|
trues : falses).push(value);
|
|
|
|
});
|
|
|
|
return [trues, falses];
|
|
|
|
},
|
|
|
|
|
|
|
|
pluck: function(property) {
|
|
|
|
var results = [];
|
2007-07-09 18:55:58 +00:00
|
|
|
this.each(function(value) {
|
2007-01-18 22:24:27 +00:00
|
|
|
results.push(value[property]);
|
|
|
|
});
|
|
|
|
return results;
|
|
|
|
},
|
|
|
|
|
2007-07-09 18:55:58 +00:00
|
|
|
reject: function(iterator, context) {
|
|
|
|
iterator = iterator.bind(context);
|
2007-01-18 22:24:27 +00:00
|
|
|
var results = [];
|
|
|
|
this.each(function(value, index) {
|
|
|
|
if (!iterator(value, index))
|
|
|
|
results.push(value);
|
|
|
|
});
|
|
|
|
return results;
|
|
|
|
},
|
|
|
|
|
2007-07-09 18:55:58 +00:00
|
|
|
sortBy: function(iterator, context) {
|
|
|
|
iterator = iterator.bind(context);
|
2007-01-18 22:24:27 +00:00
|
|
|
return this.map(function(value, index) {
|
|
|
|
return {value: value, criteria: iterator(value, index)};
|
|
|
|
}).sort(function(left, right) {
|
|
|
|
var a = left.criteria, b = right.criteria;
|
|
|
|
return a < b ? -1 : a > b ? 1 : 0;
|
|
|
|
}).pluck('value');
|
|
|
|
},
|
|
|
|
|
|
|
|
toArray: function() {
|
|
|
|
return this.map();
|
|
|
|
},
|
|
|
|
|
|
|
|
zip: function() {
|
|
|
|
var iterator = Prototype.K, args = $A(arguments);
|
|
|
|
if (typeof args.last() == 'function')
|
|
|
|
iterator = args.pop();
|
|
|
|
|
|
|
|
var collections = [this].concat(args).map($A);
|
|
|
|
return this.map(function(value, index) {
|
|
|
|
return iterator(collections.pluck(index));
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
size: function() {
|
|
|
|
return this.toArray().length;
|
|
|
|
},
|
|
|
|
|
|
|
|
inspect: function() {
|
|
|
|
return '#<Enumerable:' + this.toArray().inspect() + '>';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Object.extend(Enumerable, {
|
|
|
|
map: Enumerable.collect,
|
|
|
|
find: Enumerable.detect,
|
|
|
|
select: Enumerable.findAll,
|
2007-07-09 18:55:58 +00:00
|
|
|
filter: Enumerable.findAll,
|
2007-01-18 22:24:27 +00:00
|
|
|
member: Enumerable.include,
|
2007-07-09 18:55:58 +00:00
|
|
|
entries: Enumerable.toArray,
|
|
|
|
every: Enumerable.all,
|
|
|
|
some: Enumerable.any
|
2007-01-18 22:24:27 +00:00
|
|
|
});
|