123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281 |
- (function () {
- var instance = openerp
- openerp.print_engine = {}
-
- /**
- *
- */
- instance.print_engine.SocketManager = instance.web.Class.extend({
- init: function () {
- this.socket = null;
- this.state = 'offline';
- this.attemps = 0;
- this.start();
- },
- start: function () {
- var self = this;
- this.getSockets().then(function () {
- self.connect();
- });
- },
- getSockets: function () {
- var defer = $.Deferred();
- var self = this;
- $.get('/print_engine/sockets').done(function (data) {
- self.sockets = data.sockets;
- defer.resolve(data.sockets);
- }).fail(function (error){
- defer.reject(error);
- });
- return defer;
- },
- urlize: function (info) {
- return info.protocol + '://' + info.host + ':' + info.port + info.path;
- },
- connect: function () {
- if (this.sockets.length === 0) {
- return
- }
- var self = this;
- var url = this.urlize(this.sockets[0]);
- if (this.socket) {
- this.socket = null;
- }
-
- try {
- this.socket = new WebSocket(url);
- this.socket.onopen = function (e) {
- self.handleOpen(e);
- };
- this.socket.onclose = function (e) {
- self.handleClose(e);
- };
- this.socket.onerror = function (e) {
- self.handleError(e);
- };
- this.socket.onmessage = function (e) {
- self.handleMessage(e);
- };
- } catch(e) {
- // Ignore this error
- }
- },
- reconnect: function () {
- this.state = 'reconnecting';
- $('#printer-status').removeClass();
- $('#printer-status').addClass('printer-status-reconnect');
- var self = this;
- if (this.attemps === 3) {
- this.state = 'offline';
- $('#printer-status').removeClass();
- $('#printer-status').addClass('printer-status-offline');
- return;
- }
- setTimeout(function () {
- self.connect();
- self.attemps = self.attemps + 1;
- }, 3000);
- },
- handleOpen: function (e) {
- this.state = 'online';
- $('#printer-status').removeClass();
- $('#printer-status').addClass('printer-status-online');
- },
- handleClose: function (e) {
- this.reconnect();
- },
- handleMessage: function (e) {
- var obj = JSON.parse(e.data);
- if (obj.printers) {
- this.sendToServer('/print_engine/update', e.data).then(function () {
- instance.client.action_manager.inner_widget.views['form'].controller.reload();
- instance.web.notification.do_notify('Información', 'Impresoras actualizadas con éxito');
- });
- }
- },
- handleError: function (e) {
- // Ignore this error
- },
- sendToSocket: function (data) {
- if (this.state !== 'online') {
- return;
- }
- if (!data) {
- return;
- }
- this.socket.send(JSON.stringify(data));
- },
- sendToServer: function (url, data) {
- var defer = $.Deferred();
- var json = {
- jsonrpc: '2.0',
- method: 'call',
- params: {
- data: data
- }
- }
- $.ajax({
- type: 'POST',
- url: url,
- dataType: 'json',
- data: JSON.stringify(json),
- beforeSend: function(xhr) {
- xhr.setRequestHeader('Content-Type', 'application/json');
- },
- success: function (ok) {
- defer.resolve(ok);
- },
- });
- return defer;
- },
- discoveryPrinters: function () {
- var commands = {
- action: 'list'
- }
- this.sendToSocket(commands);
- },
- print(printer, data) {
- if (this.state !== 'online') {
- instance.web.notification.do_warn('Atención', 'La impresora está fuera de servicio');
- return;
- }
- if (!printer) {
- instance.web.notification.do_warn('Atención', 'La impresora no se encuentra');
- return;
- }
- if (!data) {
- instance.web.notification.do_warn('Atención', 'No se encuentran datos para imprimir');
- return;
- }
- var commands = {
- action: 'print',
- printer: printer,
- data: data
- }
- this.sendToSocket(commands)
- }
- });
- /**
- *
- */
- instance.print_engine.PrinterTopWidget = instance.web.Widget.extend({
- template: 'printEngine.PrinterTopWidget',
- init: function (parent) {
- this._super(parent)
- },
- start: function () {
- this.$el.click(this, this.selectDefaultPrinter);
- },
- selectDefaultPrinter: function (e) {
- e.preventDefault();
- var self = e.data;
- instance.client.action_manager.do_action({
- context: self.session.user_context,
- name: 'Impresora por defecto',
- type: 'ir.actions.act_window',
- res_model: 'print.engine.printer',
- views: [[false, 'list'], [false, 'form']],
- domain : [['socket_id.user_id', '=', self.session.user_context.uid]],
- target: 'new',
- flags: {
- action_buttons: true
- }
- })
- }
- })
- /**
- *
- *
- */
- instance.print_engine.ping = function () {
- if (!instance.print_engine.socket_manager) {
- return;
- }
- instance.web.notification.do_notify('Información', 'Pong')
- }
-
- /**
- *
- */
- instance.print_engine.discovery_printers = function (element, action) {
- if (!instance.print_engine.socket_manager) {
- return;
- }
- if(!confirm('Ésta acción quitará la impresora por defecto')) {
- return;
- }
- instance.print_engine.socket_manager.discoveryPrinters();
- }
-
- /**
- *
- */
- instance.print_engine.test_printer = function (element, action) {
- if (!instance.print_engine.socket_manager) {
- return;
- }
- instance.print_engine.socket_manager.test();
- }
- /**
- *
- */
- instance.print_engine.print = function (element, action) {
- if (!instance.print_engine.socket_manager) {
- return;
- }
- instance.print_engine.socket_manager.print();
- }
- instance.web.client_actions.add('print_engine.ping', 'instance.print_engine.ping')
- instance.web.client_actions.add('print_engine.discovery_printers', 'instance.print_engine.discovery_printers')
- instance.web.client_actions.add('print_engine.test_printer', 'instance.print_engine.test_printer')
- if (instance.web && instance.web.UserMenu) {
- instance.web.UserMenu.include({
- do_update: function(){
- var printer = new openerp.print_engine.PrinterTopWidget(this);
- printer.appendTo($('.oe_systray'));
- instance.print_engine.socket_manager = new instance.print_engine.SocketManager()
- return this._super.apply(this, arguments);
- }
- });
- }
- })()
|