CssClassStore.js 4.5 KB

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