/public/js/odontograma.js

https://bitbucket.org/walter.poch/odontograma-public · JavaScript · 243 lines · 186 code · 33 blank · 24 comment · 37 complexity · ef59ee0fed0d79fd8cd5d1ae1c95ef7b MD5 · raw file

  1. /**
  2. * jQuery Odontogram Plugin - by Walter Poch
  3. * Developed for using by Clip Solutions
  4. * http://wpoch.com.ar
  5. */
  6. (function($) {
  7. //View Models
  8. function DienteModel(id, x, y) {
  9. var self = this;
  10. self.id = id;
  11. self.x = x;
  12. self.y = y;
  13. }
  14. //Cargo los dientes
  15. var dientes = [];
  16. //Dientes izquierdos
  17. for (var i = 0; i < 8; i++) {
  18. dientes.push(new DienteModel(18 - i, i * 25, 0));
  19. }
  20. for (var i = 0; i < 5; i++) {
  21. dientes.push(new DienteModel(55 - i, (i + 3) * 25, 1 * 40));
  22. }
  23. for (var i = 0; i < 5; i++) {
  24. dientes.push(new DienteModel(85 - i, (i + 3) * 25, 2 * 40));
  25. }
  26. for (var i = 0; i < 8; i++) {
  27. dientes.push(new DienteModel(48 - i, i * 25, 3 * 40));
  28. }
  29. //Dientes derechos
  30. for (var i = 0; i < 8; i++) {
  31. dientes.push(new DienteModel(21 + i, i * 25 + 210, 0));
  32. }
  33. for (var i = 0; i < 5; i++) {
  34. dientes.push(new DienteModel(61 + i, i * 25 + 210, 1 * 40));
  35. }
  36. for (var i = 0; i < 5; i++) {
  37. dientes.push(new DienteModel(71 + i, i * 25 + 210, 2 * 40));
  38. }
  39. for (var i = 0; i < 8; i++) {
  40. dientes.push(new DienteModel(31 + i, i * 25 + 210, 3 * 40));
  41. }
  42. // Private functions
  43. function arrayFilter(array, predicate) {
  44. array = array || [];
  45. var result = [];
  46. for (var i = 0, j = array.length; i < j; i++)
  47. if (predicate(array[i])) result.push(array[i]);
  48. return result;
  49. }
  50. function drawDiente(svg, parentGroup, diente) {
  51. if (!diente) throw new Error('Error no se ha especificado el diente.');
  52. var x = diente.x || 0,
  53. y = diente.y || 0;
  54. var defaultPolygon = {
  55. fill: 'white',
  56. stroke: 'navy',
  57. strokeWidth: 0.5
  58. }
  59. var dienteGroup = svg.group(parentGroup, {
  60. transform: 'translate(' + x + ',' + y + ')'
  61. });
  62. var calculatePolygon = function(cara) {
  63. switch (cara) {
  64. case 'S':
  65. //Superior
  66. return [[0, 0], [20, 0], [15, 5], [5, 5]];
  67. case 'I':
  68. //Inferior
  69. return [[5, 15], [15, 15], [20, 20], [0, 20]];
  70. case 'D':
  71. //Derecha
  72. return [[15, 5], [20, 0], [20, 20], [15, 15]];
  73. case 'Z':
  74. //Izquierda
  75. return [[0, 0], [5, 5], [5, 15], [0, 20]];
  76. case 'C':
  77. //Central
  78. return [[5, 5], [15, 5], [15, 15], [5, 15]];
  79. default:
  80. throw new Error('La cara: ' + cara + ' no es una cara valida.');
  81. }
  82. }
  83. var attachEventsAndData = function(element, cara) {
  84. if(!settings.readOnly){
  85. element.data('cara', cara).click(function() {
  86. var me = $(this);
  87. var cara = me.data('cara');
  88. if (!tratamientoSeleccionado) {
  89. alert('Debe seleccionar un tratamiento previamente.');
  90. return false;
  91. }
  92. //Validaciones
  93. //Validamos el tratamiento
  94. if (cara == 'X' && !tratamientoSeleccionado.aplicaDiente) {
  95. alert('El tratamiento seleccionado no se puede aplicar a toda la pieza.');
  96. return false;
  97. }
  98. if (cara != 'X' && !tratamientoSeleccionado.aplicaCara) {
  99. alert('El tratamiento seleccionado no se puede aplicar a una cara.');
  100. return false;
  101. }
  102. //TODO: Validaciones de si la cara tiene tratamiento o no, etc...
  103. var tratamiento = {
  104. diente: diente,
  105. cara: cara,
  106. tratamiento: tratamientoSeleccionado
  107. };
  108. tratamientosAplicados.push(tratamiento);
  109. self.trigger('tratamientoAplicado.odontograma', tratamiento);
  110. //Actualizo el SVG
  111. renderSvg();
  112. }).mouseenter(function() {
  113. var me = $(this);
  114. me.data('oldFill', me.attr('fill'));
  115. me.attr('fill', 'yellow');
  116. }).mouseleave(function() {
  117. var me = $(this);
  118. me.attr('fill', me.data('oldFill'));
  119. });
  120. }
  121. return element;
  122. }
  123. var createCara = function(cara) {
  124. var polygonPoints = calculatePolygon(cara);
  125. return attachEventsAndData($(svg.polygon(dienteGroup, polygonPoints, defaultPolygon)), cara);
  126. }
  127. //Creo las cara SVG y las agrego como un diccionario
  128. var caras = ['S', 'I', 'D', 'Z', 'C'];
  129. for (var i = caras.length - 1; i >= 0; i--) {
  130. var cara = caras[i];
  131. caras[cara] = createCara(cara);
  132. }
  133. //Creo el diente completo y lo agrego a las caras
  134. var caraCompleto = svg.text(dienteGroup, 6, 30, diente.id.toString(), {
  135. fill: 'navy',
  136. stroke: 'navy',
  137. strokeWidth: 0.1,
  138. style: 'font-size: 6pt;font-weight:normal'
  139. });
  140. caraCompleto = attachEventsAndData($(caraCompleto), 'X');
  141. caras['X'] = caraCompleto;
  142. //Busco los tratamientos aplicados al diente
  143. var tratamientosAplicadosAlDiente = arrayFilter(tratamientosAplicados, function(t) {
  144. return t.diente.id == diente.id;
  145. });
  146. for (var i = tratamientosAplicadosAlDiente.length - 1; i >= 0; i--) {
  147. var t = tratamientosAplicadosAlDiente[i];
  148. caras[t.cara].attr('fill', 'red');
  149. }
  150. }
  151. function renderSvg() {
  152. var svg = self.svg('get').clear();
  153. var parentGroup = svg.group({
  154. transform: 'scale(' + settings.scale.toString() + ')'
  155. });
  156. for (var i = dientes.length - 1; i >= 0; i--) {
  157. drawDiente(svg, parentGroup, dientes[i]);
  158. }
  159. }
  160. var defaults = {
  161. width: '620px',
  162. height: '250px',
  163. scale: 1.5,
  164. readOnly: false,
  165. tratamientosAplicados: []
  166. }
  167. var settings,
  168. tratamientoSeleccionado,
  169. tratamientosAplicados,
  170. self;
  171. var methods = {
  172. init: function(options) {
  173. settings = $.extend(defaults, options);
  174. return this.each(function() {
  175. //Init the SVG object
  176. self = $(this);
  177. self.svg({
  178. settings: {
  179. width: settings.width,
  180. height: settings.height
  181. }
  182. });
  183. tratamientosAplicados = settings.tratamientosAplicados;
  184. renderSvg();
  185. });
  186. },
  187. setTratamiento: function(tratamiento) {
  188. tratamientoSeleccionado = tratamiento;
  189. },
  190. removeTratamiento: function(tratamiento) {
  191. tratamientosAplicados = arrayFilter(tratamientosAplicados, function(t) {
  192. return !(t.cara == tratamiento.cara && t.diente.id == tratamiento.diente.id && t.tratamiento.id == tratamiento.tratamiento.id);
  193. });
  194. renderSvg();
  195. },
  196. getTratamientosAplicados: function(){
  197. return tratamientosAplicados;
  198. }
  199. }
  200. $.fn.odontograma = function(method) {
  201. // Method calling logic
  202. if (methods[method]) {
  203. return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
  204. } else if (typeof method === 'object' || !method) {
  205. return methods.init.apply(this, arguments);
  206. } else {
  207. $.error('Method ' + method + ' does not exist on jQuery.odontograma');
  208. }
  209. }
  210. })(jQuery);