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