Alien-GvaScript
view release on metacpan or search on metacpan
lib/Alien/GvaScript/lib/GvaScript.js view on Meta::CPAN
/*-------------------------------------------------------------------------*
* GvaScript - Javascript framework born in Geneva.
*
* Authors: Laurent Dami <laurent.d...@etat.ge.ch>
* Mona Remlawi
* Jean-Christophe Durand
* Sebastien Cuendet
* LICENSE
* This library is free software, you can redistribute it and/or modify
* it under the same terms as Perl's artistic license.
*
*--------------------------------------------------------------------------*/
var GvaScript = {
Version: '1.45',
REQUIRED_PROTOTYPE: '1.7',
load: function() {
function convertVersionString(versionString) {
var v = versionString.replace(/_.*|\./g, '');
v = parseInt(v + '0'.times(4-v.length));
return versionString.indexOf('_') > -1 ? v-1 : v;
}
if((typeof Prototype=='undefined') ||
(typeof Element == 'undefined') ||
(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 || {};
lib/Alien/GvaScript/lib/GvaScript.js view on Meta::CPAN
function _leftHandler(event) {
var selectedBtn = this.selectedBtn;
if (selectedBtn) {
var nextBtn = this.previousBtn(selectedBtn);
if (nextBtn) this.select(nextBtn);
else selectedBtn.flash();
Event.stop(event);
}
}
function _rightHandler(event) {
var selectedBtn = this.selectedBtn;
if (selectedBtn) {
var prevBtn = this.nextBtn(selectedBtn);
if (prevBtn) this.select(prevBtn);
else selectedBtn.flash();
Event.stop(event);
}
}
function _tabHandler(event) {
if (this.options.preventListBlur)
if (this.isLast(this.selectedBtn))
Event.stop(event);
}
function _shiftTabHandler(event) {
if (this.options.preventListBlur)
if (this.isFirst(this.selectedBtn))
Event.stop(event);
}
function _homeHandler(event) {
if (this.selectedBtn) {
this.select(this.firstBtn());
Event.stop(event);
}
}
function _endHandler(event) {
if (this.selectedBtn) {
this.select(this.lastBtn());
Event.stop(event);
}
}
function _addHandlers() {
this.buttons.each(function(btnContainer) {
var btn;
// if the button is a GvaScript.CustomButtons.BUTTON, then the actual <button> element
// will be embedded and selectable via .btn classname:
// <span class="gva-btn-container">
// <span class="left"/>
// <span class="center">
// <button accesskey="r" class="btn" style="width: auto;" id="btn_1226916357164">
// Rechercher dans Calvin
// </button>
// </span>
// <span class="right"/>
// </span>
// this will be cleaner when all application buttons are transformed into
// GvaScript.CustomButtons.Button instances
if(btnContainer.tagName.search(/^(INPUT|BUTTON)$/i) > -1) btn = btnContainer;
else {
btn = btnContainer.down('.btn');
btn.visible = function() {return btnContainer.visible();}
// support focus function on span.buttonContainer
btnContainer.focus = function() {btn.focus();}
}
if(typeof btn == 'undefined') return;
}, this);
this.container.register('button.btn', 'focus', function(e) {
this.select.call(this, e._target.up('.'+bcss+'-btn-container'));
}.bind(this));
this.container.register('button.btn', 'blur', function(e) {
this.select.call(this, null);
}.bind(this));
}
// public members
return {
destroy: function() {
// test that element still in DOM
if(this.container) this.container.unregister();
this.keymap.destroy();
},
initialize: function(container, options) {
var defaults = {
preventListBlur : false,
flashDuration : 100, // milliseconds
flashClassName : 'flash',
keymap : null,
selectFirstBtn : true,
className : bcss+'-button'
};
this.options = Object.extend(defaults, options || {});
this.container = $(container);
// initializing the keymap
var keyHandlers = {
LEFT: _leftHandler .bindAsEventListener(this),
RIGHT: _rightHandler .bindAsEventListener(this),
TAB: _tabHandler .bindAsEventListener(this),
S_TAB: _shiftTabHandler .bindAsEventListener(this),
HOME: _homeHandler .bindAsEventListener(this),
END: _endHandler .bindAsEventListener(this)
};
this.keymap = new GvaScript.KeyMap(keyHandlers);
this.keymap.observe("keydown", container, {
preventDefault:false,
stopPropagation:false
});
// get all buttons of designated className regardless of their
// visibility jump over hidden ones when navigating
this.buttons = this.container.select('.'+this.options.className);
_addHandlers.call(this);
if (this.options.selectFirstBtn) {
if(firstButObj = this.firstBtn()) {
this.select(firstButObj);
}
// set the focus on the container anyways so that the focus
// gets trasferred successfully to windows with empty
// actionsbar
else {
this.container.writeAttribute('tabindex', 0);
this.container.focus();
}
}
},
select: function (btn) {
var previousBtn = this.selectedBtn || null;
if (previousBtn === btn) return; // selection already handled
// blur the previously selected button
if (previousBtn) {
previousBtn.removeClassName('btn-focus');
}
this.selectedBtn = btn;
if (btn) {
btn.addClassName('btn-focus');
try {
if(btn.tagName.search(/^(INPUT|BUTTON)$/i) > -1)
btn.focus();
else
btn.down('.btn').focus();
} catch (err) {}
}
},
// returns the next visible button
// null if none exists
nextBtn: function (btn) {
var _idx = this.buttons.indexOf(btn);
var _nextBtn = null;
do _nextBtn = this.buttons[++_idx]
while(_nextBtn && !(_nextBtn.visible()));
return _nextBtn;
},
// returns the previous visible button
// null if none exists
previousBtn: function (btn) {
var _idx = this.buttons.indexOf(btn);
var _prevBtn = null;
do _prevBtn = this.buttons[--_idx]
while(_prevBtn && !(_prevBtn.visible()));
return _prevBtn;
},
isFirst: function(btn) { return btn == this.firstBtn() },
isLast: function(btn) { return btn == this.lastBtn() },
// return first visible button
firstBtn: function() {
return this.buttons.find(function(e) {
return e.visible();
});
},
// return last visible button
lastBtn: function() {
return this.buttons.reverse(false).find(function(e) {
return e.visible();
});
}
}
}());
GvaScript.CustomButtons.ActionsBar = Class.create();
Object.extend(GvaScript.CustomButtons.ActionsBar.prototype, {
initialize: function(container, options) {
var bcss = CSSPREFIX();
var defaults = {
actions: [],
selectfirst: false
}
this.container = $(container);
this.container.update('');
this.options = Object.extend(defaults, options || {});
this.container.addClassName(bcss+'-actionsbar');
this.options.actions.each(function(action_props, index) {
( run in 0.826 second using v1.01-cache-2.11-cpan-0bd6704ced7 )