91d2b842f8ab9e71d7c08252a8e06edc28b6e699
[odoo/odoo.git] / addons / edi / static / src / js / edi.js
1 openerp.edi = function(instance) {
2 var _t = instance.web._t;
3 instance.edi = {};
4
5
6 instance.edi.EdiImport = instance.web.Widget.extend({
7
8     init: function(parent,url) {
9         this._super();
10         this.url = url;
11     },
12     start: function() {
13         if (!this.session.session_is_valid()) {
14             instance.redirect('/web/login?redir=' + encodeURIComponent(window.location));
15         } else {
16             this.show_import();
17         }
18     },
19
20     show_import: function() {
21         this.destroy_content();
22         this.do_import();
23     },
24
25     destroy_content: function() {
26         _.each(_.clone(this.getChildren()), function(el) {
27             el.destroy();
28         });
29         this.$el.children().remove();
30     },
31
32     do_import: function() {
33         this.rpc('/edi/import_edi_url', {url: this.url}).done(this.on_imported).fail(this.on_imported_error);
34     },
35     on_imported: function(response) {
36         if ('action' in response) {
37             this.rpc("/web/session/save_session_action", {the_action: response.action}).done(function(key) {
38                 window.location = "/#sa="+encodeURIComponent(key);
39             });
40         }
41         else {
42             $('<div>').dialog({
43                 modal: true,
44                 title: 'Import Successful!',
45                 buttons: {
46                     Ok: function() {
47                         $(this).dialog("close");
48                         window.location = "/";
49                     }
50                 }
51             }).html(_t('The document has been successfully imported!'));
52         }
53     },
54     on_imported_error: function(response){
55         var self = this;
56         var msg = _t("Sorry, the document could not be imported.");
57         if (response.data.message) {
58             msg += "\n " + _t("Reason:") + response.data.message;
59         }
60         var params = {error: response, message: msg};
61         $(instance.web.qweb.render("CrashManager.warning", params)).dialog({
62             title: _t("Document Import Notification"),
63             modal: true,
64             buttons: {
65                 Ok: function() { $(this).dialog("close"); }
66             }
67         });
68     }
69 });
70
71 instance.edi.edi_import = function (url) {
72     instance.session.session_bind().done(function () {
73         new instance.edi.EdiImport(null,url).appendTo($("body").addClass('openerp'));
74     });
75 }
76
77 };
78 // vim:et fdc=0 fdl=0 foldnestmax=3 fdm=syntax: