1 | define(["dojo/_base/declare","dojox/data/CssRuleStore"], |
---|
2 | function(declare, CssRuleStore) { |
---|
3 | |
---|
4 | /*===== var CssRuleStore = dojox.data.CssRuleStore =====*/ |
---|
5 | |
---|
6 | return declare("dojox.data.CssClassStore", CssRuleStore, { |
---|
7 | // summary: |
---|
8 | // Basic store to display CSS information. |
---|
9 | // description: |
---|
10 | // The CssClassStore allows users to get information about active Css classes in the page running the CssClassStore. |
---|
11 | // It can also filter out classes from specific stylesheets. The attributes it exposes on classes are as follows: |
---|
12 | // class: The classname, including the '.'. |
---|
13 | // classSans: The classname without the '.'. |
---|
14 | |
---|
15 | _labelAttribute: 'class', // text representation of the Item [label and identifier may need to stay due to method names] |
---|
16 | _idAttribute: 'class', |
---|
17 | _cName: "dojox.data.CssClassStore", |
---|
18 | |
---|
19 | getFeatures: function(){ |
---|
20 | // summary: |
---|
21 | // See dojo.data.api.Read.getFeatures() |
---|
22 | return { |
---|
23 | "dojo.data.api.Read" : true, |
---|
24 | "dojo.data.api.Identity" : true |
---|
25 | }; |
---|
26 | }, |
---|
27 | |
---|
28 | getAttributes: function(item){ |
---|
29 | // summary: |
---|
30 | // See dojo.data.api.Read.getAttributes() |
---|
31 | this._assertIsItem(item); |
---|
32 | return ['class', 'classSans']; |
---|
33 | }, |
---|
34 | |
---|
35 | getValue: function(item, attribute, defaultValue){ |
---|
36 | // summary: |
---|
37 | // See dojo.data.api.Read.getValue() |
---|
38 | var values = this.getValues(item, attribute); |
---|
39 | if(values && values.length > 0){ |
---|
40 | return values[0]; |
---|
41 | } |
---|
42 | return defaultValue; |
---|
43 | }, |
---|
44 | |
---|
45 | getValues: function(item, attribute){ |
---|
46 | // summary: |
---|
47 | // See dojo.data.api.Read.getValues() |
---|
48 | this._assertIsItem(item); |
---|
49 | this._assertIsAttribute(attribute); |
---|
50 | var value = []; |
---|
51 | if(attribute === "class"){ |
---|
52 | value = [item.className]; |
---|
53 | }else if(attribute === "classSans"){ |
---|
54 | value = [item.className.replace(/\./g,'')]; |
---|
55 | } |
---|
56 | return value; |
---|
57 | }, |
---|
58 | |
---|
59 | _handleRule: function(rule, styleSheet, href){ |
---|
60 | // summary: |
---|
61 | // Handles the creation of an item based on the passed rule. In this store, this implies |
---|
62 | // parsing out all available class names. |
---|
63 | var obj = {}; |
---|
64 | var s = rule['selectorText'].split(" "); |
---|
65 | for(var j=0; j<s.length; j++){ |
---|
66 | var tmp = s[j]; |
---|
67 | var first = tmp.indexOf('.'); |
---|
68 | if(tmp && tmp.length > 0 && first !== -1){ |
---|
69 | var last = tmp.indexOf(',') || tmp.indexOf('['); |
---|
70 | tmp = tmp.substring(first, ((last !== -1 && last > first)?last:tmp.length)); |
---|
71 | obj[tmp] = true; |
---|
72 | } |
---|
73 | } |
---|
74 | for(var key in obj){ |
---|
75 | if(!this._allItems[key]){ |
---|
76 | var item = {}; |
---|
77 | item.className = key; |
---|
78 | item[this._storeRef] = this; |
---|
79 | this._allItems[key] = item; |
---|
80 | } |
---|
81 | } |
---|
82 | }, |
---|
83 | |
---|
84 | _handleReturn: function(){ |
---|
85 | // summary: |
---|
86 | // Handles the return from a fetching action. Delegates requests to act on the resulting |
---|
87 | // item set to eitehr the _handleFetchReturn or _handleFetchByIdentityReturn depending on |
---|
88 | // where the request originated. |
---|
89 | var _inProgress = []; |
---|
90 | |
---|
91 | var items = {}; |
---|
92 | for(var i in this._allItems){ |
---|
93 | items[i] = this._allItems[i]; |
---|
94 | } |
---|
95 | var requestInfo; |
---|
96 | // One-level deep clone (can't use dojo.clone, since we don't want to clone all those store refs!) |
---|
97 | while(this._pending.length){ |
---|
98 | requestInfo = this._pending.pop(); |
---|
99 | requestInfo.request._items = items; |
---|
100 | _inProgress.push(requestInfo); |
---|
101 | } |
---|
102 | |
---|
103 | while(_inProgress.length){ |
---|
104 | requestInfo = _inProgress.pop(); |
---|
105 | if(requestInfo.fetch){ |
---|
106 | this._handleFetchReturn(requestInfo.request); |
---|
107 | }else{ |
---|
108 | this._handleFetchByIdentityReturn(requestInfo.request); |
---|
109 | } |
---|
110 | } |
---|
111 | }, |
---|
112 | |
---|
113 | _handleFetchByIdentityReturn: function(request){ |
---|
114 | // summary: |
---|
115 | // Handles a fetchByIdentity request by finding the correct item. |
---|
116 | var items = request._items; |
---|
117 | // Per https://bugs.webkit.org/show_bug.cgi?id=17935 , Safari 3.x always returns the selectorText |
---|
118 | // of a rule in full lowercase. |
---|
119 | var item = items[request.identity]; |
---|
120 | if(!this.isItem(item)){ |
---|
121 | item = null; |
---|
122 | } |
---|
123 | if(request.onItem){ |
---|
124 | var scope = request.scope || dojo.global; |
---|
125 | request.onItem.call(scope, item); |
---|
126 | } |
---|
127 | }, |
---|
128 | |
---|
129 | /* Identity API */ |
---|
130 | getIdentity: function(/* item */ item){ |
---|
131 | // summary: |
---|
132 | // See dojo.data.api.Identity.getIdentity() |
---|
133 | this._assertIsItem(item); |
---|
134 | return this.getValue(item, this._idAttribute); |
---|
135 | }, |
---|
136 | |
---|
137 | getIdentityAttributes: function(/* item */ item){ |
---|
138 | // summary: |
---|
139 | // See dojo.data.api.Identity.getIdentityAttributes() |
---|
140 | this._assertIsItem(item); |
---|
141 | return [this._idAttribute]; |
---|
142 | }, |
---|
143 | |
---|
144 | fetchItemByIdentity: function(/* request */ request){ |
---|
145 | // summary: |
---|
146 | // See dojo.data.api.Identity.fetchItemByIdentity() |
---|
147 | request = request || {}; |
---|
148 | if(!request.store){ |
---|
149 | request.store = this; |
---|
150 | } |
---|
151 | if(this._pending && this._pending.length > 0){ |
---|
152 | this._pending.push({request: request}); |
---|
153 | }else{ |
---|
154 | this._pending = [{request: request}]; |
---|
155 | this._fetch(request); |
---|
156 | } |
---|
157 | return request; |
---|
158 | } |
---|
159 | }); |
---|
160 | |
---|
161 | }); |
---|