root / trunk / web / dojo / dojox / data / CssRuleStore.js @ 10
History | View | Annotate | Download (6.63 KB)
1 | 9 | andrej.cim | /*
|
---|---|---|---|
2 | Copyright (c) 2004-2010, The Dojo Foundation All Rights Reserved.
|
||
3 | Available via Academic Free License >= 2.1 OR the modified BSD license.
|
||
4 | see: http://dojotoolkit.org/license for details
|
||
5 | */
|
||
6 | |||
7 | |||
8 | if(!dojo._hasResource["dojox.data.CssRuleStore"]){ |
||
9 | dojo._hasResource["dojox.data.CssRuleStore"]=true; |
||
10 | dojo.provide("dojox.data.CssRuleStore");
|
||
11 | dojo.require("dojo.data.util.filter");
|
||
12 | dojo.require("dojo.data.util.sorter");
|
||
13 | dojo.require("dojox.data.css");
|
||
14 | dojo.declare("dojox.data.CssRuleStore",null,{_storeRef:"_S",_labelAttribute:"selector",_cache:null,_browserMap:null,_cName:"dojox.data.CssRuleStore",constructor:function(_1){ |
||
15 | if(_1){
|
||
16 | dojo.mixin(this,_1);
|
||
17 | } |
||
18 | this._cache={};
|
||
19 | this._allItems=null; |
||
20 | this._waiting=[];
|
||
21 | this.gatherHandle=null; |
||
22 | var _2=this; |
||
23 | function _3(){ |
||
24 | try{
|
||
25 | _2.context=dojox.data.css.determineContext(_2.context); |
||
26 | if(_2.gatherHandle){
|
||
27 | clearInterval(_2.gatherHandle); |
||
28 | _2.gatherHandle=null;
|
||
29 | } |
||
30 | while(_2._waiting.length){
|
||
31 | var _4=_2._waiting.pop();
|
||
32 | dojox.data.css.rules.forEach(_4.forFunc,null,_2.context);
|
||
33 | _4.finishFunc(); |
||
34 | } |
||
35 | } |
||
36 | catch(e){
|
||
37 | } |
||
38 | }; |
||
39 | this.gatherHandle=setInterval(_3,250); |
||
40 | },setContext:function(_5){ |
||
41 | if(_5){
|
||
42 | this.close();
|
||
43 | this.context=dojox.data.css.determineContext(_5);
|
||
44 | } |
||
45 | },getFeatures:function(){ |
||
46 | return {"dojo.data.api.Read":true}; |
||
47 | },isItem:function(_6){ |
||
48 | if(_6&&_6[this._storeRef]==this){ |
||
49 | return true; |
||
50 | } |
||
51 | return false; |
||
52 | },hasAttribute:function(_7,_8){ |
||
53 | this._assertIsItem(_7);
|
||
54 | this._assertIsAttribute(_8);
|
||
55 | var _9=this.getAttributes(_7); |
||
56 | if(dojo.indexOf(_9,_8)!=-1){ |
||
57 | return true; |
||
58 | } |
||
59 | return false; |
||
60 | },getAttributes:function(_a){ |
||
61 | this._assertIsItem(_a);
|
||
62 | var _b=["selector","classes","rule","style","cssText","styleSheet","parentStyleSheet","parentStyleSheetHref"]; |
||
63 | var _c=_a.rule.style;
|
||
64 | if(_c){
|
||
65 | var _d;
|
||
66 | for(_d in _c){ |
||
67 | _b.push("style."+_d);
|
||
68 | } |
||
69 | } |
||
70 | return _b;
|
||
71 | },getValue:function(_e,_f,_10){ |
||
72 | var _11=this.getValues(_e,_f); |
||
73 | var _12=_10;
|
||
74 | if(_11&&_11.length>0){ |
||
75 | return _11[0]; |
||
76 | } |
||
77 | return _10;
|
||
78 | },getValues:function(_13,_14){ |
||
79 | this._assertIsItem(_13);
|
||
80 | this._assertIsAttribute(_14);
|
||
81 | var _15=null; |
||
82 | if(_14==="selector"){ |
||
83 | _15=_13.rule["selectorText"];
|
||
84 | if(_15&&dojo.isString(_15)){
|
||
85 | _15=_15.split(",");
|
||
86 | } |
||
87 | }else{
|
||
88 | if(_14==="classes"){ |
||
89 | _15=_13.classes; |
||
90 | }else{
|
||
91 | if(_14==="rule"){ |
||
92 | _15=_13.rule.rule; |
||
93 | }else{
|
||
94 | if(_14==="style"){ |
||
95 | _15=_13.rule.style; |
||
96 | }else{
|
||
97 | if(_14==="cssText"){ |
||
98 | if(dojo.isIE){
|
||
99 | if(_13.rule.style){
|
||
100 | _15=_13.rule.style.cssText; |
||
101 | if(_15){
|
||
102 | _15="{ "+_15.toLowerCase()+" }"; |
||
103 | } |
||
104 | } |
||
105 | }else{
|
||
106 | _15=_13.rule.cssText; |
||
107 | if(_15){
|
||
108 | _15=_15.substring(_15.indexOf("{"),_15.length);
|
||
109 | } |
||
110 | } |
||
111 | }else{
|
||
112 | if(_14==="styleSheet"){ |
||
113 | _15=_13.rule.styleSheet; |
||
114 | }else{
|
||
115 | if(_14==="parentStyleSheet"){ |
||
116 | _15=_13.rule.parentStyleSheet; |
||
117 | }else{
|
||
118 | if(_14==="parentStyleSheetHref"){ |
||
119 | if(_13.href){
|
||
120 | _15=_13.href; |
||
121 | } |
||
122 | }else{
|
||
123 | if(_14.indexOf("style.")===0){ |
||
124 | var _16=_14.substring(_14.indexOf("."),_14.length); |
||
125 | _15=_13.rule.style[_16]; |
||
126 | }else{
|
||
127 | _15=[]; |
||
128 | } |
||
129 | } |
||
130 | } |
||
131 | } |
||
132 | } |
||
133 | } |
||
134 | } |
||
135 | } |
||
136 | } |
||
137 | if(_15!==undefined){ |
||
138 | if(!dojo.isArray(_15)){
|
||
139 | _15=[_15]; |
||
140 | } |
||
141 | } |
||
142 | return _15;
|
||
143 | },getLabel:function(_17){ |
||
144 | this._assertIsItem(_17);
|
||
145 | return this.getValue(_17,this._labelAttribute); |
||
146 | },getLabelAttributes:function(_18){ |
||
147 | return [this._labelAttribute]; |
||
148 | },containsValue:function(_19,_1a,_1b){ |
||
149 | var _1c=undefined; |
||
150 | if(typeof _1b==="string"){ |
||
151 | _1c=dojo.data.util.filter.patternToRegExp(_1b,false);
|
||
152 | } |
||
153 | return this._containsValue(_19,_1a,_1b,_1c); |
||
154 | },isItemLoaded:function(_1d){ |
||
155 | return this.isItem(_1d); |
||
156 | },loadItem:function(_1e){ |
||
157 | this._assertIsItem(_1e.item);
|
||
158 | },fetch:function(_1f){ |
||
159 | _1f=_1f||{}; |
||
160 | if(!_1f.store){
|
||
161 | _1f.store=this;
|
||
162 | } |
||
163 | var _20=_1f.scope||dojo.global;
|
||
164 | if(this._pending&&this._pending.length>0){ |
||
165 | this._pending.push({request:_1f,fetch:true}); |
||
166 | }else{
|
||
167 | this._pending=[{request:_1f,fetch:true}]; |
||
168 | this._fetch(_1f);
|
||
169 | } |
||
170 | return _1f;
|
||
171 | },_fetch:function(_21){ |
||
172 | var _22=_21.scope||dojo.global;
|
||
173 | if(this._allItems===null){ |
||
174 | this._allItems={};
|
||
175 | try{
|
||
176 | if(this.gatherHandle){ |
||
177 | this._waiting.push({"forFunc":dojo.hitch(this,this._handleRule),"finishFunc":dojo.hitch(this,this._handleReturn)}); |
||
178 | }else{
|
||
179 | dojox.data.css.rules.forEach(dojo.hitch(this,this._handleRule),null,this.context); |
||
180 | this._handleReturn();
|
||
181 | } |
||
182 | } |
||
183 | catch(e){
|
||
184 | if(_21.onError){
|
||
185 | _21.onError.call(_22,e,_21); |
||
186 | } |
||
187 | } |
||
188 | }else{
|
||
189 | this._handleReturn();
|
||
190 | } |
||
191 | },_handleRule:function(_23,_24,_25){ |
||
192 | var _26=_23["selectorText"]; |
||
193 | var s=_26.split(" "); |
||
194 | var _27=[];
|
||
195 | for(var j=0;j<s.length;j++){ |
||
196 | var tmp=s[j];
|
||
197 | var _28=tmp.indexOf("."); |
||
198 | if(tmp&&tmp.length>0&&_28!==-1){ |
||
199 | var _29=tmp.indexOf(",")||tmp.indexOf("["); |
||
200 | tmp=tmp.substring(_28,((_29!==-1&&_29>_28)?_29:tmp.length));
|
||
201 | _27.push(tmp); |
||
202 | } |
||
203 | } |
||
204 | var _2a={};
|
||
205 | _2a.rule=_23; |
||
206 | _2a.styleSheet=_24; |
||
207 | _2a.href=_25; |
||
208 | _2a.classes=_27; |
||
209 | _2a[this._storeRef]=this; |
||
210 | if(!this._allItems[_26]){ |
||
211 | this._allItems[_26]=[];
|
||
212 | } |
||
213 | this._allItems[_26].push(_2a);
|
||
214 | },_handleReturn:function(){ |
||
215 | var _2b=[];
|
||
216 | var _2c=[];
|
||
217 | var _2d=null; |
||
218 | for(var i in this._allItems){ |
||
219 | _2d=this._allItems[i];
|
||
220 | for(var j in _2d){ |
||
221 | _2c.push(_2d[j]); |
||
222 | } |
||
223 | } |
||
224 | var _2e;
|
||
225 | while(this._pending.length){ |
||
226 | _2e=this._pending.pop();
|
||
227 | _2e.request._items=_2c; |
||
228 | _2b.push(_2e); |
||
229 | } |
||
230 | while(_2b.length){
|
||
231 | _2e=_2b.pop(); |
||
232 | this._handleFetchReturn(_2e.request);
|
||
233 | } |
||
234 | },_handleFetchReturn:function(_2f){ |
||
235 | var _30=_2f.scope||dojo.global;
|
||
236 | var _31=[];
|
||
237 | var _32="all"; |
||
238 | var i;
|
||
239 | if(_2f.query){
|
||
240 | _32=dojo.toJson(_2f.query); |
||
241 | } |
||
242 | if(this._cache[_32]){ |
||
243 | _31=this._cache[_32];
|
||
244 | }else{
|
||
245 | if(_2f.query){
|
||
246 | for(i in _2f._items){ |
||
247 | var _33=_2f._items[i];
|
||
248 | var _34=dojo.isWebKit?true:(_2f.queryOptions?_2f.queryOptions.ignoreCase:false); |
||
249 | var _35={};
|
||
250 | var key;
|
||
251 | var _36;
|
||
252 | for(key in _2f.query){ |
||
253 | _36=_2f.query[key]; |
||
254 | if(typeof _36==="string"){ |
||
255 | _35[key]=dojo.data.util.filter.patternToRegExp(_36,_34); |
||
256 | } |
||
257 | } |
||
258 | var _37=true; |
||
259 | for(key in _2f.query){ |
||
260 | _36=_2f.query[key]; |
||
261 | if(!this._containsValue(_33,key,_36,_35[key])){ |
||
262 | _37=false;
|
||
263 | } |
||
264 | } |
||
265 | if(_37){
|
||
266 | _31.push(_33); |
||
267 | } |
||
268 | } |
||
269 | this._cache[_32]=_31;
|
||
270 | }else{
|
||
271 | for(i in _2f._items){ |
||
272 | _31.push(_2f._items[i]); |
||
273 | } |
||
274 | } |
||
275 | } |
||
276 | var _38=_31.length;
|
||
277 | if(_2f.sort){
|
||
278 | _31.sort(dojo.data.util.sorter.createSortFunction(_2f.sort,this));
|
||
279 | } |
||
280 | var _39=0; |
||
281 | var _3a=_31.length;
|
||
282 | if(_2f.start>0&&_2f.start<_31.length){ |
||
283 | _39=_2f.start; |
||
284 | } |
||
285 | if(_2f.count&&_2f.count){
|
||
286 | _3a=_2f.count; |
||
287 | } |
||
288 | var _3b=_39+_3a;
|
||
289 | if(_3b>_31.length){
|
||
290 | _3b=_31.length; |
||
291 | } |
||
292 | _31=_31.slice(_39,_3b); |
||
293 | if(_2f.onBegin){
|
||
294 | _2f.onBegin.call(_30,_38,_2f); |
||
295 | } |
||
296 | if(_2f.onItem){
|
||
297 | if(dojo.isArray(_31)){
|
||
298 | for(i=0;i<_31.length;i++){ |
||
299 | _2f.onItem.call(_30,_31[i],_2f); |
||
300 | } |
||
301 | if(_2f.onComplete){
|
||
302 | _2f.onComplete.call(_30,null,_2f);
|
||
303 | } |
||
304 | } |
||
305 | }else{
|
||
306 | if(_2f.onComplete){
|
||
307 | _2f.onComplete.call(_30,_31,_2f); |
||
308 | } |
||
309 | } |
||
310 | return _2f;
|
||
311 | },close:function(){ |
||
312 | this._cache={};
|
||
313 | this._allItems=null; |
||
314 | },_assertIsItem:function(_3c){ |
||
315 | if(!this.isItem(_3c)){ |
||
316 | throw new Error(this._cName+": Invalid item argument."); |
||
317 | } |
||
318 | },_assertIsAttribute:function(_3d){ |
||
319 | if(typeof _3d!=="string"){ |
||
320 | throw new Error(this._cName+": Invalid attribute argument."); |
||
321 | } |
||
322 | },_containsValue:function(_3e,_3f,_40,_41){ |
||
323 | return dojo.some(this.getValues(_3e,_3f),function(_42){ |
||
324 | if(_42!==null&&!dojo.isObject(_42)&&_41){ |
||
325 | if(_42.toString().match(_41)){
|
||
326 | return true; |
||
327 | } |
||
328 | }else{
|
||
329 | if(_40===_42){
|
||
330 | return true; |
||
331 | } |
||
332 | } |
||
333 | return false; |
||
334 | }); |
||
335 | }}); |
||
336 | } |