OSDN Git Service

git-svn-id: https://svn.sourceforge.jp/svnroot/nucleus-jp/plugin@1020 1ca29b6e-896d...
[nucleus-jp/nucleus-plugins.git] / NP_TrackBack / trunk / trackback / js / rico / ricoBehaviors.js
1 /**
2   *  (c) 2005-2007 Richard Cowin (http://openrico.org)
3   *
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
7   **/
8
9
10 Rico.selectionSet = function(set, options){
11   new Rico.SelectionSet(set, options)
12 }
13
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)
30         },
31         reset: function(){
32           this.previouslySelected = [];
33           this.notifySelected(this.selected);
34         },
35         select: function(element){
36                 if (this.selected == element)
37                         return;
38
39                 if (this.selected)
40                   new Element.ClassNames(this.selectNode(this.selected)).remove(this.selectedClassName)
41     
42     this.notifySelected(element)
43
44                 this.selected = element;
45                 new Element.ClassNames(this.selectNode(this.selected)).add(this.selectedClassName)
46         },
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;
52     }
53         if (this.onSelect)
54       try{
55             this.onSelect(element, index)
56       } catch (e) {}
57         },
58         selectIndex: function(index){
59                 this.select(this.selectionSet[index])
60         },  
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];
65     else
66       return this.selectionSet[index + 1];        
67         },
68         selectNext: function(){
69     var index = this.selectionSet.indexOf(this.selected)
70     if (index >= this.selectionSet.length)
71       this.selectIndex(index - 1)
72     else
73       this.selectIndex(index + 1)
74         },
75         click: function(event,target) {
76                 this.select(target);
77         },
78         add: function(item){
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);
83             }.bind(this))
84           else
85                   Event.observe(item, "click", new Rico.EventWrapper(this.clickHandler,item).wrapper);
86         },
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
90         },
91         removeAll: function(){
92                 
93         }
94  }
95
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)
105       
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))  
108         },
109         _onHover: function(event,target) {
110           this.hover(target);
111         },      
112         _onUnHover: function(event,target) {
113           this.unHover(target);
114         },
115         hover: function(target) {
116           this.hoverNodes(target).each((function(t){Element.classNames(t).add(this.hoverClassName)}).bind(this));
117         },      
118         unHover: function(target) {
119           this.hoverNodes(target).each((function(t){Element.classNames(t).remove(this.hoverClassName)}).bind(this));
120         },
121                 add: function(item){
122           Event.observe(item, "mousemove", new Rico.EventWrapper(this.listenerHover,item).wrapper);
123           Event.observe(item, "mouseout", new Rico.EventWrapper(this.listenerEndHover,item).wrapper);
124                 },
125                 remove: function(item){
126                         //Todo: need to cleanup all events on item - need to keep terack of eventwrappers
127                         //stopObserving
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))
130                         //hoverSet
131                 },
132                 removeAll: function(item){
133                 }
134 }
135  
136
137 Rico.Hover = {
138   groups: {},
139   clearCurrent: function(group) {
140     var last_hover = Rico.Hover.groups[group];
141     if(!last_hover) return  
142     clearTimeout(last_hover[0])
143     last_hover[1].end()
144     Rico.Hover.groups[group] = null;
145   }, 
146   end: function(group) {
147         Rico.Hover.groups[group][1].end();
148   },
149   endWith: function(hover, group) {
150         var timer = setTimeout('Rico.Hover.end("'+ group + '")', hover.exitDelay)
151     Rico.Hover.groups[group] = [timer, hover]
152   }
153 }
154
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;
162   },
163   begin: function() {
164     Rico.Hover.clearCurrent(this.group)
165                 Element.show(this.element)
166   },
167   end: function(delay) {
168     if(delay)
169         Rico.Hover.endWith(this, this.group);
170     else 
171                   Element.hide(this.element)              
172   }
173 }
174
175
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)
182   },
183   wrapperCall: function(event){
184     this.handler(event, this.target)
185   }
186 }
187
188 Rico.includeLoaded('ricoBehaviors.js');