PageRenderTime 28ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/wwwroot/widgets/whiteboard/whiteboard.ui.js

http://github.com/AF83/ucengine
JavaScript | 320 lines | 271 code | 42 blank | 7 comment | 20 complexity | 67d9457f846074177b8da2ff8d98b011 MD5 | raw file
  1. (function($) {
  2. $.uce.WhiteBoard = function() {}
  3. $.uce.WhiteBoard.prototype = {
  4. options: {
  5. /**
  6. * uce meeting instance
  7. */
  8. ucemeeting : null,
  9. // widget title
  10. title : "Whiteboard",
  11. disabled : false,
  12. colors : [],
  13. width : null,
  14. height : null,
  15. ratio : 0.8
  16. },
  17. // ucengine events
  18. meetingsEvents : {
  19. "whiteboard.shape.draw" : 'handleWhiteboardEvent',
  20. "whiteboard.drawing.clear" : 'handleWhiteboardEvent'
  21. },
  22. _widgetClass: 'ui-widget ui-whiteboard',
  23. _create: function() {
  24. var that = this;
  25. // Default colors
  26. this.options.colors = [this._randomColor(), this._randomColor(), '#ffffff', '#000000'];
  27. this.element.addClass(this._widgetClass);
  28. this.addHeader();
  29. this._content = $("<div>").addClass("ui-widget-content").appendTo(this.element);
  30. /* create toolbar */
  31. this.tool = "pencil";
  32. this.color = "#000000";
  33. var toolbar = $('<div>').attr('class', 'ui-whiteboard-toolbar');
  34. this._tools = {};
  35. this._tools.brush = $('<span>')
  36. .attr('class', 'ui-whiteboard ui-toolbar-button ui-button-left')
  37. .button({
  38. text: false,
  39. icons: {
  40. primary: "ui-icon-pencil"
  41. }
  42. }).click(function() {
  43. that._selectTool(this);
  44. that._changeTool('pencil');
  45. }).appendTo(toolbar);
  46. this._tools.rectangle = $('<span>')
  47. .attr('class', 'ui-whiteboard ui-toolbar-button ui-button-left')
  48. .button({
  49. text: false,
  50. icons: {
  51. primary: "ui-whiteboard-icon-rectangle"
  52. }
  53. }).click(function() {
  54. that._selectTool(this);
  55. that._changeTool('rectangle');
  56. }).appendTo(toolbar);
  57. this._tools.circle = $('<span>')
  58. .attr('class', 'ui-whiteboard ui-toolbar-button ui-button-left')
  59. .button({
  60. text: false,
  61. icons: {
  62. primary: "ui-whiteboard-icon-circle"
  63. }
  64. }).click(function() {
  65. that._selectTool(this);
  66. that._changeTool('circle');
  67. }).appendTo(toolbar);
  68. this._selectTool(this._tools.brush);
  69. this._tools.clear = $('<span>')
  70. .attr('class', 'ui-whiteboard ui-toolbar-button ui-button-right')
  71. .button({
  72. text: false,
  73. icons: {
  74. primary: "ui-icon-trash"
  75. }
  76. }).click(function() {
  77. that._clear();
  78. }).appendTo(toolbar);
  79. this._colors = [];
  80. $.each(this.options.colors, function(index, hex) {
  81. var color = $('<span>')
  82. .attr('class', 'ui-whiteboard ui-toolbar-button ui-button-right ui-button-color')
  83. .button()
  84. .click(function() {
  85. that.color = hex;
  86. $.each(that._colors, function(index, color) {
  87. color.removeClass('ui-state-highlight');
  88. });
  89. $(this).addClass('ui-state-highlight');
  90. })
  91. .css('background-color', hex);
  92. color.appendTo(toolbar);
  93. that._colors.push(color);
  94. });
  95. toolbar.appendTo(this._content);
  96. var canvasId = this.element.attr('id') + "-canvas";
  97. this.canvas = $('<canvas>')
  98. .attr('id', canvasId)
  99. .bind('mousedown', function(event) {
  100. Whiteboard.setStrokeStyle(that.color);
  101. that.canvas.unbind("mouseup");
  102. that.canvas.unbind("mouseout");
  103. if (that.tool == "pencil") {
  104. var x1 = that._getX(event);
  105. var y1 = that._getY(event);
  106. that.canvas.bind("mousemove", function(event) {
  107. var x2 = that._getX(event);
  108. var y2 = that._getY(event);
  109. that._draw(x1, y1, x2, y2);
  110. x1 = x2;
  111. y1 = y2;
  112. });
  113. that.canvas.bind("mouseup", function(event) {
  114. that._endDraw();
  115. });
  116. that.canvas.bind("mouseout", function(event) {
  117. that._endDraw();
  118. });
  119. };
  120. if (that.tool == "rectangle") {
  121. var x1 = that._getX(event);
  122. var y1 = that._getY(event);
  123. Whiteboard.beginShape(x1, y1);
  124. that.canvas.bind("mousemove", function(event) {
  125. var x2 = that._getX(event);
  126. var y2 = that._getY(event);
  127. Whiteboard.drawRectangle(x2, y2);
  128. that.canvas.unbind('mouseup');
  129. that.canvas.bind("mouseup", function(event) {
  130. that._draw(x1, y1, x2, y2);
  131. that._endDraw();
  132. });
  133. })
  134. };
  135. if (that.tool == "circle") {
  136. var x1 = that._getX(event);
  137. var y1 = that._getY(event);
  138. Whiteboard.beginShape(x1, y1);
  139. that.canvas.bind("mousemove", function(event) {
  140. var x2 = that._getX(event);
  141. var y2 = that._getY(event);
  142. Whiteboard.drawOval(x2, y2);
  143. that.canvas.unbind('mouseup');
  144. that.canvas.bind("mouseup", function(event) {
  145. that._draw(x1, y1, x2, y2);
  146. that._endDraw();
  147. });
  148. });
  149. };
  150. });
  151. $('<div>')
  152. .attr('class', 'ui-whiteboard-drawing')
  153. .append(this.canvas)
  154. .appendTo(this._content);
  155. Whiteboard.init(canvasId);
  156. if (!this.options.width && !this.options.height) {
  157. this.options.width = this.element.width();
  158. }
  159. if (!this.options.width && this.options.height) {
  160. this.options.width = this.options.height / this.options.ratio;
  161. }
  162. if (this.options.height) {
  163. this.options.ratio = this.options.height / this.options.width;
  164. }
  165. this.canvas.attr('width', this.options.width);
  166. this.canvas.attr('height', this.options.width * this.options.ratio);
  167. },
  168. _getX: function(event) {
  169. var clientX = event.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
  170. var cssx = (clientX - this.canvas.offset().left);
  171. var xrel = Whiteboard.getRelative().width;
  172. var canvasx = cssx * xrel;
  173. return parseInt(canvasx);
  174. },
  175. _getY: function(event) {
  176. var clientY = event.clientY + document.body.scrollTop + document.documentElement.scrollTop;
  177. var cssy = (clientY - this.canvas.offset().top);
  178. var yrel = Whiteboard.getRelative().height;
  179. var canvasy = cssy * yrel;
  180. return parseInt(canvasy);
  181. },
  182. _clear: function() {
  183. if (this.options.disabled == false) {
  184. this.options.ucemeeting.push("whiteboard.drawing.clear", {});
  185. }
  186. },
  187. _draw: function(x1, y1, x2, y2) {
  188. if (this.options.disabled == false) {
  189. this.options.ucemeeting.push("whiteboard.shape.draw",
  190. {"tool": this.tool,
  191. "color": this.color,
  192. "x1": x1,
  193. "y1": y1,
  194. "x2": x2,
  195. "y2": y2});
  196. }
  197. },
  198. _endDraw: function(event) {
  199. this.canvas.unbind("mousemove");
  200. this.canvas.unbind("mouseup");
  201. this.canvas.unbind("mouseout");
  202. },
  203. _selectTool: function(tool) {
  204. this._tools.brush.removeClass('ui-state-highlight');
  205. this._tools.rectangle.removeClass('ui-state-highlight');
  206. this._tools.circle.removeClass('ui-state-highlight');
  207. $(tool).addClass('ui-state-highlight');
  208. },
  209. _changeTool: function(tool) {
  210. this.tool = tool;
  211. },
  212. _randomColor: function() {
  213. var rint = Math.round(0xffffff * Math.random());
  214. var color = ('#0' + rint.toString(16)).replace(/^#0([0-9a-f]{6})$/i, '#$1');
  215. return color;
  216. },
  217. _setOption: function(key, value) {
  218. $.Widget.prototype._setOption.apply(this, arguments);
  219. },
  220. reduce: function() {
  221. this._resize();
  222. },
  223. expand: function() {
  224. this._resize();
  225. },
  226. clear: function() {
  227. var canvas = this.canvas[0];
  228. var context = canvas.getContext('2d');
  229. context.clearRect(0, 0, canvas.width, canvas.height);
  230. this._endDraw();
  231. },
  232. _resize: function() {
  233. var width = this.element.width();
  234. this.canvas.css('height', width * this.options.ratio);
  235. },
  236. hideControls: function() {
  237. this.element.find('.ui-whiteboard-toolbar').hide();
  238. },
  239. showControls: function() {
  240. this.element.find('.ui-whiteboard-toolbar').show();
  241. },
  242. handleWhiteboardEvent: function(event) {
  243. var x1 = parseInt(event.metadata.x1);
  244. var y1 = parseInt(event.metadata.y1);
  245. var x2 = parseInt(event.metadata.x2);
  246. var y2 = parseInt(event.metadata.y2);
  247. Whiteboard.setStrokeStyle(event.metadata.color);
  248. if (event.type == "whiteboard.shape.draw") {
  249. switch (event.metadata.tool) {
  250. case "pencil":
  251. Whiteboard.beginPencilDraw(x1, y1);
  252. Whiteboard.pencilDraw(x2, y2);
  253. break;
  254. case "rectangle":
  255. Whiteboard.beginShape(x1, y1);
  256. Whiteboard.drawRectangle(x2, y2);
  257. break;
  258. case "circle":
  259. Whiteboard.beginShape(x1, y1);
  260. Whiteboard.drawOval(x2, y2);
  261. break;
  262. }
  263. }
  264. if (event.type == "whiteboard.drawing.clear") {
  265. var canvas = this.canvas[0];
  266. var context = canvas.getContext('2d');
  267. context.clearRect(0, 0, canvas.width, canvas.height);
  268. this._endDraw();
  269. }
  270. },
  271. destroy: function() {
  272. this.element.find('*').remove();
  273. this.element.removeClass(this._widgetClass);
  274. $.Widget.prototype.destroy.apply(this, arguments); // default destroy
  275. }
  276. };
  277. $.uce.widget("whiteboard", new $.uce.WhiteBoard());
  278. })(jQuery);