initial commit
[namibia] / public / js / app / ajax.js
1 ;
2 (function () {
3
4         _App.Ajax = function ()
5         {
6
7                 this.initialize();
8
9         };
10
11         _App.Ajax.prototype =
12         {
13
14                 exportCounter: 0,
15
16                 initialize: function ()
17                 {
18
19                 },
20
21                 DOWNLOAD: function (args)
22                 {
23                         var target = !args.direct
24                                 ? 'target="_blank"'
25                                 : '';
26                         $('<form id="downloadForm' + this.exportCounter + '" method="get" ' + target + ' action="' + args.url + '">'
27                           + '</form>').appendTo('body').submit();
28                         $('#downloadForm' + this.exportCounter).remove();
29                         this.exportCounter++;
30                 },
31
32                 EXPORT: function (args)
33                 {
34                         args.data = args.data[0];
35                         inputs = '';
36                         inputs += '<input type="hidden" name="Contract" value="' + args.data.Contract + '">';
37                         for (var group in args.data.Packet)
38                         {
39                                 for (var param in args.data.Packet[group])
40                                 {
41                                         inputs += '<input type="hidden" name="Packet[' + group + '][' + param + ']" value="' + args.data.Packet[group][param] + '">';
42                                 }
43                         }
44                         if (args.data.Options)
45                         {
46                                 for (var param in args.data.Options)
47                                 {
48                                         inputs += '<input type="hidden" name="Options[' + param + ']" value="' + args.data.Options[param] + '">';
49                                 }
50                         }
51                         var target = !args.direct
52                                 ? 'target="_blank"'
53                                 : '';
54                         $('<form id="exportForm' + this.exportCounter + '" method="post" ' + target + ' action="' + args.url + '">'
55                           + inputs
56                           + '</form>').appendTo('body').submit();
57                         $('#exportForm' + this.exportCounter).remove();
58                         this.exportCounter++;
59                 },
60
61                 JSON: function (args, callback, errorCallback)
62                 {
63
64                         args.type = 'POST';
65                         args.dataType = 'json';
66                         args.url = window.location.protocol
67                                    + '//' + window.location.hostname
68                                    + ':' + window.location.port
69                                    + ( args.url.indexOf('/') === 0
70                                         ? args.url
71                                         : '/' + args.url);
72                         args.data = JSON.stringify(args.data);
73                         $.ajax(args)
74                                 .done(function (data) {
75                                         (undefined != args.id)
76                                                 ? callback(args.id, data)
77                                                 : callback(data);
78                                 })
79                                 .fail(function (jqXHR, textStatus, errorThrown) {
80                                         if (errorCallback)
81                                         {
82                                                 (undefined != args.id)
83                                                         ? errorCallback(args.id, textStatus, errorThrown)
84                                                         : errorCallback(textStatus, errorThrown);
85                                         }
86                                 });
87
88                 },
89
90                 SCRIPT: function (args, callback, errorCallback)
91                 {
92                         args.type = 'GET';
93                         args.dataType = 'script';
94                         args.url = window.location.protocol
95                                    + '//' + window.location.hostname
96                                    + ':' + window.location.port
97                                    + ( args.url.indexOf('/') === 0
98                                         ? args.url
99                                         : '/' + args.url);
100                         $.ajax(args)
101                                 .done(function (data) {
102                                         if (callback)
103                                         {
104                                                 (args.id)
105                                                         ? callback(args.id, data)
106                                                         : callback(data);
107                                         }
108                                 })
109                                 .fail(function (jqXHR, textStatus, errorThrown) {
110                                         if (errorCallback)
111                                         {
112                                                 (args.id)
113                                                         ? errorCallback(args.id, textStatus, errorThrown)
114                                                         : errorCallback(textStatus, errorThrown);
115                                         }
116                                 });
117
118                 },
119
120                 POST: function (args, callback, errorCallback)
121                 {
122
123                         args.type = 'POST';
124                         args.url = window.location.protocol
125                                    + '//' + window.location.hostname
126                                    + ':' + window.location.port
127                                    + ( args.url.indexOf('/') === 0
128                                         ? args.url
129                                         : '/' + args.url);
130                         $.ajax(args)
131                                 .done(function (data) {
132                                         if (callback)
133                                         {
134                                                 (args.id)
135                                                         ? callback(args.id, data)
136                                                         : callback(data);
137                                         }
138                                 })
139                                 .fail(function (jqXHR, textStatus, errorThrown) {
140                                         if (errorCallback)
141                                         {
142                                                 (args.id)
143                                                         ? errorCallback(args.id, textStatus, errorThrown)
144                                                         : errorCallback(textStatus, errorThrown);
145                                         }
146                                 });
147
148                 },
149
150                 GET: function (args, callback, errorCallback)
151                 {
152
153                         args.type = 'GET';
154                         args.url = window.location.protocol
155                                    + '//' + window.location.hostname
156                                    + ':' + window.location.port
157                                    + ( args.url.indexOf('/') === 0
158                                         ? args.url
159                                         : '/' + args.url);
160                         $.ajax(args)
161                                 .done(function (data) {
162                                         if (callback)
163                                         {
164                                                 (args.id)
165                                                         ? callback(args.id, data)
166                                                         : callback(data);
167                                         }
168                                 })
169                                 .fail(function (jqXHR, textStatus, errorThrown) {
170                                         if (errorCallback)
171                                         {
172                                                 (args.id)
173                                                         ? errorCallback(args.id, textStatus, errorThrown)
174                                                         : errorCallback(textStatus, errorThrown);
175                                         }
176                                 });
177
178                 }
179
180         };
181
182 })();