[Jifty-commit] r4331 - in
jifty/branches/prototype-1.6/share/web/static/js: .
jifty-commit at lists.jifty.org
jifty-commit at lists.jifty.org
Tue Oct 30 05:03:41 EDT 2007
Author: sunnavy
Date: Tue Oct 30 05:01:57 2007
New Revision: 4331
Modified:
jifty/branches/prototype-1.6/share/web/static/js/jifty.js
jifty/branches/prototype-1.6/share/web/static/js/prototype.js
jifty/branches/prototype-1.6/share/web/static/js/scriptaculous/effects.js
Log:
new Hash api in prototype 1.6.0
Modified: jifty/branches/prototype-1.6/share/web/static/js/jifty.js
==============================================================================
--- jifty/branches/prototype-1.6/share/web/static/js/jifty.js (original)
+++ jifty/branches/prototype-1.6/share/web/static/js/jifty.js Tue Oct 30 05:01:57 2007
@@ -519,9 +519,9 @@
getAction: function (element) {
element = $(element);
var moniker = Form.Element.getMoniker(element);
- if (!current_actions[moniker])
- current_actions[moniker] = new Action(moniker);
- return current_actions[moniker];
+ if (!current_actions.get(moniker))
+ current_actions.set(moniker, new Action(moniker));
+ return current_actions.get(moniker);
},
// Returns the name of the field
@@ -738,7 +738,7 @@
this.args = $H(args);
this.path = path;
this.parent = parent ? fragments[parent] : null;
- if (fragments[name]) {
+ if (fragments.get(name)) {
// If this fragment already existed, we want to wipe out
// whatever evil lies we might have said earlier; do this
// by clearing out everything that looks relevant
@@ -747,12 +747,12 @@
var k = keys[i];
var parsed = k.match(/^(.*?)\.(.*)/);
if ((parsed != null) && (parsed.length == 3) && (parsed[1] == this.name)) {
- delete current_args[k];
+ current_args.unset(k);
}
}
}
- fragments[name] = this;
+ fragments.set(name, this);
},
setPath: function(supplied) {
@@ -761,7 +761,7 @@
for (var i = 0; i < keys.length; i++) {
var k = keys[i];
if (k == this.name) {
- this.path = current_args[k];
+ this.path = current_args.get(k);
}
}
@@ -771,7 +771,7 @@
}
// Propagate back to current args
- current_args[this.name] = this.path;
+ current_args.set(this.name, this.path);
// Return new value
return this.path;
@@ -785,7 +785,7 @@
var k = keys[i];
var parsed = k.match(/^(.*?)\.(.*)/);
if ((parsed != null) && (parsed.length == 3) && (parsed[1] == this.name)) {
- this.args[parsed[2]] = current_args[k];
+ this.args.set(parsed[2], current_args.get(k));
}
}
@@ -796,7 +796,7 @@
keys = supplied.keys();
for (var i = 0; i < keys.length; i++) {
var k = keys[i];
- current_args[this.name+'.'+k] = supplied[k];
+ current_args.set(this.name+'.'+k, supplied[k]);
}
// Return new values
@@ -854,12 +854,12 @@
// If we're removing the element, do it now
// XXX TODO: Effects on this?
if (f['mode'] == "Delete") {
- fragments[name] = null;
+ fragments.set(name, null);
Element.remove(element);
return;
}
- f['is_new'] = (fragments[name] ? false : true);
+ f['is_new'] = (fragments.get(name) ? false : true);
// If it's new, we need to create it so we can dump it
if (f['is_new']) {
// Find what region we're inside
@@ -878,14 +878,14 @@
// Make the region (for now)
new Region(name, f['args'], f['path'], f['parent']);
- } else if ((f['path'] != null) && f['toggle'] && (f['path'] == fragments[name].path)) {
+ } else if ((f['path'] != null) && f['toggle'] && (f['path'] == fragments.get(name).path)) {
// If they set the 'toggle' flag, and clicking wouldn't change the path
Element.update(element, '');
- fragments[name].path = null;
+ fragments.get(name).path = null;
return;
} else if (f['path'] == null) {
// If they didn't know the path, fill it in now
- f['path'] == fragments[name].path;
+ f['path'] == fragments.get(name).path;
}
return f;
@@ -931,7 +931,7 @@
// - f: fragment spec
var apply_fragment_updates = function(fragment, f) {
// We found the right fragment
- var dom_fragment = fragments[f['region']];
+ var dom_fragment = fragments.get(f['region']);
var new_dom_args = $H();
var element = f['element'];
@@ -947,7 +947,7 @@
} else if (fragment_bit.firstChild) {
textContent = fragment_bit.firstChild.nodeValue;
}
- new_dom_args[fragment_bit.getAttribute("name")] = textContent;
+ new_dom_args.set(fragment_bit.getAttribute("name"), textContent);
},
content: function(fragment_bit) {
var textContent = '';
@@ -1019,7 +1019,7 @@
var disabled_elements = $A();
// Set request base path
- request['path'] = '/__jifty/webservices/xml';
+ request.set('path', '/__jifty/webservices/xml');
// Grab extra arguments (from a button)
var button_args = Form.Element.buttonFormElements(trigger);
@@ -1039,11 +1039,11 @@
optional_fragments = [ prepare_element_for_update({'mode':'Replace','args':{},'region':'__page','path': null}) ];
// Build actions structure
var has_request = 0;
- request['actions'] = $H();
+ request.set('actions', $H());
for (var moniker in named_args['actions']) {
var disable = named_args['actions'][moniker];
var a = new Action(moniker, button_args);
- current_actions[moniker] = a; // XXX: how do i make this bloody singleton?
+ current_actions.set(moniker, a); // XXX: how do i make this bloody singleton?
// Special case for Redirect, allow optional, implicit __page
// from the response to be used.
if (a.actionClass == 'Jifty::Action::Redirect')
@@ -1066,13 +1066,13 @@
fields[argname] = { value: override[argname] };
}
}
- request['actions'][moniker] = param;
+ request.get('actions')[moniker] = param;
++has_request;
}
}
- request['fragments'] = $H();
+ request.set('fragments', $H());
var update_from_cache = new Array;
// Build fragments structure
@@ -1087,7 +1087,7 @@
var content_node = document.createElement('content');
var cached_result;
- Jifty.Web.current_region = fragments[f['region']];
+ Jifty.Web.current_region = fragments.get(f['region']);
try { cached_result = apply_cached_for_action(cached['content'], []) }
catch (e) { alert(e) }
@@ -1107,7 +1107,7 @@
my_fragment.setAttribute('id', f['region']);
update_from_cache.push(function(){
var cached_result;
- Jifty.Web.current_region = fragments[f['region']];
+ Jifty.Web.current_region = fragments.get(f['region']);
try {
cached_result = apply_cached_for_action(cached['content'], Form.getActions(form));
}
@@ -1134,14 +1134,14 @@
// Update with all new values
var name = f['region'];
- var fragment_request = fragments[name].data_structure(f['path'], f['args']);
+ var fragment_request = fragments.get(name).data_structure(f['path'], f['args']);
if (f['is_new'])
// Ask for the wrapper if we are making a new region
fragment_request['wrapper'] = 1;
// Push it onto the request stack
- request['fragments'][name] = fragment_request;
+ request.get('fragments').set(name, fragment_request);
++has_request;
}
@@ -1169,7 +1169,7 @@
var text = error.textContent
? error.textContent
: (error.firstChild ? error.firstChild.nodeValue : '');
- var action = current_actions[moniker];
+ var action = current_actions.get(moniker);
action.result.field_error[field.getAttribute("name")] = text;
}
}});
@@ -1226,15 +1226,15 @@
};
// Build variable structure
- request['variables'] = $H();
+ request.set('variables', $H());
var keys = current_args.keys();
for (var i = 0; i < keys.length; i++) {
var k = keys[i];
- request['variables']['region-'+k] = current_args[k];
+ request.get('variables').set('region-'+k, current_args.get(k));
}
// Build continuation structure
- request['continuation'] = named_args['continuation'];
+ request.set('continuation', named_args['continuation']);
// Push any state variables which we set into the forms
for (var i = 0; i < document.forms.length; i++) {
@@ -1243,20 +1243,20 @@
for (var j = 0; j < keys.length; j++) {
var n = keys[j];
if (form['J:V-region-'+n]) {
- form['J:V-region-'+n].value = current_args[n];
+ form['J:V-region-'+n].value = current_args.get(n);
} else {
var hidden = document.createElement('input');
hidden.setAttribute('type', 'hidden');
hidden.setAttribute('name', 'J:V-region-'+n);
hidden.setAttribute('id', 'J:V-region-'+n);
- hidden.setAttribute('value', current_args[n]);
+ hidden.setAttribute('value', current_args.get(n));
form.appendChild(hidden);
}
}
}
// Set up our options
- var options = { postBody: JSON.stringify(request),
+ var options = { postBody: request.toJSON(), //JSON.stringify(request.toObject),
onSuccess: onSuccess,
onException: onFailure,
onFailure: onFailure,
@@ -1411,10 +1411,10 @@
getUpdatedChoices: function() {
var request = { path: this.url, actions: {} };
- var a = $H();
+ var a = {}; //$H();
a['moniker'] = 'autocomplete';
a['class'] = 'Jifty::Action::Autocomplete';
- a['fields'] = $H();
+ a['fields'] = {}; //$H();
a['fields']['moniker'] = this.action.moniker;
a['fields']['argument'] = Form.Element.getField(this.field);
request['actions']['autocomplete'] = a;
Modified: jifty/branches/prototype-1.6/share/web/static/js/prototype.js
==============================================================================
--- jifty/branches/prototype-1.6/share/web/static/js/prototype.js (original)
+++ jifty/branches/prototype-1.6/share/web/static/js/prototype.js Tue Oct 30 05:01:57 2007
@@ -974,7 +974,125 @@
$w('abs round ceil floor').each(function(method){
Number.prototype[method] = Math[method].methodize();
});
+function $H(object) {
+ return new Hash(object);
+};
+
+var Hash = Class.create(Enumerable, (function() {
+ if (function() {
+ var i = 0, Test = function(value) { this.key = value };
+ Test.prototype.key = 'foo';
+ for (var property in new Test('bar')) i++;
+ return i > 1;
+ }()) {
+ function each(iterator) {
+ var cache = [];
+ for (var key in this._object) {
+ var value = this._object[key];
+ if (cache.include(key)) continue;
+ cache.push(key);
+ var pair = [key, value];
+ pair.key = key;
+ pair.value = value;
+ iterator(pair);
+ }
+ }
+ } else {
+ function each(iterator) {
+ for (var key in this._object) {
+ var value = this._object[key], pair = [key, value];
+ pair.key = key;
+ pair.value = value;
+ iterator(pair);
+ }
+ }
+ }
+
+ function toQueryPair(key, value) {
+ if (Object.isUndefined(value)) return key;
+ return key + '=' + encodeURIComponent(String.interpret(value));
+ }
+
+ return {
+ initialize: function(object) {
+ this._object = Object.isHash(object) ? object.toObject() : Object.clone(object);
+ },
+
+ _each: each,
+
+ set: function(key, value) {
+ return this._object[key] = value;
+ },
+
+ get: function(key) {
+ return this._object[key];
+ },
+
+ unset: function(key) {
+ var value = this._object[key];
+ delete this._object[key];
+ return value;
+ },
+
+ toObject: function() {
+ return Object.clone(this._object);
+ },
+
+ keys: function() {
+ return this.pluck('key');
+ },
+
+ values: function() {
+ return this.pluck('value');
+ },
+
+ index: function(value) {
+ var match = this.detect(function(pair) {
+ return pair.value === value;
+ });
+ return match && match.key;
+ },
+
+ merge: function(object) {
+ return this.clone().update(object);
+ },
+
+ update: function(object) {
+ return new Hash(object).inject(this, function(result, pair) {
+ result.set(pair.key, pair.value);
+ return result;
+ });
+ },
+
+ toQueryString: function() {
+ return this.map(function(pair) {
+ var key = encodeURIComponent(pair.key), values = pair.value;
+
+ if (values && typeof values == 'object') {
+ if (Object.isArray(values))
+ return values.map(toQueryPair.curry(key)).join('&');
+ }
+ return toQueryPair(key, values);
+ }).join('&');
+ },
+
+ inspect: function() {
+ return '#<Hash:{' + this.map(function(pair) {
+ return pair.map(Object.inspect).join(': ');
+ }).join(', ') + '}>';
+ },
+
+ toJSON: function() {
+ return Object.toJSON(this.toObject());
+ },
+
+ clone: function() {
+ return new Hash(this);
+ }
+ }
+})());
+Hash.from = $H;
var ObjectRange = Class.create(Enumerable, {
initialize: function(start, end, exclusive) {
this.start = start;
@@ -1139,9 +1257,9 @@
var headerNames = [ 'X-Requested-With', 'X-Prototype-Version', 'Accept' ];
if (this.method == 'post') {
- headers['Content-Type'] = this.options.contentType +
- (this.options.encoding ? '; charset=' + this.options.encoding : '');
- headerNames.push('Content-Type');
+ headers['Content-Type'] = this.options.contentType +
+ (this.options.encoding ? '; charset=' + this.options.encoding : '');
+ headerNames.push('Content-Type');
/* Force "Connection: close" for older Mozilla browsers to work
* around a bug where XMLHttpRequest sends an incorrect
@@ -3856,6 +3974,8 @@
})();
/*------------------------------- DEPRECATED -------------------------------*/
+Hash.toQueryString = Object.toQueryString;
+
var Toggle = { display: Element.toggle };
Element.Methods.childOf = Element.Methods.descendantOf;
@@ -4035,113 +4155,6 @@
}
};
-var Hash = function(object) {
- if (object instanceof Hash) this.merge(object);
- else Object.extend(this, object || {});
-};
-
-Object.extend(Hash, {
- toQueryString: function(obj) {
- var parts = [];
- parts.add = arguments.callee.addPair;
-
- this.prototype._each.call(obj, function(pair) {
- if (!pair.key) return;
- var value = pair.value;
-
- if (value && typeof value == 'object') {
- if (value.constructor == Array) value.each(function(value) {
- parts.add(pair.key, value);
- });
- return;
- }
- parts.add(pair.key, value);
- });
-
- return parts.join('&');
- },
-
- toJSON: function(object) {
- var results = [];
- this.prototype._each.call(object, function(pair) {
- var value = Object.toJSON(pair.value);
- if (value !== undefined) results.push(pair.key.toJSON() + ': ' + value);
- });
- return '{' + results.join(', ') + '}';
- }
-});
-
-Hash.toQueryString.addPair = function(key, value, prefix) {
- key = encodeURIComponent(key);
- if (value === undefined) this.push(key);
- else this.push(key + '=' + (value == null ? '' : encodeURIComponent(value)));
-}
-
-Object.extend(Hash.prototype, Enumerable);
-Object.extend(Hash.prototype, {
- _each: function(iterator) {
- for (var key in this) {
- var value = this[key];
- if (value && value == Hash.prototype[key]) continue;
-
- var pair = [key, value];
- pair.key = key;
- pair.value = value;
- iterator(pair);
- }
- },
-
- keys: function() {
- return this.pluck('key');
- },
-
- values: function() {
- return this.pluck('value');
- },
-
- merge: function(hash) {
- return $H(hash).inject(this, function(mergedHash, pair) {
- mergedHash[pair.key] = pair.value;
- return mergedHash;
- });
- },
-
- remove: function() {
- var result;
- for(var i = 0, length = arguments.length; i < length; i++) {
- var value = this[arguments[i]];
- if (value !== undefined){
- if (result === undefined) result = value;
- else {
- if (result.constructor != Array) result = [result];
- result.push(value)
- }
- }
- delete this[arguments[i]];
- }
- return result;
- },
-
- toQueryString: function() {
- return Hash.toQueryString(this);
- },
-
- inspect: function() {
- return '#<Hash:{' + this.map(function(pair) {
- return pair.map(Object.inspect).join(': ');
- }).join(', ') + '}>';
- },
-
- toJSON: function() {
- return Hash.toJSON(this);
- }
-});
-
-function $H(object) {
- if (object instanceof Hash) return object;
- return new Hash(object);
-};
-
Object.extend(Element.ClassNames.prototype, Enumerable);
/*--------------------------------------------------------------------------*/
Modified: jifty/branches/prototype-1.6/share/web/static/js/scriptaculous/effects.js
==============================================================================
--- jifty/branches/prototype-1.6/share/web/static/js/scriptaculous/effects.js (original)
+++ jifty/branches/prototype-1.6/share/web/static/js/scriptaculous/effects.js Tue Oct 30 05:01:57 2007
@@ -231,10 +231,10 @@
get: function(queueName) {
if(typeof queueName != 'string') return queueName;
- if(!this.instances[queueName])
- this.instances[queueName] = new Effect.ScopedQueue();
+ if(!this.instances.get(queueName))
+ this.instances.set(queueName, new Effect.ScopedQueue());
- return this.instances[queueName];
+ return this.instances.get(queueName);
}
}
Effect.Queue = Effect.Queues.get('global');
More information about the Jifty-commit
mailing list