|
| 1 | +export default Ember.ObjectController.extend(Ember.Validations.Mixin, { |
| 2 | + encryptionKey: '', |
| 3 | + newUserName: '', |
| 4 | + queryParams: ['encryptionKey'], |
| 5 | + |
| 6 | + actions: { |
| 7 | + addNewUser: function(){ |
| 8 | + var newUser = { |
| 9 | + name: this.get('newUserName'), |
| 10 | + selections: this.get('newUserSelections') |
| 11 | + }; |
| 12 | + |
| 13 | + // send new user to controller for saving |
| 14 | + this.send('saveNewUser', newUser); |
| 15 | + |
| 16 | + // clear input fields |
| 17 | + this.set('newUserName', ''); |
| 18 | + this.get('newUserSelections').forEach(function(selection){ |
| 19 | + selection.set('value', ''); |
| 20 | + }); |
| 21 | + |
| 22 | + // reset validation erros |
| 23 | + this.set('errors.newUserName', ''); |
| 24 | + this.set('errors.everyOptionIsAnswered', ''); |
| 25 | + |
| 26 | + // recalculate fixedHeaders |
| 27 | + $('.user-selections-table').floatThead('reflow'); |
| 28 | + }, |
| 29 | + |
| 30 | + /* |
| 31 | + * save a new user |
| 32 | + */ |
| 33 | + saveNewUser: function(user){ |
| 34 | + var self = this; |
| 35 | + |
| 36 | + // create new user record in store |
| 37 | + var newUser = this.store.createRecord('user', { |
| 38 | + name: user.name, |
| 39 | + creationDate: new Date(), |
| 40 | + poll: this.get('model'), |
| 41 | + selections: user.selections |
| 42 | + }); |
| 43 | + |
| 44 | + // save new user |
| 45 | + newUser.save().then(function(){ |
| 46 | + // assign new user to poll |
| 47 | + self.get('model.users').pushObject(newUser); |
| 48 | + }); |
| 49 | + }, |
| 50 | + |
| 51 | + submitNewUser: function() { |
| 52 | + this.validate(); |
| 53 | + |
| 54 | + $.each(Ember.View.views, function(id, view) { |
| 55 | + if(view.isEasyForm) { |
| 56 | + view.focusOut(); |
| 57 | + } |
| 58 | + }); |
| 59 | + |
| 60 | + if (this.get('isValid')) { |
| 61 | + // tricker save action |
| 62 | + this.send('addNewUser'); |
| 63 | + } |
| 64 | + } |
| 65 | + }, |
| 66 | + |
| 67 | + dateGroups: function() { |
| 68 | + // group dates only for find a date with times |
| 69 | + if ( this.get('isFindADate') !== true || |
| 70 | + this.get('isDateTime') !== true ) { |
| 71 | + return []; |
| 72 | + } |
| 73 | + |
| 74 | + var datetimes = this.get('dates'), |
| 75 | + dates = [], |
| 76 | + datesCount = {}, |
| 77 | + dateGroups = []; |
| 78 | + |
| 79 | + var lastDate = null, |
| 80 | + count = 0; |
| 81 | + datetimes.forEach(function(el){ |
| 82 | + var date; |
| 83 | + date = new Date( el.title ); |
| 84 | + date.setHours(0); |
| 85 | + date.setMinutes(0); |
| 86 | + date.setSeconds(0); |
| 87 | + |
| 88 | + if (lastDate === null) { |
| 89 | + lastDate = date; |
| 90 | + } |
| 91 | + |
| 92 | + if (date.getTime() === lastDate.getTime()) { |
| 93 | + count++; |
| 94 | + } |
| 95 | + else { |
| 96 | + // push last values; |
| 97 | + dateGroups.pushObject({ |
| 98 | + "value": lastDate, |
| 99 | + "colspan": count |
| 100 | + }); |
| 101 | + |
| 102 | + // set lastDate to current date and reset count |
| 103 | + lastDate = date; |
| 104 | + count = 1; |
| 105 | + } |
| 106 | + }); |
| 107 | + dateGroups.pushObject({ |
| 108 | + "value": lastDate, |
| 109 | + "colspan": count |
| 110 | + }); |
| 111 | + |
| 112 | + return dateGroups; |
| 113 | + }.property('dates.@each'), |
| 114 | + |
| 115 | + /* |
| 116 | + * handles options if they are dates |
| 117 | + */ |
| 118 | + dates: function() { |
| 119 | + // if poll type is find a date |
| 120 | + // we return an empty array |
| 121 | + if( !this.get('isFindADate') ) { |
| 122 | + return []; |
| 123 | + } |
| 124 | + |
| 125 | + // if current timezone doesn't differ to timezone poll got created with or |
| 126 | + // if local timezone should be used |
| 127 | + // we return original options array |
| 128 | + if ( |
| 129 | + !this.get('timezoneDiffers') || |
| 130 | + this.get('useLocalTimezone') |
| 131 | + ) { |
| 132 | + return Ember.copy( this.get('options') ); |
| 133 | + } |
| 134 | + else { |
| 135 | + var timezoneDifference = new Date().getTimezoneOffset() - this.get('timezoneOffset'), |
| 136 | + dates = [], |
| 137 | + self = this; |
| 138 | + this.get('options').forEach(function(option){ |
| 139 | + dates.pushObject({ |
| 140 | + title: new Date( option.title ).setMinutes( |
| 141 | + new Date( option.title ).getMinutes() - self.get('timezoneOffset') |
| 142 | + ) |
| 143 | + }); |
| 144 | + }); |
| 145 | + return dates; |
| 146 | + } |
| 147 | + }.property('options.@each', 'useLocalTimezone'), |
| 148 | + |
| 149 | + /* |
| 150 | + * evaluates poll data |
| 151 | + * if free text answers are allowed evaluation is disabled |
| 152 | + */ |
| 153 | + evaluation: function() { |
| 154 | + // disable evaluation if answer type is free text |
| 155 | + if (this.get('answerType') === 'FreeText') { |
| 156 | + return []; |
| 157 | + } |
| 158 | + |
| 159 | + var evaluation = [], |
| 160 | + options = [], |
| 161 | + lookup = []; |
| 162 | + |
| 163 | + // init options array |
| 164 | + this.get('options').forEach(function(option, index){ |
| 165 | + options[index] = 0; |
| 166 | + }); |
| 167 | + |
| 168 | + // init array of evalutation objects |
| 169 | + // create object for every possible answer |
| 170 | + this.get('answers').forEach(function(answer){ |
| 171 | + evaluation.push({ |
| 172 | + id: answer.label, |
| 173 | + label: answer.label, |
| 174 | + options: $.extend([], options) |
| 175 | + }); |
| 176 | + }); |
| 177 | + // create object for no answer if answers are not forced |
| 178 | + if (!this.get('forceAnswer')){ |
| 179 | + evaluation.push({ |
| 180 | + id: null, |
| 181 | + label: 'no answer', |
| 182 | + options: $.extend([], options) |
| 183 | + }); |
| 184 | + } |
| 185 | + |
| 186 | + // create lookup array |
| 187 | + evaluation.forEach(function(value, index){ |
| 188 | + lookup[value.id] = index; |
| 189 | + }); |
| 190 | + |
| 191 | + // loop over all users |
| 192 | + this.get('users').forEach(function(user){ |
| 193 | + // loop over all selections of the user |
| 194 | + user.get('selections').forEach(function(selection, optionindex){ |
| 195 | + var answerindex; |
| 196 | + |
| 197 | + // get answer index by lookup array |
| 198 | + if (typeof lookup[selection.value.label] === 'undefined') { |
| 199 | + answerindex = lookup[null]; |
| 200 | + } |
| 201 | + else { |
| 202 | + answerindex = lookup[selection.value.label]; |
| 203 | + } |
| 204 | + |
| 205 | + // increment counter |
| 206 | + try { |
| 207 | + evaluation[answerindex]['options'][optionindex] = evaluation[answerindex]['options'][optionindex] + 1; |
| 208 | + } catch (e) { |
| 209 | + // ToDo: Throw an error |
| 210 | + } |
| 211 | + }); |
| 212 | + }); |
| 213 | + |
| 214 | + return evaluation; |
| 215 | + }.property('users.@each'), |
| 216 | + |
| 217 | + /* |
| 218 | + * returns true if user has selected an answer for every option provided |
| 219 | + */ |
| 220 | + everyOptionIsAnswered: function(){ |
| 221 | + try { |
| 222 | + var newUserSelections = this.get('newUserSelections'), |
| 223 | + allAnswered = true; |
| 224 | + |
| 225 | + if (typeof newUserSelections === 'undefined') { |
| 226 | + return false; |
| 227 | + } |
| 228 | + |
| 229 | + newUserSelections.forEach(function(item, index, enumerable){ |
| 230 | + if (Ember.isEmpty(item.value)) { |
| 231 | + allAnswered = false; |
| 232 | + } |
| 233 | + }); |
| 234 | + |
| 235 | + return allAnswered; |
| 236 | + } |
| 237 | + catch (e) { |
| 238 | + return false; |
| 239 | + } |
| 240 | + }.property('newUserSelections.@each.value'), |
| 241 | + |
| 242 | + /* |
| 243 | + * calculate colspan for a row which should use all columns in table |
| 244 | + * used by evaluation row |
| 245 | + */ |
| 246 | + fullRowColspan: function(){ |
| 247 | + var colspan = this.get('options.length') + 2; |
| 248 | + return colspan; |
| 249 | + }.property('options.@each'), |
| 250 | + |
| 251 | + /* |
| 252 | + * switch isValid state |
| 253 | + * is needed for disable submit button |
| 254 | + */ |
| 255 | + isNotValid: function(){ |
| 256 | + return !this.get('isValid'); |
| 257 | + }.property('isValid'), |
| 258 | + |
| 259 | + // array to store selections of new user |
| 260 | + newUserSelections: function(){ |
| 261 | + var newUserSelections = Ember.A(), |
| 262 | + options = this.get('options'); |
| 263 | + |
| 264 | + options.forEach(function(){ |
| 265 | + var newSelection = Ember.Object.create({value: ''}); |
| 266 | + newUserSelections.pushObject(newSelection); |
| 267 | + }); |
| 268 | + |
| 269 | + return newUserSelections; |
| 270 | + }.property('options'), |
| 271 | + |
| 272 | + optionCount: function() { |
| 273 | + return this.get('options.length'); |
| 274 | + }.property('options'), |
| 275 | + |
| 276 | + pollUrl: function() { |
| 277 | + return window.location.href; |
| 278 | + }.property('currentPath', 'encryptionKey'), |
| 279 | + |
| 280 | + /* |
| 281 | + * return true if current timezone differs from timezone poll got created with |
| 282 | + */ |
| 283 | + timezoneDiffers: function() { |
| 284 | + return new Date().getTimezoneOffset() !== this.get('timezoneOffset'); |
| 285 | + }.property('timezoneOffset'), |
| 286 | + |
| 287 | + updateEncryptionKey: function() { |
| 288 | + // update encryption key |
| 289 | + this.set('encryption.key', this.get('encryptionKey')); |
| 290 | + |
| 291 | + // reload content to recalculate computed properties |
| 292 | + // if encryption key was set before |
| 293 | + if (this.get('encryption.isSet') === true) { |
| 294 | + this.get('content').reload(); |
| 295 | + } |
| 296 | + |
| 297 | + this.set('encryption.isSet', true); |
| 298 | + }.observes('encryptionKey'), |
| 299 | + |
| 300 | + useLocalTimezone: function() { |
| 301 | + return false; |
| 302 | + }.property(), |
| 303 | + |
| 304 | + validations: { |
| 305 | + everyOptionIsAnswered: { |
| 306 | + /* |
| 307 | + * validate if every option is answered |
| 308 | + * if it's forced by poll settings (forceAnswer === true) |
| 309 | + * |
| 310 | + * using a computed property therefore which returns true / false |
| 311 | + * in combinatoin with acceptance validator |
| 312 | + * |
| 313 | + * ToDo: Show validation errors |
| 314 | + */ |
| 315 | + acceptance: { |
| 316 | + if: function(object, validator){ |
| 317 | + return object.get('forceAnswer'); |
| 318 | + }, |
| 319 | + message: Ember.I18n.t('poll.error.newUser.everyOptionIsAnswered') |
| 320 | + } |
| 321 | + }, |
| 322 | + |
| 323 | + newUserName: { |
| 324 | + presence: { |
| 325 | + /* |
| 326 | + * validate if a user name is given |
| 327 | + * if it's forced by poll settings (anonymousUser === false) |
| 328 | + */ |
| 329 | + unless: function(object, validator){ |
| 330 | + /* have in mind that anonymousUser is undefined on init */ |
| 331 | + return object.get('anonymousUser'); |
| 332 | + } |
| 333 | + } |
| 334 | + } |
| 335 | + }, |
| 336 | + |
| 337 | + /* |
| 338 | + * have to manually rerun validation when encryption key is present in model |
| 339 | + * otherwise ember-validation is not using correct values for properties in |
| 340 | + * conditional validators |
| 341 | + */ |
| 342 | + validationsFixBug: function() { |
| 343 | + if(!Ember.isEmpty(this.get('model.encryption.key'))) { |
| 344 | + this.validate(); |
| 345 | + } |
| 346 | + }.observes('model.encryption.key') |
| 347 | +}); |
0 commit comments