@@ -18,11 +18,15 @@ function wildlifespotter(wsParams, imagePrefix, recordValues, placeholders) {
18
18
19
19
var selectedIndicies = { } ;
20
20
21
+ // Default save button to disabled until a selection has been made.
22
+ $ ( '#btnSave' ) . attr ( 'disabled' , 'disabled' ) ;
23
+
21
24
// selection
22
25
$ ( '#ct-container' ) . on ( 'click' , '.ws-selector' , function ( ) {
23
26
var $this = $ ( this ) ;
24
27
var index = $this . closest ( '[data-item-index]' ) . data ( 'item-index' ) ;
25
- toggleIndex ( index ) ;
28
+ var validationtype = $this . data ( 'validationType' ) ;
29
+ toggleIndex ( index , validationtype ) ;
26
30
} ) ;
27
31
28
32
$ ( '#ct-container' ) . on ( 'click' , '.animalDelete' , function ( ) {
@@ -31,16 +35,36 @@ function wildlifespotter(wsParams, imagePrefix, recordValues, placeholders) {
31
35
deselectIndex ( index ) ;
32
36
} ) ;
33
37
34
- function toggleIndex ( index ) {
38
+ $ ( 'input[name=recordValues\\.0\\.noAnimalsVisible]' ) . change ( function ( ) {
39
+ checkCheckboxValues ( ) ;
40
+ } ) ;
41
+
42
+ $ ( 'input[name=recordValues\\.0\\.problemWithImage]' ) . change ( function ( ) {
43
+ checkCheckboxValues ( ) ;
44
+ } ) ;
45
+
46
+ function checkCheckboxValues ( ) {
47
+ var q1 = $ ( 'input[name=recordValues\\.0\\.noAnimalsVisible]:checked' ) . val ( ) ;
48
+ var q2 = $ ( 'input[name=recordValues\\.0\\.problemWithImage]:checked' ) . val ( ) ;
49
+ if ( ! q1 && ! q2 ) {
50
+ $ ( '#btnSave' ) . attr ( 'disabled' , 'disabled' ) ;
51
+ } else {
52
+ $ ( '#btnSave' ) . removeAttr ( 'disabled' ) ;
53
+ }
54
+ }
55
+
56
+ function toggleIndex ( index , validationType = "speciesWithCount" ) {
35
57
if ( selectedIndicies . hasOwnProperty ( index ) ) {
36
58
deselectIndex ( index ) ;
37
59
} else {
38
- selectIndex ( index ) ;
60
+ selectIndex ( index , validationType ) ;
39
61
}
40
62
}
41
63
42
- function selectIndex ( index ) {
43
- selectedIndicies [ index ] = { count : 1 , notes : '' , editorOpen : false } ;
64
+ function selectIndex ( index , validationType = "speciesWithCount" ) {
65
+ var count = 0 ;
66
+ if ( validationType === "speciesOnly" ) count = 1 ;
67
+ selectedIndicies [ index ] = { count : count , notes : '' , editorOpen : false , init : true } ;
44
68
syncSelections ( ) ;
45
69
}
46
70
@@ -50,14 +74,17 @@ function wildlifespotter(wsParams, imagePrefix, recordValues, placeholders) {
50
74
}
51
75
52
76
function syncSelections ( ) {
53
- var usKeys = _ . chain ( selectedIndicies ) . keys ( ) . filter ( function ( idx ) { return selectedIndicies [ idx ] . count > 0 ; } ) ;
77
+ var usKeys = _ . chain ( selectedIndicies ) . keys ( ) . filter ( function ( idx ) {
78
+ return selectedIndicies [ idx ] . count >= 0 ;
79
+ } ) ;
54
80
var dataItemIndexes = usKeys . map ( function ( v , i , l ) { return "[data-item-index='" + v + "']" } ) ;
55
81
var wsSelectionIndicator = dataItemIndexes . map ( function ( v , i , l ) { return v + " .ws-selected" ; } ) ;
56
82
var wsSelectorIndicator = dataItemIndexes . map ( function ( v , i , l ) { return v + " .ws-selector" ; } ) ;
57
83
$ ( wsSelectionIndicator . value ( ) . join ( ", " ) ) . addClass ( 'selected' ) ;
58
84
$ ( wsSelectorIndicator . value ( ) . join ( ", " ) ) . attr ( 'aria-selected' , 'true' ) ;
59
85
$ ( '[data-item-index]:not(' + dataItemIndexes . value ( ) . join ( ',' ) + ') .ws-selected' ) . removeClass ( 'selected' ) . attr ( 'aria-selected' , 'false' ) ;
60
86
$ ( '[data-item-index]:not(' + dataItemIndexes . value ( ) . join ( ',' ) + ') .ws-selector' ) . attr ( 'aria-selected' , 'false' ) ;
87
+
61
88
var length = usKeys . value ( ) . length ;
62
89
if ( length == 0 ) {
63
90
hideSelectionPanel ( ) ;
@@ -83,17 +110,13 @@ function wildlifespotter(wsParams, imagePrefix, recordValues, placeholders) {
83
110
return {
84
111
index : v ,
85
112
name : wsParams . animals [ v ] . vernacularName ,
86
- options : _ ( [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ] ) . map ( function ( opt , i ) {
87
- return {
88
- val : opt ,
89
- selected : selectedIndicies [ v ] . count == opt ? 'selected' : '' ,
90
- isSelected : selectedIndicies [ v ] . count == opt ? 'true' : 'false'
91
- } ;
92
- } ) ,
113
+ curval : selectedIndicies [ v ] . count ,
93
114
comment : selectedIndicies [ v ] . comment
94
115
} ;
116
+
95
117
} ) . sortBy ( function ( o ) { return o . index ; } ) . value ( )
96
118
} ;
119
+
97
120
mu . replaceTemplate ( parent , 'status-detail-list-template' , templateObj ) ;
98
121
99
122
parent . show ( ) ;
@@ -110,14 +133,20 @@ function wildlifespotter(wsParams, imagePrefix, recordValues, placeholders) {
110
133
generateFormFields ( ) ;
111
134
} ) ;
112
135
113
- $ ( '#ct-container' ) . on ( 'change' , 'select.numAnimals' , function ( ) {
136
+ //$('#ct-container').on('change', 'select.numAnimals', function() {
137
+ $ ( '#ct-container' ) . on ( 'change' , 'input.numAnimals' , function ( ) {
114
138
var $this = $ ( this ) ;
115
139
var idx = $this . closest ( '[data-item-index]' ) . data ( 'item-index' ) ;
116
140
var count = $this . val ( ) ;
141
+ // console.log("value change: " + count);
117
142
selectedIndicies [ idx ] . count = parseInt ( count ) ;
118
143
generateFormFields ( ) ;
119
144
} ) ;
120
145
146
+ $ ( '.input-group-btn-vertical' ) . click ( function ( ) {
147
+ $ ( '#ct-container' ) . trigger ( "change" ) ;
148
+ } ) ;
149
+
121
150
$ ( '#ct-container' ) . on ( 'click' , '.editCommentButton' , function ( ) {
122
151
var $this = $ ( this ) ;
123
152
var idx = $this . closest ( '[data-item-index]' ) . data ( 'item-index' ) ;
@@ -128,6 +157,19 @@ function wildlifespotter(wsParams, imagePrefix, recordValues, placeholders) {
128
157
selectedIndicies [ idx ] . editorOpen = true ;
129
158
} ) ;
130
159
160
+ $ ( "#ct-container" ) . on ( 'keydown' , '.numAnimals' , function ( e ) {
161
+ // Allow: backspace, delete, tab, escape, enter and .
162
+ if ( $ . inArray ( e . keyCode , [ 46 , 8 , 9 , 27 , 13 , 110 , 190 ] ) !== - 1 || ( e . keyCode === 65 && e . ctrlKey === true ) || ( e . keyCode >= 35 && e . keyCode <= 40 ) ) {
163
+ // console.log("key " + e.keyCode + " allowed");
164
+ return ;
165
+ }
166
+
167
+ if ( ( e . shiftKey || ( e . keyCode < 48 || e . keyCode > 57 ) ) && ( e . keyCode < 96 || e . keyCode > 105 ) ) {
168
+ // console.log("key " + e.keyCode + " not allowed");
169
+ e . preventDefault ( ) ;
170
+ }
171
+ } ) ;
172
+
131
173
$ ( '#ct-container' ) . on ( 'click' , '.saveCommentButton' , function ( ) {
132
174
var $this = $ ( this ) ;
133
175
var idx = $this . closest ( '[data-item-index]' ) . data ( 'item-index' ) ;
@@ -313,19 +355,27 @@ function wildlifespotter(wsParams, imagePrefix, recordValues, placeholders) {
313
355
function generateFormFields ( ) {
314
356
var $ctFields = $ ( '#ct-fields' ) ;
315
357
$ctFields . empty ( ) ;
358
+ var enableSubmit = true ;
316
359
if ( _ . keys ( selectedIndicies ) . length > 0 ) {
317
360
$ ( 'input[name=recordValues\\.0\\.noAnimalsVisible]' ) . removeAttr ( 'checked' ) ;
318
361
$ ( 'input[name=recordValues\\.0\\.problemWithImage]' ) . removeAttr ( 'checked' ) ;
319
362
if ( recordValues && recordValues [ 0 ] ) {
320
363
delete recordValues [ 0 ] . noAnimalsVisible ;
321
364
delete recordValues [ 0 ] . problemWithImage ;
322
365
}
366
+ _ . each ( selectedIndicies , function ( value , key , list ) {
367
+ if ( value . count === 0 ) enableSubmit = false ;
368
+ } ) ;
323
369
} else {
324
370
if ( recordValues && recordValues [ 0 ] ) {
325
371
delete recordValues [ 0 ] . vernacularName ;
326
372
delete recordValues [ 0 ] . scientificName ;
327
373
delete recordValues [ 0 ] . individualCount ;
328
374
}
375
+
376
+ var q1 = $ ( 'input[name=recordValues\\.0\\.noAnimalsVisible]:checked' ) . val ( ) ;
377
+ var q2 = $ ( 'input[name=recordValues\\.0\\.problemWithImage]:checked' ) . val ( ) ;
378
+ if ( ! q1 && ! q2 ) enableSubmit = false ;
329
379
}
330
380
var i = 0 ;
331
381
_ . each ( selectedIndicies , function ( value , key , list ) {
@@ -335,6 +385,10 @@ function wildlifespotter(wsParams, imagePrefix, recordValues, placeholders) {
335
385
mu . appendTemplate ( $ctFields , 'input-template' , { id : 'recordValues.' + i + '.comment' , value : value . comment } ) ;
336
386
++ i ;
337
387
} ) ;
388
+
389
+ // console.log("Enable submit button? " + enableSubmit);
390
+ if ( enableSubmit ) $ ( '#btnSave' ) . removeAttr ( 'disabled' ) ;
391
+ else $ ( '#btnSave' ) . attr ( 'disabled' , 'disabled' ) ;
338
392
}
339
393
340
394
function syncRecordValues ( ) {
@@ -371,12 +425,10 @@ function wildlifespotter(wsParams, imagePrefix, recordValues, placeholders) {
371
425
errorList . push ( { element : null , message : "You must either indicate that there are no animals, there's a problem with the image or select at least one animal before you can submit" , type : "Error" } ) ;
372
426
}
373
427
} ) ;
374
- transcribeValidation . setErrorRenderFunctions ( function ( errorList ) {
375
- } ,
376
- function ( ) {
377
- } ) ;
378
428
379
- submitRequiresConfirmation = true ;
429
+ transcribeValidation . setErrorRenderFunctions ( function ( errorList ) { } , function ( ) { } ) ;
430
+ var submitRequiresConfirmation = true ;
431
+
380
432
postValidationFunction = function ( validationResults ) {
381
433
if ( validationResults . errorList . length > 0 ) bootbox . alert ( "<h3>Invalid selection</h3><ul><li>" + _ . pluck ( validationResults . errorList , 'message' ) . join ( '</li><li>' ) + "</li>" ) ;
382
434
} ;
0 commit comments