Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

187 linhas
6.0KB

  1. /**
  2. * Copyright 2014 IBM Corp.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. **/
  16. RED.touch = RED.touch||{};
  17. RED.touch.radialMenu = (function() {
  18. var touchMenu = null;
  19. var isActive = false;
  20. var isOutside = false;
  21. var activeOption = null;
  22. function createRadial(obj,pos,options) {
  23. isActive = true;
  24. try {
  25. var w = $("body").width();
  26. var h = $("body").height();
  27. touchMenu = d3.select("body").append("div")
  28. .style({
  29. position:"absolute",
  30. top: 0,
  31. left:0,
  32. bottom:0,
  33. right:0,
  34. "z-index": 1000
  35. })
  36. .on('touchstart',function() {
  37. hide();
  38. d3.event.preventDefault();
  39. });
  40. var menu = touchMenu.append("div")
  41. .style({
  42. position: "absolute",
  43. top: (pos[1]-80)+"px",
  44. left:(pos[0]-80)+"px",
  45. "border-radius": "80px",
  46. width: "160px",
  47. height: "160px",
  48. background: "rgba(255,255,255,0.6)",
  49. border: "1px solid #666"
  50. });
  51. var menuOpts = [];
  52. var createMenuOpt = function(x,y,opt) {
  53. opt.el = menu.append("div")
  54. .style({
  55. position: "absolute",
  56. top: (y+80-25)+"px",
  57. left:(x+80-25)+"px",
  58. "border-radius": "20px",
  59. width: "50px",
  60. height: "50px",
  61. background: "#fff",
  62. border: "2px solid #666",
  63. "text-align": "center",
  64. "line-height":"50px"
  65. });
  66. if (opt.icon) {
  67. opt.el.append("i").attr("class","icon "+opt.icon)
  68. } else {
  69. opt.el.html(opt.name);
  70. }
  71. if (opt.disabled) {
  72. opt.el.style({"border-color":"#ccc",color:"#ccc"});
  73. }
  74. opt.x = x;
  75. opt.y = y;
  76. menuOpts.push(opt);
  77. opt.el.on('touchstart',function() {
  78. opt.el.style("background","#999");
  79. d3.event.preventDefault();
  80. d3.event.stopPropagation();
  81. });
  82. opt.el.on('touchend',function() {
  83. hide();
  84. opt.onselect();
  85. d3.event.preventDefault();
  86. d3.event.stopPropagation();
  87. });
  88. }
  89. var n = options.length;
  90. var dang = Math.max(Math.PI/(n-1),Math.PI/4);
  91. var ang = Math.PI;
  92. for (var i=0;i<n;i++) {
  93. var x = Math.floor(Math.cos(ang)*80);
  94. var y = Math.floor(Math.sin(ang)*80);
  95. if (options[i].name) {
  96. createMenuOpt(x,y,options[i]);
  97. }
  98. ang += dang;
  99. }
  100. var hide = function() {
  101. isActive = false;
  102. activeOption = null;
  103. touchMenu.remove();
  104. touchMenu = null;
  105. }
  106. obj.on('touchend.radial',function() {
  107. obj.on('touchend.radial',null);
  108. obj.on('touchmenu.radial',null);
  109. if (activeOption) {
  110. try {
  111. activeOption.onselect();
  112. } catch(err) {
  113. RED._debug(err);
  114. }
  115. hide();
  116. } else if (isOutside) {
  117. hide();
  118. }
  119. });
  120. obj.on('touchmove.radial',function() {
  121. try {
  122. var touch0 = d3.event.touches.item(0);
  123. var p = [touch0.pageX - pos[0],touch0.pageY-pos[1]];
  124. for (var i=0;i<menuOpts.length;i++) {
  125. var opt = menuOpts[i];
  126. if (!opt.disabled) {
  127. if (p[0]>opt.x-30 && p[0]<opt.x+30 && p[1]>opt.y-30 && p[1]<opt.y+30) {
  128. if (opt !== activeOption) {
  129. opt.el.style("background","#999");
  130. activeOption = opt;
  131. }
  132. } else if (opt === activeOption) {
  133. opt.el.style("background","#fff");
  134. activeOption = null;
  135. } else {
  136. opt.el.style("background","#fff");
  137. }
  138. }
  139. }
  140. if (!activeOption) {
  141. var d = Math.abs((p[0]*p[0])+(p[1]*p[1]));
  142. isOutside = (d > 80*80);
  143. }
  144. } catch(err) {
  145. RED._debug(err);
  146. }
  147. });
  148. } catch(err) {
  149. RED._debug(err);
  150. }
  151. }
  152. return {
  153. show: createRadial,
  154. active: function() {
  155. return isActive;
  156. }
  157. }
  158. })();