@@ -2,6 +2,34 @@ if (typeof contactList === 'undefined' || contactList === null) {
2
2
contactList = { } ;
3
3
} ;
4
4
5
+ ko . extenders . required = function ( target , options ) {
6
+ //add some sub-observables to our observable
7
+ target . hasError = ko . observable ( ) ;
8
+ target . validationMessage = ko . observable ( ) ;
9
+ target . validationClass = ko . observable ( ) ;
10
+
11
+ var regEx = new RegExp ( options . regEx ) ;
12
+ //define a function to do validation
13
+ function validate ( newValue ) {
14
+ target . hasError ( regEx . test ( newValue ) && newValue !== "" ? false : true ) ;
15
+ target . validationClass ( regEx . test ( newValue ) && newValue !== "" ? "form-control" : "form-control has-error" ) ;
16
+ target . validationMessage ( regEx . test ( newValue ) && newValue !== "" ? "" : options . overrideMessage || "This field is required" ) ;
17
+ }
18
+
19
+ //validate whenever the value changes
20
+ target . subscribe ( validate ) ;
21
+
22
+ //return the original observable
23
+ return target ;
24
+ } ;
25
+
26
+ function clearErrors ( obsArr ) {
27
+ for ( var i = 0 ; i < obsArr . length ; i ++ ) {
28
+ obsArr [ i ] . hasError ( false ) ;
29
+ obsArr [ i ] . validationClass ( "form-control" ) ;
30
+ }
31
+ }
32
+
5
33
contactList . contactsViewModel = function ( config ) {
6
34
var self = this ;
7
35
var resx = config . resx ;
@@ -20,6 +48,8 @@ contactList.contactsViewModel = function(config) {
20
48
self . isFormEnabled = ko . observable ( config . settings . isFormEnabled ) ;
21
49
self . isEditMode = ko . observable ( false ) ;
22
50
self . contacts = ko . observableArray ( [ ] ) ;
51
+ self . totalResults = ko . observable ( preloadedData . pageCount ) ;
52
+ self . pageIndex = ko . observable ( 0 ) ;
23
53
24
54
self . selectedContact = new contactList . contactViewModel ( self , config ) ;
25
55
@@ -30,6 +60,7 @@ contactList.contactsViewModel = function(config) {
30
60
self . addContact = function ( ) {
31
61
toggleView ( ) ;
32
62
self . selectedContact . init ( ) ;
63
+ clearErrors ( [ self . selectedContact . firstName , self . selectedContact . lastName , self . selectedContact . phone , self . selectedContact . email ] ) ;
33
64
} ;
34
65
35
66
self . closeEdit = function ( ) {
@@ -67,12 +98,16 @@ contactList.contactsViewModel = function(config) {
67
98
68
99
self . getContacts = function ( ) {
69
100
var params = {
101
+ pageSize : self . pageSize ,
102
+ pageIndex : self . pageIndex ( ) ,
103
+ searchTerm : ""
70
104
} ;
71
105
72
106
util . contactService ( ) . get ( "GetContacts" , params ,
73
107
function ( data ) {
74
108
if ( typeof data !== "undefined" && data != null && data . success === true ) {
75
109
//Success
110
+ self . totalResults ( data . data . totalCount ) ;
76
111
self . load ( data . data ) ;
77
112
} else {
78
113
//Error
@@ -95,6 +130,7 @@ contactList.contactsViewModel = function(config) {
95
130
self . getContacts ( ) ;
96
131
}
97
132
quickSettingsDispatcher . addSubcriber ( moduleId , quickSettingsDispatcher . eventTypes . SAVE , updateView ) ;
133
+ pager . init ( self , 5 , self . refresh , resx ) ;
98
134
} ;
99
135
100
136
self . load = function ( data ) {
@@ -120,36 +156,49 @@ contactList.contactViewModel = function(parentViewModel, config) {
120
156
121
157
self . parentViewModel = parentViewModel ;
122
158
self . contactId = ko . observable ( - 1 ) ;
123
- self . firstName = ko . observable ( '' ) ;
124
- self . lastName = ko . observable ( '' ) ;
125
- self . email = ko . observable ( '' ) ;
126
- self . phone = ko . observable ( '' ) ;
159
+ self . firstName = ko . observable ( '' ) . extend ( { required : { overrideMessage : "Please enter a first name" } } ) ;
160
+ self . lastName = ko . observable ( '' ) . extend ( { required : { overrideMessage : "Please enter a last name" } } ) ;
161
+ self . email = ko . observable ( '' ) . extend ( { required : { overrideMessage : "Please enter a valid email address" , regEx : / ^ \w + ( [ \. - ] ? \w + ) * @ \w + ( [ \. - ] ? \w + ) * ( \. \w { 2 , 3 } ) + $ / } } ) ;
162
+ self . phone = ko . observable ( '' ) . extend ( { required : { overrideMessage : "Please enter a valid phone number in the format: 123-456-7890" , regEx : / ^ ( \+ \d { 1 , 2 } \s ) ? \( ? \d { 3 } \) ? [ \s . - ] \d { 3 } [ \s . - ] \d { 4 } $ / } } ) ;
127
163
self . twitter = ko . observable ( '' ) ;
128
164
129
- self . cancel = function ( ) {
165
+ self . cancel = function ( ) {
166
+ clearErrors ( [ self . firstName , self . lastName , self . email , self . phone ] ) ;
130
167
parentViewModel . closeEdit ( ) ;
131
168
} ;
132
169
133
170
self . deleteContact = function ( data , e ) {
134
- var params = {
135
- contactId : data . contactId ( ) ,
136
- firstName : data . firstName ( ) ,
137
- lastName : data . lastName ( ) ,
138
- email : data . email ( ) ,
139
- phone : data . phone ( ) ,
140
- twitter : data . twitter ( )
141
- } ;
171
+ var opts = {
172
+ callbackTrue : function ( ) {
173
+ var params = {
174
+ contactId : data . contactId ( ) ,
175
+ firstName : data . firstName ( ) ,
176
+ lastName : data . lastName ( ) ,
177
+ email : data . email ( ) ,
178
+ phone : data . phone ( ) ,
179
+ twitter : data . twitter ( )
180
+ } ;
142
181
143
182
util . contactService ( ) . post ( "DeleteContact" , params ,
144
183
function ( data ) {
145
184
//Success
146
185
parentViewModel . refresh ( ) ;
147
186
} ,
148
187
149
- function ( data ) {
150
- //Failure
151
- }
152
- ) ;
188
+ function ( data ) {
189
+ //Failure
190
+ }
191
+ ) ;
192
+ } ,
193
+ text : resx . DeleteConfirm ,
194
+ yesText : resx . Delete ,
195
+ noText : resx . Cancel ,
196
+ title : resx . ConfirmDeleteTitle . replace ( "{0}" , data . firstName ( ) + " " + data . lastName ( ) )
197
+ } ;
198
+
199
+ $ . dnnConfirm ( opts ) ;
200
+
201
+
153
202
} ;
154
203
155
204
self . init = function ( ) {
@@ -170,7 +219,14 @@ contactList.contactViewModel = function(parentViewModel, config) {
170
219
self . twitter ( data . twitter ) ;
171
220
} ;
172
221
173
- self . saveContact = function ( data , e ) {
222
+ self . saveContact = function ( data , e ) {
223
+ self . firstName . valueHasMutated ( ) ;
224
+ self . lastName . valueHasMutated ( ) ;
225
+ self . phone . valueHasMutated ( ) ;
226
+ self . email . valueHasMutated ( ) ;
227
+ if ( ( self . firstName . hasError ( ) || self . lastName . hasError ( ) || self . email . hasError ( ) || self . phone . hasError ( ) ) ) {
228
+ return ;
229
+ }
174
230
var params = {
175
231
contactId : data . contactId ( ) ,
176
232
firstName : data . firstName ( ) ,
0 commit comments