initial commit
[namibia] / public / js / app / admin-workspace / increments.js
1 ;(function(){
2
3         window._w.increments = {
4
5                 ti : null,
6                 itemId : null,
7
8                 onTemplateReady : function( template, event, eventData )
9                 {
10                         _w.increments.ti = _t[template];
11
12                         _w.increments.initGrid();
13
14                         // Increment grid
15                         if (null == App.API.taskContract('gridIncrement'))
16                         {
17                                 App.API.getTask(
18                                                 'gridIncrement', 'Auction', 'Increment.List', null,
19                                                 {}, _w.increments.loadIncrementGrid, _w.taskContractError
20                                                 );
21                         }
22                         else
23                         {
24                                 _w.increments.loadIncrementGrid( null, {}, {} );
25                         }
26                         if (null == App.API.taskContract('addIncrement'))
27                         {
28                                 App.API.getTask(
29                                                 'addIncrement', 'Auction', 'Increment.Create', null,
30                                                 {}, function () {}, _w.taskContractError
31                                                 );
32                         }
33                 },
34
35                 onTemplatePublished : function( template, event, eventData )
36                 {
37                         _w.increments.ti.hydrateParam('from', {
38                                 type  : 'input',
39                                 id    : 'from',
40                                 title : 'From',
41                                 value : ''
42                         });
43                         _w.increments.ti.hydrateParam('to', {
44                                 type  : 'input',
45                                 id    : 'to',
46                                 title : 'To',
47                                 value : ''
48                         });
49                         _w.increments.ti.hydrateParam('amount', {
50                                 type  : 'input',
51                                 id    : 'amount',
52                                 title : 'Amount',
53                                 value : ''
54                         });
55                         _w.increments._setupValidation();
56                         $('#btnNew').click(function() {
57                                 _w.increments.itemId = null;
58                                 $('#from').val('');
59                                 $('#to').val('');
60                                 $('#amount').val('');
61                                 $('#btnSave').html(' Add ');
62                         });
63                 },
64
65                 initGrid : function()
66                 {
67
68                         _w.increments.ti.hydrate({
69                                 // Increment grid
70                                 'gridIncrementTitle': 'Manage increments',
71                                 'gridIncrementColumnHeaders': {
72                                         constructor: 'GridColumnHeader',
73                                         items: {
74                                                 From: {
75                                                         id: 'gridIncrementColumn_From',
76                                                         title: 'FROM'
77                                                 },
78                                                 To: {
79                                                         id: 'gridIncrementColumn_To',
80                                                         title: 'TO'
81                                                 },
82                                                 Amount: {
83                                                         id: 'gridIncrementColumn_Amount',
84                                                         title: 'AMOUNT'
85                                                 }
86                                         }
87                                 },
88                                 'gridIncrementRowRepeater': {}
89                         });
90
91                 },
92
93                 loadIncrementGrid : function( contract, data, options )
94                 {
95                         App.API.execTask(
96                                         'gridIncrement', data, options,
97                                         _w.increments._onIncrementGridDataReceived, _w.taskExecError
98                                         );
99                 },
100
101                 _onIncrementGridDataReceived : function( response )
102                 {
103                         _w.increments.ti.hydrateParam('gridIncrementRowRepeater', {});
104                         var gridData = [];
105                         for (var i = 0; i < response.Data.length; i++)
106                         {
107                                 var row = response.Data[i];
108                                 gridData.push({
109                                         prepend: {
110                                                 Amount  : 'R '
111                                         },
112                                         rowId : row.id,
113                                         onClick : function(meta) {
114                                                 _w.increments.itemId = meta.rowId;
115                                                 $('#from').val(meta.items.From);
116                                                 $('#to').val(meta.items.To);
117                                                 $('#amount').val(meta.items.Amount);
118                                                 $('#btnSave').html(' Update ');
119                                         },
120                                         items: {
121                                                 'id'     : row.id,
122                                                 'From'   : row.from ? row.from : '',
123                                                 'To'     : row.to ? row.to : '',
124                                                 'Amount' : row.amount ? row.amount : ''
125                                         }
126                                 });
127                         }
128                         _w.increments.ti.hydrateParam('gridIncrementRowRepeater', {
129                                 constructor : 'GridDataRow',
130                                 items       : gridData
131                         });
132                 },
133
134                 _setupValidation : function ()
135                 {
136                         $('#frmIncrementView').validate({
137                                 "rules": {
138                                         name: "required",
139                                         from: {digits: true, required: true},
140                                         to: {digits: true, required: true},
141                                         amount: {number: true, required: true}
142                                 },
143                                 messages: {
144                                         name : "This field is required."
145                                 },
146                                 submitHandler: function( form ) {
147                                         $('#btnSave').prop('disabled', true);
148                                         var data = {
149                                                         Increment: {
150                                                                 from: $('#from').val(),
151                                                                 to: $('#to').val(),
152                                                                 amount: $('#amount').val()
153                                                         }
154                                         };
155                                         if (null == _w.increments.itemId)
156                                         {
157                                                 App.API.execTask(
158                                                                 'addIncrement', data, {},
159                                                                 function() {
160                                                                         $('#from').val('');
161                                                                         $('#to').val('');
162                                                                         $('#amount').val('');
163                                                                         _w.increments.loadIncrementGrid( null, {}, {} );
164                                                                         $('#btnSave').prop('disabled', false);
165                                                                         $('#btnSave').html(' Add ');
166                                                                 }, _w.taskExecError
167                                                         );
168                                         }
169                                         else
170                                         {
171                                                 App.API.getTask(
172                                                                 'updateIncrement' + _w.increments.itemId, 'Auction', 'Increment.Update', null,
173                                                                 {id: _w.increments.itemId}, function () {
174                                                                         App.API.execTask(
175                                                                                         'updateIncrement' + _w.increments.itemId, data, {},
176                                                                                         function() {
177                                                                                                 $('#from').val('');
178                                                                                                 $('#to').val('');
179                                                                                                 $('#amount').val('');
180                                                                                                 _w.increments.loadIncrementGrid( null, {}, {} );
181                                                                                                 $('#btnSave').prop('disabled', false);
182                                                                                                 $('#btnSave').html(' Add ');
183                                                                                         }, _w.taskExecError
184                                                                                 );
185                                                                 }, _w.taskContractError
186                                                                 );
187                                         }
188                                 }
189                         });
190                         $('#btnSave').prop('disabled', false);
191                 }
192
193         };
194
195 })();