Alien-GvaScript
view release on metacpan or search on metacpan
lib/Alien/GvaScript/lib/GvaScript.js view on Meta::CPAN
(typeof Element.Methods=='undefined') ||
(convertVersionString(Prototype.Version) <
convertVersionString(GvaScript.REQUIRED_PROTOTYPE)))
throw("GvaScript requires the Prototype JavaScript framework >= " +
GvaScript.REQUIRED_PROTOTYPE);
}
};
GvaScript.load();
//----------protoExtensions.js
//-----------------------------------------------------
// Some extensions to the prototype javascript framework
//-----------------------------------------------------
// fire value:change event when setValue method
// is used to change the value of a Form Element
Form.Element.Methods.setValue = Form.Element.Methods.setValue.wrap(
function($p, element, value) {
var oldvalue = $F(element);
var _return = $p(element, value);
element.fire('value:change', {oldvalue: oldvalue, newvalue: value});
return _return;
}
);
Element.addMethods();
// adds the method flash to SPAN, DIV, INPUT, BUTTON elements
// flashes an element by adding a classname for a brief moment of time
// options: {classname: // classname to add (default: flash)
// duration: // duration in ms to keep the classname (default: 100ms)}
var _element_list = ['DIV', 'INPUT',
'BUTTON', 'TEXTAREA', 'A',
'H1', 'H2', 'H3', 'H4', 'H5'];
// for the moment, SPAN not supported on WebKit (see prototype.js bug in
// https://prototype.lighthouseapp.com/projects/8886/tickets/976-elementaddmethodsspan-fails-on-webkit)
if (!Prototype.Browser.WebKit) _element_list.push('SPAN');
Element.addMethods(_element_list, {
flash: function(element, options) {
if (element._IS_FLASHING) return;
element = $(element);
options = options || {};
var duration = options.duration || 100;
var classname = options.classname || 'flash';
element._IS_FLASHING = true;
var endFlash = function() {
this.removeClassName(classname);
this._IS_FLASHING = false;
};
element.addClassName(classname);
setTimeout(endFlash.bind(element), duration);
}
});
// utilities for hash
// expands flat hash into a multi-level deep hash
// javascript version of Perl CGI::Expand::expand_hash
Hash.expand = function(flat_hash) {
var tree = {};
// iterate on keys in the flat hash
for (var k in flat_hash) {
var parts = k.split(/\./);
var loop = {tree: tree, key: "root"};
// iterate on path parts within the key
for (var i = 0 ; i < parts.length; i++) {
var part = parts[i];
// if no subtree yet, build it (Array or Object)
if (!loop.tree[loop.key])
loop.tree[loop.key] = part.match(/^\d+$/) ? [] : {};
// walk down to subtree
loop = {tree: loop.tree[loop.key], key:part};
}
// store value in leaf
loop.tree[loop.key] = flat_hash[k];
}
return tree.root;
}
// collapses deep hash into a one level hash
Hash.flatten = function(deep_hash, prefix, tree) {
tree = tree || {};
for (var i in deep_hash) {
var v = deep_hash[i];
var new_prefix = prefix? prefix + '.' + i : i;
switch (typeof(v)) {
case "function": continue; break;
case "object" : Hash.flatten(v, new_prefix, tree); break;
case "string" :
case "number" : tree["" + new_prefix + ""] = v; break;
default : break;
}
}
return tree;
}
// utilities for string
Object.extend(String.prototype, {
chomp: function() {
return this.replace(/(\n|\r)+$/, '');
}
});
Object.extend(Element, {
classRegExp : function(wanted_classes) {
if (typeof wanted_classes != "string" &&
wanted_classes instanceof Array)
wanted_classes = wanted_classes.join("|");
return new RegExp("\\b(" + wanted_classes + ")\\b");
},
lib/Alien/GvaScript/lib/GvaScript.js view on Meta::CPAN
// take this elem out of the DOM and into a string ...
{
// a) force the id that will be needed in the template)
element.id = "#{" + repeat.name + ".path}";
// b) remove "repeat*" attributes (don't want them in the template)
var attrs = element.attributes;
var repeat_attrs = [];
for (var i = 0; i < attrs.length; i++) {
var name = attrs[i].name;
if (name.match(/^repeat/i)) repeat_attrs.push(name);
}
repeat_attrs.each(function(name){element.removeAttribute(name, 0)});
// c) keep it as a template string and remove from DOM
repeat.template = Element.outerHTML(element);
element.remove();
}
// store all properties within the placeholder
placeholder.repeat = repeat;
// create initial repetition blocks
this.add(placeholder, n_blocks);
}
};
//----------form.js
/* TODO
- submit attrs on buttons
- action / method / enctype / replace / target / novalidate
- after_submit:
- 204 NO CONTENT : leave doc, apply metadata
- 205 RESET CONTENT : reset form
- replace="document" (new page)
- replace="values" (fill form with new tree)
- relace element
- others ?
- "onreceive" event (response after submit)
- check prototype.js serialize on multivalues
*/
GvaScript.Form = Class.create();
GvaScript.Form.Methods = {
to_hash: function(form) {
form = $(form);
return form.serialize({hash:true});
},
to_tree: function(form) {
form = $(form);
return Hash.expand(GvaScript.Form.to_hash(form));
},
fill_from_tree : (function() {
var doc = document; // local variable is faster than global 'document'
// IMPLEMENTATION NOTE : Form.Element.setValue() is quite similar,
// but our treatment of arrays is different, so we have to reimplement
var _fill_from_value = function(form, elem, val, is_init) {
// force val into an array
if (!(val instanceof Array)) val = [val];
var old_value = null; // needed for value:change custom event
var new_value = null;
switch (elem.type) {
case "text" :
case "textarea" :
case "hidden" :
old_value = elem.value;
elem.value = new_value = val.join(",");
break;
case "checkbox" :
case "radio":
var elem_val = elem.value;
old_value = elem.checked ? elem_val : null;
// hand-crafted loop through val array (because val.include() is too slow)
elem.checked = false;
for (var count = val.length; count--;) {
if (val[count] == elem_val) {
elem.checked = true;
break;
}
}
new_value = elem.checked ? elem_val : null;
break;
case "select-one" :
case "select-multiple" :
var options = elem.options;
var old_values = [],
new_values = [];
for (var i=0, len=options.length; i<len; i++) {
var opt = options[i];
var opt_value = opt.value || opt.text;
if (opt.selected) old_values.push(opt_value);
// hand-crafted loop through val array (because val.include() is too slow
opt.selected = false;
for (var count = val.length; count--;) {
if (val[count] == opt_value) {
new_values.push(opt_value);
opt.selected = true;
break;
}
}
( run in 2.008 seconds using v1.01-cache-2.11-cpan-5b529ec07f3 )