1 | define(['dojo/_base/declare','dojo/_base/lang','dojo/_base/array','dojo/_base/event', |
---|
2 | 'dojo/_base/Deferred','dojo/store/JsonRest','dijit/layout/ContentPane', |
---|
3 | 'rft/ui/_Page','rft/ui/QuestionWidget'], |
---|
4 | function(declare,lang,array,event,Deferred,JsonRest,ContentPane,_Page,QuestionWidget) { |
---|
5 | return declare('rft.pages.questions',[_Page],{ |
---|
6 | constructor: function() { |
---|
7 | this.inherited(arguments); |
---|
8 | this.containers = {}; |
---|
9 | this.questions = {}; |
---|
10 | }, |
---|
11 | onVisit: function() { |
---|
12 | this._store = new JsonRest({ |
---|
13 | target:"../server/api.php/data/Question/", |
---|
14 | idProperty: 'uid' |
---|
15 | }); |
---|
16 | this._refresh(); |
---|
17 | }, |
---|
18 | _refresh: function() { |
---|
19 | Deferred.when( this._store.query() ).then(lang.hitch(this,function(results){ |
---|
20 | array.forEach(results,lang.hitch(this,'_addQuestion')); |
---|
21 | })); |
---|
22 | }, |
---|
23 | _getContainerForQuestion: function(q) { |
---|
24 | var category = q.category || 'Unsorted'; |
---|
25 | var categoryContainer = this.containers[category]; |
---|
26 | if ( !categoryContainer ) { |
---|
27 | var ac = new ContentPane({ |
---|
28 | title:category |
---|
29 | }); |
---|
30 | categoryContainer = this.containers[category] = { |
---|
31 | container: ac |
---|
32 | }; |
---|
33 | ac.placeAt(this.accordion); |
---|
34 | ac.startup(); |
---|
35 | } |
---|
36 | return categoryContainer.container; |
---|
37 | }, |
---|
38 | _addQuestion: function(q) { |
---|
39 | var uid = q.uid; |
---|
40 | var question = this.questions[uid]; |
---|
41 | if ( !question ) { |
---|
42 | var qw = new QuestionWidget({ |
---|
43 | question: q |
---|
44 | }); |
---|
45 | qw.startup(); |
---|
46 | question = this.questions[uid] = { |
---|
47 | question: q, |
---|
48 | widget: qw |
---|
49 | } |
---|
50 | } |
---|
51 | var container = this._getContainerForQuestion(q); |
---|
52 | question.widget.placeAt(container.containerNode); |
---|
53 | container.resize(); |
---|
54 | }, |
---|
55 | onNewQuestion: function() { |
---|
56 | Deferred.when( this._store.add({}) ) |
---|
57 | .then(lang.hitch(this,function(question){ |
---|
58 | this.questionForm.reset(); |
---|
59 | this.questionForm.set('value',question); |
---|
60 | this.questionDialog.show(); |
---|
61 | })); |
---|
62 | }, |
---|
63 | onSaveQuestion: function(evt) { |
---|
64 | var value = this.questionForm.get('value'); |
---|
65 | Deferred.when( this._store.put(value) ) |
---|
66 | .then(lang.hitch(this,function(){ |
---|
67 | this.questionDialog.hide(); |
---|
68 | this.questionForm.reset(); |
---|
69 | this._refresh(); |
---|
70 | })); |
---|
71 | event.stop(evt); |
---|
72 | return false; |
---|
73 | }, |
---|
74 | onCancelQuestion: function() { |
---|
75 | this.questionDialog.hide(); |
---|
76 | this.questionForm.reset(); |
---|
77 | this._refresh(); |
---|
78 | } |
---|
79 | }); |
---|
80 | }); |
---|