CssClassStore.js 4.9 KB

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