1 | define(['dojo/_base/declare','dojo/_base/lang','dojo/_base/event', |
---|
2 | 'dojo/_base/Deferred','rft/ui/AccordionList','rft/ui/LineWithActionsWidget', |
---|
3 | 'rft/store','rft/ui/_Page','rft/api'], |
---|
4 | function(declare,lang,event,Deferred,AccordionList,LineWithActionsWidget,store,_Page,api){ |
---|
5 | return declare('rft.pages.survey',[_Page],{ |
---|
6 | object: null, |
---|
7 | onVisit: function() { |
---|
8 | if ( this.pageArgs.uid ) { |
---|
9 | Deferred.when(store.get(this.pageArgs.uid)) |
---|
10 | .then(lang.hitch(this,function(obj){ |
---|
11 | this.object = obj; |
---|
12 | this.setFields(obj); |
---|
13 | return Deferred.when( obj.creator && store.dereference(obj.creator) ); |
---|
14 | })) |
---|
15 | .then(lang.hitch(this,function(obj){ |
---|
16 | this.creator.innerHTML = (obj && obj.email) || 'unknown'; |
---|
17 | })); |
---|
18 | Deferred.when(store.query('_design/default/_view/by_type',{key:'Question'})) |
---|
19 | .then(lang.hitch(this,function(items){ |
---|
20 | this._questionList = new AccordionList({ |
---|
21 | actions: { |
---|
22 | 'Add': lang.hitch(this,'_addQuestion') |
---|
23 | }, |
---|
24 | categoryProperty: 'category', |
---|
25 | titleProperty: 'title' |
---|
26 | },this.allQuestions); |
---|
27 | this._questionList.startup(); |
---|
28 | this._questionList.setItems(items); |
---|
29 | })); |
---|
30 | } else { |
---|
31 | this.header.innerHTML = "Error: no uid for survey!" |
---|
32 | } |
---|
33 | }, |
---|
34 | onReset: function() { |
---|
35 | this.setFields(this.object); |
---|
36 | }, |
---|
37 | onSave: function(evt) { |
---|
38 | lang.mixin(this.object,this.form.get('value')); |
---|
39 | Deferred.when( store.put(this.object) ) |
---|
40 | .then(lang.hitch(this,function(obj){ |
---|
41 | this.object = obj; |
---|
42 | this.setFields(obj); |
---|
43 | api.notify("Object saved"); |
---|
44 | }),lang.hitch(this,function(){ |
---|
45 | api.notify("Object save failed",'error'); |
---|
46 | })); |
---|
47 | event.stop(evt); |
---|
48 | evt.stopPropagation(); |
---|
49 | return false; |
---|
50 | }, |
---|
51 | setFields: function(obj) { |
---|
52 | this.form.reset(); |
---|
53 | this.header.innerHTML = "Edit survey '"+(obj.title || '(undefined)')+"'"; |
---|
54 | obj && this.form.set('value',obj); |
---|
55 | }, |
---|
56 | _addQuestion: function(obj) { |
---|
57 | var d = {}; |
---|
58 | d.widget = new LineWithActionsWidget({ |
---|
59 | title:obj.title, |
---|
60 | userObj: obj, |
---|
61 | actions:{ |
---|
62 | "Remove": lang.hitch(this,'_removeQuestion',d) |
---|
63 | } |
---|
64 | }); |
---|
65 | d.widget.placeAt(this.surveyQuestions); |
---|
66 | }, |
---|
67 | _removeQuestion: function(data,obj) { |
---|
68 | data.widget.destroy(); |
---|
69 | } |
---|
70 | }); |
---|
71 | }); |
---|
72 | |
---|