2 * (c) 2005-2007 Richard Cowin (http://openrico.org)
4 * Rico is licensed under the Apache License, Version 2.0 (the "License"); you may not use this
5 * file except in compliance with the License. You may obtain a copy of the License at
6 * http://www.apache.org/licenses/LICENSE-2.0
10 Rico.selectionSet = function(set, options){
11 new Rico.SelectionSet(set, options)
14 Rico.SelectionSet = Class.create();
15 Rico.SelectionSet.prototype = {
16 initialize: function(selectionSet, options){
17 this.options = options || {}
18 if (typeof selectionSet == 'string')
19 selectionSet = $$(selectionSet)
20 this.previouslySelected = [];
21 this.selectionSet = selectionSet;
22 this.selectedClassName = this.options.selectedClass || "selected";
23 this.selectNode = this.options.selectNode || function(e){return e};
24 this.onSelect = this.options.onSelect;
25 this.onFirstSelect = this.options.onFirstSelect;
26 this.clickHandler = this.click.bind(this);
27 selectionSet.each(function(e) {Event.observe(e, "click", new Rico.EventWrapper(this.clickHandler,e).wrapper);}.bind(this))
28 if (!this.options.noDefault)
29 this.selectIndex(this.options.selectedIndex || 0)
32 this.previouslySelected = [];
33 this.notifySelected(this.selected);
35 select: function(element){
36 if (this.selected == element)
40 new Element.ClassNames(this.selectNode(this.selected)).remove(this.selectedClassName)
42 this.notifySelected(element)
44 this.selected = element;
45 new Element.ClassNames(this.selectNode(this.selected)).add(this.selectedClassName)
47 notifySelected: function(element){
48 var index = this.selectionSet.indexOf(element)
49 if (this.onFirstSelect && !this.previouslySelected[index]){
50 this.onFirstSelect(element, index)
51 this.previouslySelected[index] = true;
55 this.onSelect(element, index)
58 selectIndex: function(index){
59 this.select(this.selectionSet[index])
61 nextSelectItem: function(index){
62 var index = this.selectionSet.indexOf(this.selected)
63 if (index + 1 >= this.selectionSet.length)
64 return this.selectionSet[index - 1];
66 return this.selectionSet[index + 1];
68 selectNext: function(){
69 var index = this.selectionSet.indexOf(this.selected)
70 if (index >= this.selectionSet.length)
71 this.selectIndex(index - 1)
73 this.selectIndex(index + 1)
75 click: function(event,target) {
79 // this.selectionSet.push(item)
80 if (item.constructur == Array)
81 item.each(function(e){
82 Event.observe(e, "click", new Rico.EventWrapper(this.clickHandler,item).wrapper);
85 Event.observe(item, "click", new Rico.EventWrapper(this.clickHandler,item).wrapper);
87 remove: function(item){
88 this.selectionSet = this.selectionSet.without(item)
89 //Todo: need to cleanup all events on item - need to keep track of eventwrappers
91 removeAll: function(){
96 Rico.HoverSet = Class.create();
97 Rico.HoverSet.prototype = {
98 initialize: function(hoverSet, options){
99 options = options || [];
100 this.hoverSet = hoverSet;
101 this.hoverClassName = options.hoverClass || "hover";
102 this.hoverNodes = options.hoverNodes || function(e){return [e]};
103 this.listenerHover = this._onHover.bind(this)
104 this.listenerEndHover = this._onUnHover.bind(this)
106 this.hoverSet.each((function(e) {Event.observe(e, "mousemove", new Rico.EventWrapper(this.listenerHover,e).wrapper);}).bind(this))
107 this.hoverSet.each((function(e) {Event.observe(e, "mouseout", new Rico.EventWrapper(this.listenerEndHover,e).wrapper);}).bind(this))
109 _onHover: function(event,target) {
112 _onUnHover: function(event,target) {
113 this.unHover(target);
115 hover: function(target) {
116 this.hoverNodes(target).each((function(t){Element.classNames(t).add(this.hoverClassName)}).bind(this));
118 unHover: function(target) {
119 this.hoverNodes(target).each((function(t){Element.classNames(t).remove(this.hoverClassName)}).bind(this));
122 Event.observe(item, "mousemove", new Rico.EventWrapper(this.listenerHover,item).wrapper);
123 Event.observe(item, "mouseout", new Rico.EventWrapper(this.listenerEndHover,item).wrapper);
125 remove: function(item){
126 //Todo: need to cleanup all events on item - need to keep terack of eventwrappers
128 //Event.stopObserving(e, "mousemove", new Rico.EventWrapper(this.listenerHover,e).wrapper);}).bind(this))
129 //this.hoverSet.each((function(e) {Event.observe(e, "mouseout", new Rico.EventWrapper(this.listenerEndHover,e).wrapper);}).bind(this))
132 removeAll: function(item){
139 clearCurrent: function(group) {
140 var last_hover = Rico.Hover.groups[group];
141 if(!last_hover) return
142 clearTimeout(last_hover[0])
144 Rico.Hover.groups[group] = null;
146 end: function(group) {
147 Rico.Hover.groups[group][1].end();
149 endWith: function(hover, group) {
150 var timer = setTimeout('Rico.Hover.end("'+ group + '")', hover.exitDelay)
151 Rico.Hover.groups[group] = [timer, hover]
155 Rico.HoverDisplay = Class.create();
156 Rico.HoverDisplay.prototype = {
157 initialize: function(element, options) {
158 this.element = element;
159 this.options = options || {};
160 this.group = this.options.group;
161 this.exitDelay = this.options.delay || 1000;
164 Rico.Hover.clearCurrent(this.group)
165 Element.show(this.element)
167 end: function(delay) {
169 Rico.Hover.endWith(this, this.group);
171 Element.hide(this.element)
176 Rico.EventWrapper = Class.create();
177 Rico.EventWrapper.prototype = {
178 initialize: function(handler, target){
179 this.handler = handler;
180 this.target = target;
181 this.wrapper = this.wrapperCall.bindAsEventListener(this)
183 wrapperCall: function(event){
184 this.handler(event, this.target)
188 Rico.includeLoaded('ricoBehaviors.js');