/**
* (c) 2005-2007 Richard Cowin (http://openrico.org)
*
* Rico is licensed under the Apache License, Version 2.0 (the "License"); you may not use this
* file except in compliance with the License. You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
**/
/** @ignore */
Rico.selectionSet = function(set, options){
new Rico.SelectionSet(set, options);
};
Rico.SelectionSet = Class.create(
/** @lends Rico.SelectionSet# */
{
/**
* @class
* @constructs
* @param selectionSet collection of DOM elements (or a CSS selection string)
* @param options object may contain any of the following:
* - selectedClass
- class name to add when element is selected, default is "selected"
* - selectNode
- optional function that returns the element to be selected
* - onSelect
- optional function that gets called when element is selected
* - onFirstSelect
- optional function that gets called the first time element is selected
* - noDefault
- when true, no element in the set is initially selected, default is false
* - selectedIndex
- index of the element that should be initially selected, default is 0
* - cookieName
- optional name of cookie to use to remember selected element. If specified, and the cookie exists, then the cookie value overrides selectedIndex.
* - cookieDays
- specifies how long cookie should persist (in days). If unspecified, then the cookie persists for the current session.
* - cookiePath
- optional cookie path
* - cookieDomain
- optional cookie domain
*
*/
initialize: function(selectionSet, options){
this.options = options || {};
if (typeof selectionSet == 'string')
selectionSet = $$(selectionSet);
this.previouslySelected = [];
this.selectionSet = selectionSet;
this.selectedClassName = this.options.selectedClass || "selected";
this.selectNode = this.options.selectNode || function(e){return e;};
this.onSelect = this.options.onSelect;
this.onFirstSelect = this.options.onFirstSelect;
this.clickHandler = this.click.bind(this);
selectionSet.each(function(e) {Event.observe(e, "click", new Rico.EventWrapper(this.clickHandler,e).wrapper);}.bind(this));
if (!this.options.noDefault) {
var cookieIndex=this.options.cookieName ? this.getCookie() : 0;
this.selectIndex(cookieIndex || this.options.selectedIndex || 0);
}
},
getCookie: function() {
var cookie = RicoUtil.getCookie(this.options.cookieName);
if (!cookie) return 0;
var index = parseInt(cookie);
return index < this.selectionSet.length ? index : 0;
},
reset: function(){
this.previouslySelected = [];
this.notifySelected(this.selected);
},
select: function(element){
if (this.selected == element)
return;
if (this.selected)
Element.removeClassName(this.selectNode(this.selected), this.selectedClassName);
this.notifySelected(element);
this.selected = element;
Element.addClassName(this.selectNode(this.selected), this.selectedClassName);
},
notifySelected: function(element){
var index = this.selectionSet.indexOf(element);
if (this.options.cookieName)
RicoUtil.setCookie(this.options.cookieName, index, this.options.cookieDays, this.options.cookiePath, this.options.cookieDomain);
if (this.onFirstSelect && !this.previouslySelected[index]){
this.onFirstSelect(element, index);
this.previouslySelected[index] = true;
}
if (this.onSelect)
try{
this.onSelect(element, index);
} catch (e) {};
},
selectIndex: function(index){
this.select(this.selectionSet[index]);
},
nextSelectItem: function(){
var index = this.selectionSet.indexOf(this.selected);
if (index + 1 >= this.selectionSet.length)
return this.selectionSet[index - 1];
else
return this.selectionSet[index + 1];
},
selectNext: function(){
var index = this.selectionSet.indexOf(this.selected);
if (index >= this.selectionSet.length)
this.selectIndex(index - 1);
else
this.selectIndex(index + 1);
},
click: function(event,target) {
this.select(target);
},
add: function(item){
// this.selectionSet.push(item)
if (item.constructur == Array) {
item.each(function(e){
Event.observe(e, "click", new Rico.EventWrapper(this.clickHandler,item).wrapper);
}.bind(this));
} else {
Event.observe(item, "click", new Rico.EventWrapper(this.clickHandler,item).wrapper);
}
},
remove: function(item){
this.selectionSet = this.selectionSet.without(item);
//Todo: need to cleanup all events on item - need to keep track of eventwrappers
},
removeAll: function(){
}
});
Rico.HoverSet = Class.create(
/** @lends Rico.HoverSet# */
{
/**
* @class
* @constructs
* @param hoverSet collection of DOM elements
* @param options object may contain any of the following:
* - hoverClass
- class name to add when mouse is over element, default is "hover"
* - hoverNodes
- optional function to select/filter which nodes are in the set
*
*/
initialize: function(hoverSet, options){
options = options || [];
this.hoverSet = hoverSet;
this.hoverClassName = options.hoverClass || "hover";
this.hoverNodes = options.hoverNodes || function(e){return [e];};
this.listenerHover = this._onHover.bind(this);
this.listenerEndHover = this._onUnHover.bind(this);
for (var i=0; i
* group
* delay
*
*/
initialize: function(element, options) {
this.element = element;
this.options = options || {};
this.group = this.options.group;
this.exitDelay = this.options.delay || 1000;
},
begin: function() {
Rico.Hover.clearCurrent(this.group);
Element.show(this.element);
},
end: function(delay) {
if(delay)
Rico.Hover.endWith(this, this.group);
else
Element.hide(this.element);
}
});
Rico.EventWrapper = Class.create(
/** @lends Rico.EventWrapper# */
{
/**
* @class
* @constructs
* @param handler
* @param target
*/
initialize: function(handler, target){
this.handler = handler;
this.target = target;
this.wrapper = this.wrapperCall.bindAsEventListener(this);
},
wrapperCall: function(event){
this.handler(event, this.target);
}
});
Rico.includeLoaded('ricoBehaviors.js');