not really known
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

200 lines
4.8 KiB

  1. // Colorized icons cache
  2. var iconColorCache = {
  3. names: [],
  4. values: []
  5. };
  6. // Class for a Sugar icon
  7. enyo.kind({
  8. name: "Sugar.Icon",
  9. kind: enyo.Control,
  10. published: {
  11. icon: null,
  12. size: constant.iconSizeStandard,
  13. x: -1, y: -1,
  14. colorized: false,
  15. colorizedColor: null,
  16. disabled: false,
  17. disabledBackground: 'white',
  18. popupShow: null,
  19. popupHide: null,
  20. isNative: false,
  21. data: null
  22. },
  23. classes: "web-activity",
  24. components: [
  25. { name: "icon", classes: "web-activity-icon", onmouseover: "popupShowTimer", onmouseout: "popupHideTimer"},
  26. { name: "disable", classes: "web-activity-disable", showing: false}
  27. ],
  28. // Constructor
  29. create: function() {
  30. this.inherited(arguments);
  31. this.iconChanged();
  32. this.sizeChanged();
  33. this.colorizedColorChanged();
  34. this.colorizedChanged();
  35. this.isNativeChanged();
  36. this.disabledBackgroundChanged();
  37. this.disabledChanged();
  38. this.xChanged();
  39. this.yChanged();
  40. this.timer = null;
  41. this.emulateMouseOver = false;
  42. },
  43. // Timer handler for popup menu
  44. popupShowTimer: function() {
  45. if (this.popupShow == null) {
  46. return;
  47. }
  48. if (this.timer != null) {
  49. window.clearInterval(this.timer);
  50. }
  51. this.timer = window.setInterval(enyo.bind(this, "showPopup"), constant.timerPopupDuration);
  52. },
  53. showPopup: function(icon, e) {
  54. this.popupShow(this);
  55. window.clearInterval(this.timer);
  56. this.timer = null;
  57. },
  58. popupHideTimer: function(icon, e) {
  59. if (this.popupHide == null) {
  60. return;
  61. }
  62. if (this.timer != null) {
  63. window.clearInterval(this.timer);
  64. }
  65. this.timer = window.setInterval(enyo.bind(this, "hidePopup"), constant.timerPopupDuration);
  66. },
  67. hidePopup: function() {
  68. if (!this.popupHide(this)) {
  69. return;
  70. }
  71. window.clearInterval(this.timer);
  72. this.timer = null;
  73. },
  74. // Render
  75. rendered: function() {
  76. this.inherited(arguments);
  77. var node = this.$.icon.hasNode();
  78. if (node && enyo.platform.touch) {
  79. // HACK: On iOS and Chrome Android use touch events to simulate mouseover/mouseout
  80. var that = this;
  81. if (enyo.platform.ios || enyo.platform.androidChrome) {
  82. enyo.dispatcher.listen(node, "touchstart", function(e) {
  83. mouse.position = {x: e.touches[0].clientX, y: e.touches[0].clientY};
  84. that.popupShowTimer();
  85. });
  86. enyo.dispatcher.listen(node, "touchend", function(e) {
  87. that.popupHideTimer();
  88. });
  89. }
  90. }
  91. // If colorized
  92. if (this.colorized && !this.icon.isNative) {
  93. // Get colorized color
  94. var colorToUse = this.colorizedColor;
  95. var name = this.icon.directory+"/"+this.icon.icon;
  96. var cachename;
  97. node.style.backgroundImage = "url('" + name + "')";
  98. if (colorToUse == null) {
  99. // Try to find colorized version in cache
  100. colorToUse = preferences.getColor();
  101. cachename = name+"_"+colorToUse.stroke+"_"+colorToUse.fill;
  102. for (var i = 0 ; i < iconColorCache.names.length ; i++)
  103. if (iconColorCache.names[i] == cachename) {
  104. node.style.backgroundImage = iconColorCache.values[i];
  105. return;
  106. }
  107. } else {
  108. cachename = name+"_"+colorToUse.stroke+"_"+colorToUse.fill;
  109. }
  110. // Build it
  111. iconLib.colorize(node, colorToUse, function() {
  112. // Cache it
  113. iconColorCache.names.push(cachename);
  114. iconColorCache.values.push(node.style.backgroundImage);
  115. });
  116. }
  117. },
  118. // Test is cursor is inside the icon
  119. cursorIsInside: function() {
  120. return util.cursorIsInside(this);
  121. },
  122. // Property changed
  123. xChanged: function() {
  124. if (this.x != -1) {
  125. this.applyStyle("margin-left", this.x+"px");
  126. }
  127. },
  128. yChanged: function() {
  129. if (this.y != -1) {
  130. this.applyStyle("margin-top", this.y+"px");
  131. }
  132. },
  133. sizeChanged: function() {
  134. this.$.icon.applyStyle("width", this.size+"px");
  135. this.$.icon.applyStyle("height", this.size+"px");
  136. this.$.icon.applyStyle("background-size", this.size+"px "+this.size+"px");
  137. this.$.disable.applyStyle("width", this.size+"px");
  138. this.$.disable.applyStyle("height", this.size+"px");
  139. this.$.disable.applyStyle("background-size", this.size+"px "+this.size+"px");
  140. },
  141. iconChanged: function() {
  142. if (this.icon != null) {
  143. if (this.icon.isNative != null) {
  144. this.$.icon.applyStyle("background-image", "url('"+this.icon.icon+"');");
  145. } else {
  146. this.$.icon.applyStyle("background-image", "url('" + this.icon.directory+"/"+this.icon.icon + "')");
  147. }
  148. }
  149. else {
  150. this.$.icon.applyStyle("background-image", null);
  151. }
  152. },
  153. colorizedColorChanged: function() {
  154. this.render();
  155. },
  156. colorizedChanged: function() {
  157. this.render();
  158. },
  159. isNativeChanged: function(){
  160. this.render();
  161. },
  162. disabledBackgroundChanged: function() {
  163. this.$.disable.applyStyle("background-color", this.disabledBackground);
  164. },
  165. disabledChanged: function() {
  166. this.$.disable.setShowing(this.disabled);
  167. },
  168. // Colorize
  169. colorize: function(color) {
  170. var node = this.hasNode();
  171. if (!node || !node.style.backgroundImage) {
  172. return;
  173. }
  174. iconLib.colorize(node, color, function() {});
  175. }
  176. });