initial commit
[namibia] / public / js / app / admin-workspace / configview.js
1 ;(function(){
2
3         window._w.configview = {
4
5                 itemId : null,
6                 itemData : null,
7                 formMeta : null,
8                 ti : null,
9
10                 onTemplateReady : function( template, event, eventData )
11                 {
12                         _w.configview.ti = _t[template];
13                         _w.configview.setConfigViewFormMeta();
14                         _w.configview.ti.hydrate(_w.configview.formMeta);
15
16                         // Retrieve initial view data.
17                         _w.configview.itemId = 1;
18                         _w.configview.ownAccount = (App.userData.id == _w.configview.itemId)
19                                 ? true
20                                 : false;
21                         App.API.getTask(
22                                 'updateConfig:' + _w.configview.itemId, 'AppConfig', 'Config.Update', _w.configview.itemId,
23                                 {'id':_w.configview.itemId}, $.proxy( _w.configview._onConfigViewDataReceived, this ), _w.taskContractError
24                         );
25                 },
26
27                 onTemplatePublished : function( template, event, eventData )
28                 {
29                         if (null != _w.configview.itemData)
30                         {
31                                 _w.configview._setupValidation();
32                         }
33                 },
34
35                 setConfigViewFormMeta : function( data )
36                 {
37                         if (!data)
38                         {
39                                 data = {};
40                         }
41                         _w.configview.formMeta = {
42                                 'sourceEmailAddress' : {
43                                         type  : 'input',
44                                         id    : 'sourceEmailAddress',
45                                         title : 'Source email address',
46                                         value : data.sourceEmailAddress ? data.sourceEmailAddress : '',
47                                         maxlength : 250
48                                 },
49                                 'sourceMobileAddress' : {
50                                         type  : 'mobile',
51                                         id    : 'sourceMobileAddress',
52                                         title : 'Source mobile address',
53                                         value : data.sourceMobileAddress ? data.sourceMobileAddress : '',
54                                         maxlength : 20
55                                 },
56                                 'adminEmailAddress' : {
57                                         type  : 'input',
58                                         id    : 'adminEmailAddress',
59                                         title : 'Administrative email address',
60                                         value : data.adminEmailAddress ? data.adminEmailAddress : '',
61                                         maxlength : 250
62                                 },
63                                 'priceGuideOpenDays' : {
64                                         type  : 'input',
65                                         id    : 'priceGuideOpenDays',
66                                         title : 'Open Hours',
67                                         value : data.priceGuideOpenDays ? data.priceGuideOpenDays : '',
68                                         maxlength : 2
69                                 },
70                                 'priceGuideCompletionDays' : {
71                                         type  : 'input',
72                                         id    : 'priceGuideCompletionDays',
73                                         title : 'Completion days',
74                                         value : data.priceGuideCompletionDays ? data.priceGuideCompletionDays : '',
75                                         maxlength : 2
76                                 },
77                                 'auctionMaxDays' : {
78                                         type  : 'input',
79                                         id    : 'auctionMaxDays',
80                                         title : 'Auction max days',
81                                         value : data.auctionMaxDays ? data.auctionMaxDays : '',
82                                                         maxlength : 2
83                                 }
84                         };
85                 },
86
87                 _onConfigViewDataReceived : function( response )
88                 {
89                         _w.configview.itemData = response.Data;
90                         _w.configview.setConfigViewFormMeta(response.Data);
91                         _w.configview.ti.hydrate(_w.configview.formMeta);
92                         if (_w.configview.ti.published)
93                         {
94                                 _w.configview._setupValidation();
95                         }
96                 },
97
98                 _setupValidation : function ()
99                 {
100                         $('#frmConfigView').validate({
101                                 "rules": {
102                                         priceGuideOpenDays: {required: true, minlength: 1, maxlength: 2, digits: true},
103                                         priceGuideCompletionDays: {required: true, minlength: 1, maxlength: 2, digits: true},
104                                         auctionMaxDays: {required: true, minlength: 1, maxlength: 2, digits: true},
105                                         sourceMobileAddress: {required: false, mobile: true},
106                                         sourceEmailAddress: {required: true, email: true},
107                                         adminEmailAddress: {required: true, email: true}
108                                 },
109                                 messages: {
110                                         sourceEmailAddress: {
111                                                 email: "Must be a valid email address."
112                                         },
113                                         adminEmailAddress: {
114                                                 email: "Must be a valid email address."
115                                         },
116                                         priceGuideOpenDays: {
117                                                 minlength: "Must be 1 to 2 digit.",
118                                                 maxlength: "Must be 1 to 2 digit."
119                                         },
120                                         priceGuideCompletionDays: {
121                                                 minlength: "Must be 1 to 2 digits.",
122                                                 maxlength: "Must be 1 to 2 digits."
123                                         },
124                                         auctionMaxDays: {
125                                                 minlength: "Must be 1 to 2 digits.",
126                                                 maxlength: "Must be 1 to 2 digits."
127                                         }
128                                 },
129                                 submitHandler: function( form ) {
130                                         $('#btnUpdate').prop('disabled', true);
131                                         var data = _w.configview._prepConfigUpdateData();
132                                         App.API.execTask(
133                                                         'updateConfig:' + _w.configview.itemId, data, {},
134                                                         _w.configview._onConfigUpdated, _w.taskExecError
135                                                 );
136                                 }
137                         });
138                         $('#btnUpdate').prop('disabled', false);
139                 },
140
141                 _prepConfigUpdateData : function()
142                 {
143                         var data = {Config:_w.configview.ti.harvest()};
144                         if ('' == data.Config.sourceMobileAddress)
145                         {
146                                 data.Config.sourceMobileAddress = null;
147                         }
148                         return data;
149                 },
150
151                 _onConfigUpdated : function( response )
152                 {
153                         window.location.hash = '/systemsettings';
154                 }
155
156         };
157
158 })();