From cca69aed60d5560a8fb17a88197216c0f629264c Mon Sep 17 00:00:00 2001 From: jelhan Date: Fri, 4 Apr 2014 01:14:08 +0200 Subject: [PATCH] Support for pre defined answer possibilities Implemented answer types are: * yes, no (default) * yes, no, maybe * free text While creating a poll the user choose possible answers by defining an answer type. Later user defined answer possibilites could be added. --- index.html | 45 +++++++++++++++++++--- js/croodle.js | 103 +++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 129 insertions(+), 19 deletions(-) diff --git a/index.html b/index.html index 5b422ae..52e8d34 100644 --- a/index.html +++ b/index.html @@ -39,7 +39,15 @@

{{description}}

type

-

{{type}}

+

{{pollType}}

+ +

answer type

+

{{answerType}}

+ +

answers

+ {{#each answer in answers}} + {{answer}}
+ {{/each}}

options and user

@@ -67,7 +75,18 @@ {{#each newUserSelection in view.newUserSelections}} - + {{/each}} @@ -84,25 +103,30 @@ diff --git a/js/croodle.js b/js/croodle.js index d9624fc..12f1f4f 100644 --- a/js/croodle.js +++ b/js/croodle.js @@ -42,13 +42,12 @@ Ember.computed.encrypted = function(encryptedField) { // check if encryptKey is set if (typeof encryptKey === 'undefined') { - console.log("encryption key is not set for: " + this.toString() + " " + encryptedField); return; } // setter if (arguments.length === 2) { - encryptedValue = Ember.isNone(decryptedValue) ? null : String( sjcl.encrypt( encryptKey , decryptedValue) ); + encryptedValue = Ember.isNone(decryptedValue) ? null : String(sjcl.encrypt(encryptKey , decryptedValue)); this.set(encryptedField, encryptedValue); } @@ -63,20 +62,52 @@ Ember.computed.encrypted = function(encryptedField) { // try to decrypt value try { - decryptedValue = sjcl.decrypt( encryptKey , encryptedValue); + decryptedValue = sjcl.decrypt(encryptKey, encryptedValue); } catch (e) { console.log('Error on decrypting ' + encryptedField); - console.log('Value to decrypt:'); - console.log(encryptedValue); - console.log('Error message by SJCL:'); console.log(e); console.log('Perhaps a wrong encryption key?'); decryptedValue = ''; } - return Ember.isNone(encryptedValue) ? null : String( decryptedValue ); + return Ember.isNone(encryptedValue) ? null : String(decryptedValue); }); }; +/* + * array as attribute for models + */ +App.ArrayTransform = DS.Transform.extend({ + // If the outgoing json is already a valid javascript array + // then pass it through untouched. In all other cases, replace it + // with an empty array. This means null or undefined values + // automatically become empty arrays when serializing this type. + serialize: function (jsonData) { + if (Em.typeOf(jsonData) === 'array') { + return jsonData; + } + else { + return []; + } + }, + + // If the incoming data is a javascript array, pass it through. + // If it is a string, then coerce it into an array by splitting + // it on commas and trimming whitespace on each element. + // Otherwise pass back an empty array. This has the effect of + // turning all other data types (including nulls and undefined + // values) into empty arrays. + deserialize: function (externalData) { + switch (Em.typeOf(externalData)) { + case 'array': + return externalData; + case 'string': + return externalData.split(',').map( function(item) { return jQuery.trim(item) } ); + default: + return []; + } + } +}), + /* * models */ @@ -87,8 +118,11 @@ App.Poll = DS.Model.extend({ title : Ember.computed.encrypted('encryptedTitle'), encryptedDescription : DS.attr('string'), description: Ember.computed.encrypted('encryptedDescription'), - encryptedType : DS.attr('string'), - type : Ember.computed.encrypted('encryptedType'), + encryptedPollType : DS.attr('string'), + pollType : Ember.computed.encrypted('encryptedPollType'), + encryptedAnswerType: DS.attr('string'), + answerType : Ember.computed.encrypted('encryptedAnswerType'), + answers : DS.attr('array'), options : DS.hasMany('option', {async: true}), users : DS.hasMany('user', {async: true}), creationDate : DS.attr('date') @@ -123,7 +157,7 @@ App.Encryption = Ember.Object.extend({ isSet: false }); -App.Types = [ +App.PollTypes = [ Ember.Object.create({ id : "FindADate", label : "Find a date" @@ -134,6 +168,24 @@ App.Types = [ }) ]; +App.AnswerTypes = [ + Ember.Object.create({ + id : "YesNo", + label : "yes, no", + answers : ["yes", "no"] + }), + Ember.Object.create({ + id : "YesNoMaybe", + label : "yes, no, maybe", + answers : ["yes", "no", "maybe"] + }), + Ember.Object.create({ + id : "FreeText", + label : "free text", + answers : [] + }) +]; + /* * Serializer */ @@ -202,9 +254,9 @@ App.CreateMetaRoute = Ember.Route.extend({ return this.modelFor('create'); }, - // redirect to create/index if type is not set + // redirect to create/index if poll type is not set afterModel: function(create){ - if (create.get('type') === null) { + if (create.get('pollType') === null) { this.transitionTo('create.index'); } } @@ -312,7 +364,22 @@ App.CreateSettingsController = Ember.ObjectController.extend({ }); }); } - } + }, + + updateAnswers: function(){ + var selectedAnswer = this.get('model.answerType'), + answers = []; + + if (selectedAnswer !== null) { + for (var i=0; i
{{input value=view.newUserName}}{{input value=newUserSelection.value}} + {{#if isFreeText}} + {{input value=newUserSelection.value}} + {{else}} + {{view Ember.Select + contentBinding="answers" + optionValuePath="content" + optionLabelPath="content" + prompt="Please select an answer" + valueBinding="newUserSelection.value"}} + {{/if}} +