initial commit
[namibia] / public / js / app / admin-workspace / systemxmlrpcclientview.js
1 ;
2 (function () {
3
4         window._w.systemxmlrpcclientview = {
5                 itemId: null,
6                 itemData: null,
7                 formMeta: null,
8                 ti: null,
9                 limited: false,
10                 newItem: false,
11                 ownAccount: true,
12                 onTemplateReady: function (template, event, eventData)
13                 {
14                         _w.systemxmlrpcclientview.itemData = null;
15                         _w.systemxmlrpcclientview.ti = _t[template];
16                         _w.systemxmlrpcclientview.limited = false;
17
18                         _w.systemxmlrpcclientview.setXmlRpcViewFormMeta();
19                         _w.systemxmlrpcclientview.ti.hydrate(_w.systemxmlrpcclientview.formMeta);
20
21                         // Retrieve initial view data.
22                         _w.systemxmlrpcclientview.itemId = App.Util.getUrlParam('id');
23                         _w.systemxmlrpcclientview.newItem = 0 == _w.systemxmlrpcclientview.itemId;
24                         if (_w.systemxmlrpcclientview.newItem)
25                         {
26                                 App.API.getTask(
27                                                 'createXmlRpc', 'Valuation', 'XmlRpc.Create', null,
28                                                 {}, function(){}, _w.taskContractError
29                                         );
30                         }
31                         else
32                         {
33                                 App.API.getTask(
34                                                         'updateXmlRpc:' + _w.systemxmlrpcclientview.itemId, 'Valuation', 'XmlRpc.Update', null,
35                                                         {'id': _w.systemxmlrpcclientview.itemId}, $.proxy(_w.systemxmlrpcclientview._onXmlRpcViewDataReceived, this), _w.taskContractError
36                                                 );
37                         }
38                 },
39                 onTemplatePublished: function (template, event, eventData)
40                 {
41                         if (null != _w.systemxmlrpcclientview.itemData
42                                         || _w.systemxmlrpcclientview.newItem)
43                         {
44                                 _w.systemxmlrpcclientview._setupValidation();
45                         }
46                 },
47                 setXmlRpcViewFormMeta: function (data)
48                 {
49                         if (!data)
50                         {
51                                 data = {};
52                         }
53                         _w.systemxmlrpcclientview.formMeta = {
54                                 'clientName': {
55                                         type: 'input',
56                                         id: 'clientName',
57                                         title: 'Client name',
58                                         value: data.clientName
59                                 },
60                                 'apiPassword': {
61                                         type: 'input',
62                                         id: 'apiPassword',
63                                         title: 'API password',
64                                         value: ''
65                                 },
66                                 'callbackUrl': {
67                                         type: 'input',
68                                         id: 'callbackUrl',
69                                         title: 'Callback URL',
70                                         value: data.callbackUrl
71                                 },
72                                 'triggerSentToSales': {
73                                         type: 'checkbox',
74                                         id: 'triggerSentToSales',
75                                         title: 'Send to sales callback',
76                                         value: data.triggerSentToSales
77                                 }
78                         };
79                 },
80
81                 _onXmlRpcViewDataReceived: function (response)
82                 {
83                         _w.systemxmlrpcclientview.itemId = response.Data.id;
84                         _w.systemxmlrpcclientview.itemData = response.Data;
85                         _w.systemxmlrpcclientview.setXmlRpcViewFormMeta(response.Data);
86                         _w.systemxmlrpcclientview.ti.hydrate(_w.systemxmlrpcclientview.formMeta);
87                         if (_w.systemxmlrpcclientview.ti.published)
88                         {
89                                 _w.systemxmlrpcclientview._setupValidation();
90                         }
91                 },
92
93                 _setupValidation: function ()
94                 {
95                         var rules = {
96                                         'clientName': {required: true},
97                                         'callbackUrl': {required: true}
98                                 };
99                         if (_w.systemxmlrpcclientview.newItem)
100                         {
101                                 rules.apiPassword = {required: true}
102                         }
103                         $('#frmXmlRpcView').validate({
104                                 rules: rules,
105                                 messages: {
106                                         'clientName': "This field is required.",
107                                         'callbackUrl': "This field is required."
108                                 },
109                                 submitHandler: function (form) {
110                                         $('#btnUpdate').prop('disabled', true);
111                                         $('.dbtn').prop('disabled', true);
112                                         var data = _w.systemxmlrpcclientview._prepXmlRpcUpdateData();
113                                         if (_w.systemxmlrpcclientview.newItem)
114                                         {
115                                                 App.API.execTask(
116                                                                 'createXmlRpc', data, {},
117                                                                 _w.systemxmlrpcclientview._onXmlRpcUpdated, _w.taskExecError
118                                                         );
119                                         }
120                                         else
121                                         {
122                                                 App.API.execTask(
123                                                                         'updateXmlRpc:' + _w.systemxmlrpcclientview.itemId, data, {},
124                                                                         _w.systemxmlrpcclientview._onXmlRpcUpdated, _w.taskExecError
125                                                                 );
126                                         }
127                                 }
128                         });
129                         $('#btnUpdate').prop('disabled', false);
130                         if (!_w.systemxmlrpcclientview.newItem)
131                         {
132                                 if (_w.systemxmlrpcclientview.itemData.archived)
133                                 {
134                                         $('#btnActivate').show();
135                                 }
136                                 else
137                                 {
138                                         $('#btnArchive').show();
139                                 }
140                                 $('#btnActivate').click(function () {
141                                         if (!$('#frmXmlRpcView').valid())
142                                         {
143                                                 alert('Please fill in all required fields.');
144                                                 return;
145                                         }
146                                         $('#btnUpdate').prop('disabled', true);
147                                         $('.dbtn').prop('disabled', true);
148                                         var data = _w.systemxmlrpcclientview._prepXmlRpcUpdateData();
149
150                                         App.API.execTask(
151                                                         'updateXmlRpc:' + _w.systemxmlrpcclientview.itemId, data, {},
152                                                         function () {
153                                                                 App.API.getTask(
154                                                                                 'unDeleteXmlRpc:' + _w.systemxmlrpcclientview.itemId, 'Valuation', 'XmlRpc.UnDelete', null,
155                                                                                 {'id': _w.systemxmlrpcclientview.itemId},
156                                                                                 $.proxy(function()
157                                                                                 {
158                                                                                         App.API.execTask(
159                                                                                                         'unDeleteXmlRpc:' + _w.systemxmlrpcclientview.itemId, data, {},
160                                                                                                         _w.systemxmlrpcclientview._onXmlRpcUpdated, _w.taskExecError
161                                                                                                 );
162                                                                                 }, this),
163                                                                                 _w.taskContractError
164                                                                         );
165                                                         }, _w.taskExecError
166                                                 );
167                                 });
168                                 $('#btnArchive').click(function () {
169                                         if (!$('#frmXmlRpcView').valid())
170                                         {
171                                                 alert('Please fill in all required fields.');
172                                                 return;
173                                         }
174                                         $('#btnUpdate').prop('disabled', true);
175                                         var data = _w.systemxmlrpcclientview._prepXmlRpcUpdateData();
176
177                                         App.API.execTask(
178                                                         'updateXmlRpc:' + _w.systemxmlrpcclientview.itemId, data, {},
179                                                         function () {
180                                                                 App.API.getTask(
181                                                                                 'deleteXmlRpc:' + _w.systemxmlrpcclientview.itemId, 'Valuation', 'XmlRpc.Delete', null,
182                                                                                 {'id': _w.systemxmlrpcclientview.itemId},
183                                                                                 $.proxy(function()
184                                                                                 {
185                                                                                         App.API.execTask(
186                                                                                                         'deleteXmlRpc:' + _w.systemxmlrpcclientview.itemId, data, {},
187                                                                                                         _w.systemxmlrpcclientview._onXmlRpcUpdated, _w.taskExecError
188                                                                                                 );
189                                                                                 }, this),
190                                                                                 _w.taskContractError
191                                                                         );
192                                                         }, _w.taskExecError
193                                                 );
194                                 });
195                         }
196                 },
197
198                 _prepXmlRpcUpdateData: function ()
199                 {
200                         var data = _w.systemxmlrpcclientview.ti.harvest();
201                         if (null != data.apiPassword && '' != data.apiPassword)
202                         {
203                                 data.password = data.apiPassword;
204                         }
205                         delete data.apiPassword;
206                         var postData = {XmlRpc: data};
207                         return postData;
208                 },
209
210                 _onXmlRpcUpdated: function (response)
211                 {
212                         window.location.hash = '/xmlrpcclients';
213                 }
214
215         };
216
217 })();