platform/plugins/Q/web/js/tools/pie.js - Q Platform
Show:

File: platform/plugins/Q/web/js/tools/pie.js

  1. (function (Q, $) {
  2. /**
  3. * @module Q-tools
  4. */
  5.  
  6. /**
  7. * This tool show round process bar.
  8. * @class Q pie
  9. * @constructor
  10. * @param {Object} [options] Override various options for this tool
  11. * @param {Number} [options.fraction] Value process
  12. * @param {Number} [options.size] Circle size
  13. * @param {Object} [options.bgImage] URL to image to put in background
  14. * @param {Object} [options.color] Arc color
  15. * @param {Object} [options.borderSize] Arc border size. The progress line width.
  16. * @return {Q.Tool}
  17. */
  18. Q.Tool.define("Q/pie", function (options) {
  19. var tool = this;
  20. var $toolElement = $(tool.element);
  21. var state = tool.state;
  22. options = options || {};
  23.  
  24. Q.addStylesheet('{{Q}}/css/pie.css');
  25.  
  26. // positioning tool box
  27. tool.positionParent();
  28.  
  29. // rotate arcs according to fraction
  30. tool.Q.onStateChanged('fraction').set(function () {
  31. var angle = 360/(100/parseFloat(state.fraction));
  32.  
  33. var arcLeftAngle = angle > 180 ? angle - 180 + 45 : 45;
  34. arcLeftAngle = angle > 360 ? 225 : arcLeftAngle;
  35. var arcLeftDisplay = arcLeftAngle > 45 ? "block" : "none"; // only for Chrome
  36.  
  37. var arcRightAngle = angle < 180 ? angle + 45 : 180 + 45;
  38. arcRightAngle = angle < 0 ? 45 : arcRightAngle;
  39.  
  40. $(".Q_pie_arc.Q_pie_arcLeft", $toolElement).css({transform: "rotate(" + arcLeftAngle + "deg)", display: arcLeftDisplay}); // Chrome incorrect calculate positions
  41. $(".Q_pie_arc.Q_pie_arcRight", $toolElement).css("transform", "rotate(" + arcRightAngle + "deg)");
  42. }, "Q/pie");
  43.  
  44. // set new size
  45. tool.Q.onStateChanged('size').set(function () {
  46. var size = state.size;
  47.  
  48. $(".Q_pie_box", $toolElement).css({width: size, height: size});
  49. }, "Q/pie");
  50.  
  51. // set new Arc color
  52. tool.Q.onStateChanged('color').set(function () {
  53. var color = state.color;
  54.  
  55. $(".Q_pie_arc.Q_pie_arcLeft", $toolElement).css("border-color", color + " " + color + " transparent transparent");
  56. $(".Q_pie_arc.Q_pie_arcRight", $toolElement).css("border-color", "transparent transparent " + color + " " + color);
  57. }, "Q/pie");
  58.  
  59. // set new Arc borderSize
  60. tool.Q.onStateChanged('borderSize').set(function () {
  61. var borderSize = state.borderSize;
  62.  
  63. $(".Q_pie_arc, .Q_pie_shield", $toolElement).css("border-width", borderSize);
  64. }, "Q/pie");
  65.  
  66. tool.refresh();
  67. },
  68.  
  69. {
  70. fraction: 0,
  71. size: 80,
  72. bgImage: "",
  73. color: 'red',
  74. borderSize: 5, // in px
  75. clickPos: {
  76. angle: 0,
  77. anglePercent: 0
  78. } // service param contain info about click event inside tool element
  79. },
  80.  
  81. {
  82. refresh: function () {
  83. var tool = this;
  84. var state = tool.state;
  85. var $toolElement = $(tool.element);
  86.  
  87. var bgImage = state.bgImage ? "background-image: url("+state.bgImage+")" : "";
  88.  
  89. var fields = {
  90. bgImage: bgImage
  91. };
  92.  
  93. Q.Template.render(
  94. 'Q/pie/main',
  95. fields,
  96. function (err, html) {
  97. if (err) return;
  98.  
  99. $toolElement.html(html);
  100.  
  101. // set arcs rotate
  102. tool.stateChanged('fraction');
  103.  
  104. // set pie size
  105. tool.stateChanged('size');
  106.  
  107. // set Arc color
  108. tool.stateChanged('color');
  109.  
  110. // set Arc borderSize
  111. tool.stateChanged('borderSize');
  112.  
  113. // set toolElement click event
  114. $toolElement.on(Q.Pointer.click, function (event) {
  115. var x = event.pageX - $toolElement.offset().left;
  116. var y = event.pageY - $toolElement.offset().top;
  117. var toolWidth = $toolElement.width();
  118. var insideR = (toolWidth - state.borderSize * 2)/2; // radius of circle inside arcs
  119. var outsideR = toolWidth/2; // radius of circle
  120. var angle = Math.atan2(y - outsideR, x - outsideR)*180/Math.PI + 90;
  121. angle = angle > 0 ? angle : 360 + angle;
  122. var anglePercent = angle/360*100;
  123.  
  124. state.clickPos.angle = angle;
  125. state.clickPos.anglePercent = anglePercent;
  126.  
  127. if(Math.sqrt((x - outsideR) * (x - outsideR) + (y - outsideR) * (y - outsideR)) < insideR){
  128. state.clickPos.inside = true;
  129. }else{
  130. state.clickPos.inside = false;
  131.  
  132. //state.fraction = anglePercent;
  133. //tool.stateChanged('fraction');
  134. }
  135. });
  136. }
  137. )
  138. },
  139. /**
  140. * Positioning tool box in parent box
  141. * @method positionParent
  142. */
  143. positionParent: function(){
  144. var tool = this;
  145.  
  146. // if size already set in params - exit
  147. if(tool.state.size) return;
  148.  
  149. var parent = $(tool.element).parent();
  150. var parentW = parent.width();
  151. var parentH = parent.height();
  152.  
  153. tool.state.size = parentW > parentH ? parentH : parentW;
  154. },
  155. /**
  156. * Set pie to initial position
  157. * @method initPos
  158. */
  159. initPos: function(){
  160. var tool = this;
  161.  
  162. tool.state.fraction = 0;
  163. tool.stateChanged('fraction');
  164. }
  165. }
  166. );
  167.  
  168. Q.Template.set('Q/pie/main',
  169. '<div class="Q_pie_box" style="{{bgImage}}">'
  170. + '<div class="Q_pie_shield"></div>'
  171. + '<div class="Q_pie_arcBox Q_pie_arcBoxRight"><div class="Q_pie_arc Q_pie_arcRight"></div></div>'
  172. + '<div class="Q_pie_arcBox Q_pie_arcBoxLeft"><div class="Q_pie_arc Q_pie_arcLeft"></div></div>'
  173. + '</div>'
  174. );
  175.  
  176. })(Q, jQuery);
  177.