click to refresh errors
[namibia] / public / js / app / admin-workspace / opendays.js
1 ;(function(){
2
3         window._w.opendays = {
4
5                 ti : null,
6                 itemId : null,
7
8                 onTemplateReady : function( template, event, eventData )
9                 {
10                         _w.opendays.ti = _t[template];
11
12                         _w.opendays.initGrid();
13
14                         // Openday grid
15                         App.DataStore.loadSelectListData('groups', true, 'Company', 'Group.SelectList');
16                         App.DataStore.listen('openDayChecklistBuilder', 'group');
17                         if (null == App.API.taskContract('gridOpenday'))
18                         {
19                                 App.API.getTask(
20                                                 'gridOpenday', 'Auction', 'OpenDay.List', null,
21                                                 {}, _w.opendays.loadOpendayGrid, _w.taskContractError
22                                                 );
23                         }
24                         else
25                         {
26                                 _w.opendays.loadOpendayGrid( null, {}, {} );
27                         }
28                         if (null == App.API.taskContract('addOpenday'))
29                         {
30                                 App.API.getTask(
31                                                 'addOpenday', 'Auction', 'OpenDay.Create', null,
32                                                 {}, function () {}, _w.taskContractError
33                                                 );
34                         }
35                 },
36
37                 onTemplatePublished : function( template, event, eventData )
38                 {
39                         _w.opendays.ti.hydrateParam('openday', {
40                                 type  : 'date',
41                                 id    : 'openday',
42                                 title : 'Date',
43                                 value : '',
44                                 dtopt : {
45                                         daysOfWeekDisabled: [0,1,2,3,4,5]
46                                 }
47                         });
48                         _w.opendays.ti.hydrateParam('allgroups', {
49                                 type            : 'checkbox',
50                                 id              : 'allgroups',
51                                 title           : '<b>Select all</b>',
52                                 value           : false,
53                                 onChange        : function() {
54                                         $('.companyGroup').prop('checked', $(this).is(':checked'));
55                                 }
56                         });
57                         _w.opendays.ti.hydrateParam('groups', {
58                                 type            : 'checkGroup',
59                                 id              : 'groups',
60                                 dataStoreId : 'groups',
61                                 itemName    : 'companyGroup',
62                                 data            : {}
63                         });
64                         _w.opendays._setupValidation();
65                         $('#btnNew').click(function() {
66                                 _w.opendays.itemId = null;
67                                 $('#openday').val('');
68                                 $('#allgroups').prop('checked', false);
69                                 $('.companyGroup').prop('checked', false);
70                                 $('#btnSave').html(' Add ');
71                                 $('#btnDelete').hide();
72                                 $('#openday').attr('disabled', false);
73                         });
74                         $('#btnDelete').click(function() {
75                                 App.API.getTask(
76                                                 'deleteOpenday' + _w.opendays.itemId, 'Auction', 'OpenDay.Delete', null,
77                                                 {id: _w.opendays.itemId}, function () {
78                                                         App.API.execTask(
79                                                                         'deleteOpenday' + _w.opendays.itemId, {}, {},
80                                                                         function() {
81                                                                                 $('#btnDelete').hide();
82                                                                                 $('#openday').val('');
83                                                                                 $('#allgroups').prop('checked', false);
84                                                                                 $('.companyGroup').prop('checked', false);
85                                                                                 _w.opendays.loadOpendayGrid( null, {}, {} );
86                                                                                 $('#btnSave').prop('disabled', false);
87                                                                                 $('#btnSave').html(' Add ');
88                                                                                 $('#openday').attr('disabled', false);
89                                                                                 _w.opendays.itemId = null;
90                                                                         }, function(error) {
91                                                                                 alert(error.StatusReason);
92                                                                         }
93                                                                 );
94                                                 }, _w.taskContractError
95                                         );
96                         });
97                 },
98
99                 initGrid : function()
100                 {
101
102                         _w.opendays.ti.hydrate({
103                                 // Openday grid
104                                 'gridOpendayTitle': 'Manage open auction days',
105                                 'gridOpendayColumnHeaders': {
106                                         constructor: 'GridColumnHeader',
107                                         items: {
108                                                 Openday: {
109                                                         id: 'gridOpendayColumn_Openday',
110                                                         title: 'OPEN DAY'
111                                                 }
112                                         }
113                                 },
114                                 'gridOpendayRowRepeater': {}
115                         });
116
117                 },
118
119                 loadOpendayGrid : function( contract, data, options )
120                 {
121                         App.API.execTask(
122                                         'gridOpenday', data, options,
123                                         _w.opendays._onOpendayGridDataReceived, _w.taskExecError
124                                         );
125                 },
126
127                 _onOpendayGridDataReceived : function( response )
128                 {
129                         _w.opendays.ti.hydrateParam('gridOpendayRowRepeater', {});
130                         var gridData = [];
131                         for (var i = 0; i < response.Data.length; i++)
132                         {
133                                 var row = response.Data[i];
134                                 gridData.push({
135                                         prepend: {
136                                                 Amount  : 'R '
137                                         },
138                                         rowId : row.id,
139                                         onClick : $.proxy(function(row, meta) {
140                                                 _w.opendays.itemId = meta.rowId;
141                                                 $('#openday').val(meta.items.Openday);
142                                                 $('#openday').attr('disabled', true);
143                                                 $('.companyGroup').prop('checked', false);
144                                                 for (var i in row.groups)
145                                                 {
146                                                         $('input[data-id="' + row.groups[i].id + '"]').prop('checked', true);
147                                                 }
148                                                 $('#btnSave').html(' Update ');
149                                                 $('#btnDelete').show();
150                                         }, this, row),
151                                         items: {
152                                                 'id'      : row.id,
153                                                 'Openday' : row.openDate ? row.openDate : ''
154                                         }
155                                 });
156                         }
157                         _w.opendays.ti.hydrateParam('gridOpendayRowRepeater', {
158                                 constructor : 'GridDataRow',
159                                 items       : gridData
160                         });
161                 },
162
163                 _setupValidation : function ()
164                 {
165                         $('#frmOpendayView').validate({
166                                 "rules": {
167                                         openday: {required: true, dateISO: true}
168                                 },
169                                 messages: {
170                                         openday : {
171                                                 dateISO: "Must be a valid date."
172                                         }
173                                 },
174                                 submitHandler: function( form ) {
175                                         $('#btnSave').prop('disabled', true);
176                                         var items = [];
177                                         $.each($.find('.companyGroup'), function(i, input) {
178                                                 if ( $(input).prop('checked') )
179                                                 {
180                                                         items.push({
181                                                                 'id': $(input).attr('data-id')
182                                                         });
183                                                 }
184                                         });
185                                         var data = {
186                                                         OpenDay: {
187                                                                 openDate: $('#openday').val(),
188                                                                 groups: items
189                                                         }
190                                         };
191                                         if (null == _w.opendays.itemId)
192                                         {
193                                                 App.API.execTask(
194                                                                 'addOpenday', data, {},
195                                                                 function() {
196                                                                         $('#btnDelete').hide();
197                                                                         $('#openday').val('');
198                                                                         $('#allgroups').prop('checked', false);
199                                                                         $('.companyGroup').prop('checked', false);
200                                                                         _w.opendays.loadOpendayGrid( null, {}, {} );
201                                                                         $('#btnSave').prop('disabled', false);
202                                                                         $('#btnSave').html(' Add ');
203                                                                         $('#openday').attr('disabled', false);
204                                                                         _w.opendays.itemId = null;
205                                                                 }, _w.taskExecError
206                                                         );
207                                         }
208                                         else
209                                         {
210                                                 App.API.getTask(
211                                                                 'updateOpenday' + _w.opendays.itemId, 'Auction', 'OpenDay.Update', null,
212                                                                 {id: _w.opendays.itemId}, function () {
213                                                                         App.API.execTask(
214                                                                                         'updateOpenday' + _w.opendays.itemId, data, {},
215                                                                                         function() {
216                                                                                                 $('#btnDelete').hide();
217                                                                                                 $('#openday').val('');
218                                                                                                 $('#allgroups').prop('checked', false);
219                                                                                                 $('.companyGroup').prop('checked', false);
220                                                                                                 _w.opendays.loadOpendayGrid( null, {}, {} );
221                                                                                                 $('#btnSave').prop('disabled', false);
222                                                                                                 $('#btnSave').html(' Add ');
223                                                                                                 $('#openday').attr('disabled', false);
224                                                                                                 _w.opendays.itemId = null;
225                                                                                         }, _w.taskExecError
226                                                                                 );
227                                                                 }, _w.taskContractError
228                                                                 );
229                                         }
230                                 }
231                         });
232                         $('#btnSave').prop('disabled', false);
233                 }
234
235         };
236
237 })();