main.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. (function () {
  2. var instance = openerp
  3. openerp.print_engine = {}
  4. /**
  5. *
  6. */
  7. instance.print_engine.SocketManager = instance.web.Class.extend({
  8. init: function () {
  9. this.socket = null;
  10. this.state = 'offline';
  11. this.attemps = 0;
  12. this.start();
  13. },
  14. start: function () {
  15. var self = this;
  16. this.getSockets().then(function () {
  17. self.connect();
  18. });
  19. },
  20. getSockets: function () {
  21. var defer = $.Deferred();
  22. var self = this;
  23. $.get('/print_engine/sockets').done(function (data) {
  24. self.sockets = data.sockets;
  25. defer.resolve(data.sockets);
  26. }).fail(function (error){
  27. defer.reject(error);
  28. });
  29. return defer;
  30. },
  31. urlize: function (info) {
  32. return info.protocol + '://' + info.host + ':' + info.port + info.path;
  33. },
  34. connect: function () {
  35. if (this.sockets.length === 0) {
  36. return
  37. }
  38. var self = this;
  39. var url = this.urlize(this.sockets[0]);
  40. if (this.socket) {
  41. this.socket = null;
  42. }
  43. try {
  44. this.socket = new WebSocket(url);
  45. this.socket.onopen = function (e) {
  46. self.handleOpen(e);
  47. };
  48. this.socket.onclose = function (e) {
  49. self.handleClose(e);
  50. };
  51. this.socket.onerror = function (e) {
  52. self.handleError(e);
  53. };
  54. this.socket.onmessage = function (e) {
  55. self.handleMessage(e);
  56. };
  57. } catch(e) {
  58. // Ignore this error
  59. }
  60. },
  61. reconnect: function () {
  62. this.state = 'reconnecting';
  63. $('#printer-status').removeClass();
  64. $('#printer-status').addClass('printer-status-reconnect');
  65. var self = this;
  66. if (this.attemps === 3) {
  67. this.state = 'offline';
  68. $('#printer-status').removeClass();
  69. $('#printer-status').addClass('printer-status-offline');
  70. return;
  71. }
  72. setTimeout(function () {
  73. self.connect();
  74. self.attemps = self.attemps + 1;
  75. }, 3000);
  76. },
  77. handleOpen: function (e) {
  78. this.state = 'online';
  79. $('#printer-status').removeClass();
  80. $('#printer-status').addClass('printer-status-online');
  81. },
  82. handleClose: function (e) {
  83. this.reconnect();
  84. },
  85. handleMessage: function (e) {
  86. var obj = JSON.parse(e.data);
  87. if (obj.printers) {
  88. this.sendToServer('/print_engine/update', e.data).then(function () {
  89. instance.client.action_manager.inner_widget.views['form'].controller.reload();
  90. instance.web.notification.do_notify('Información', 'Impresoras actualizadas con éxito');
  91. });
  92. }
  93. },
  94. handleError: function (e) {
  95. // Ignore this error
  96. },
  97. sendToSocket: function (data) {
  98. if (this.state !== 'online') {
  99. return;
  100. }
  101. if (!data) {
  102. return;
  103. }
  104. this.socket.send(JSON.stringify(data));
  105. },
  106. sendToServer: function (url, data) {
  107. var defer = $.Deferred();
  108. var json = {
  109. jsonrpc: '2.0',
  110. method: 'call',
  111. params: {
  112. data: data
  113. }
  114. }
  115. $.ajax({
  116. type: 'POST',
  117. url: url,
  118. dataType: 'json',
  119. data: JSON.stringify(json),
  120. beforeSend: function(xhr) {
  121. xhr.setRequestHeader('Content-Type', 'application/json');
  122. },
  123. success: function (ok) {
  124. defer.resolve(ok);
  125. },
  126. });
  127. return defer;
  128. },
  129. discoveryPrinters: function () {
  130. var commands = {
  131. action: 'list'
  132. }
  133. this.sendToSocket(commands);
  134. },
  135. print(printer, data) {
  136. if (this.state !== 'online') {
  137. instance.web.notification.do_warn('Atención', 'La impresora está fuera de servicio');
  138. return;
  139. }
  140. if (!printer) {
  141. instance.web.notification.do_warn('Atención', 'La impresora no se encuentra');
  142. return;
  143. }
  144. if (!data) {
  145. instance.web.notification.do_warn('Atención', 'No se encuentran datos para imprimir');
  146. return;
  147. }
  148. var commands = {
  149. action: 'print',
  150. printer: printer,
  151. data: data
  152. }
  153. this.sendToSocket(commands)
  154. }
  155. });
  156. /**
  157. *
  158. */
  159. instance.print_engine.PrinterTopWidget = instance.web.Widget.extend({
  160. template: 'printEngine.PrinterTopWidget',
  161. init: function (parent) {
  162. this._super(parent)
  163. },
  164. start: function () {
  165. this.$el.click(this, this.selectDefaultPrinter);
  166. },
  167. selectDefaultPrinter: function (e) {
  168. e.preventDefault();
  169. var self = e.data;
  170. instance.client.action_manager.do_action({
  171. context: self.session.user_context,
  172. name: 'Impresora por defecto',
  173. type: 'ir.actions.act_window',
  174. res_model: 'print.engine.printer',
  175. views: [[false, 'list'], [false, 'form']],
  176. domain : [['socket_id.user_id', '=', self.session.user_context.uid]],
  177. target: 'new',
  178. flags: {
  179. action_buttons: true
  180. }
  181. })
  182. }
  183. })
  184. /**
  185. *
  186. *
  187. */
  188. instance.print_engine.ping = function () {
  189. if (!instance.print_engine.socket_manager) {
  190. return;
  191. }
  192. instance.web.notification.do_notify('Información', 'Pong')
  193. }
  194. /**
  195. *
  196. */
  197. instance.print_engine.discovery_printers = function (element, action) {
  198. if (!instance.print_engine.socket_manager) {
  199. return;
  200. }
  201. if(!confirm('Ésta acción quitará la impresora por defecto')) {
  202. return;
  203. }
  204. instance.print_engine.socket_manager.discoveryPrinters();
  205. }
  206. /**
  207. *
  208. */
  209. instance.print_engine.test_printer = function (element, action) {
  210. if (!instance.print_engine.socket_manager) {
  211. return;
  212. }
  213. instance.print_engine.socket_manager.test();
  214. }
  215. /**
  216. *
  217. */
  218. instance.print_engine.print = function (element, action) {
  219. if (!instance.print_engine.socket_manager) {
  220. return;
  221. }
  222. instance.print_engine.socket_manager.print();
  223. }
  224. instance.web.client_actions.add('print_engine.ping', 'instance.print_engine.ping')
  225. instance.web.client_actions.add('print_engine.discovery_printers', 'instance.print_engine.discovery_printers')
  226. instance.web.client_actions.add('print_engine.test_printer', 'instance.print_engine.test_printer')
  227. if (instance.web && instance.web.UserMenu) {
  228. instance.web.UserMenu.include({
  229. do_update: function(){
  230. var printer = new openerp.print_engine.PrinterTopWidget(this);
  231. printer.appendTo($('.oe_systray'));
  232. instance.print_engine.socket_manager = new instance.print_engine.SocketManager()
  233. return this._super.apply(this, arguments);
  234. }
  235. });
  236. }
  237. })()