1 | define(['dojo/_base/declare','dojo/_base/lang','dojo/_base/array','dojo/_base/event', |
---|
2 | 'dojo/_base/Deferred','dojo/dom-construct','rft/store','dijit/TitlePane', |
---|
3 | 'rft/ui/_Page','rft/ui/LineWithActionsWidget'], |
---|
4 | function(declare,lang,array,event,Deferred,domConstruct,store,TitlePane,_Page,LineWithActionsWidget) { |
---|
5 | return declare('rft.pages.questions',[_Page],{ |
---|
6 | constructor: function() { |
---|
7 | this.inherited(arguments); |
---|
8 | this.questions = {}; |
---|
9 | }, |
---|
10 | onVisit: function() { |
---|
11 | this._store = store.getStore('Question'); |
---|
12 | this._refresh(true); |
---|
13 | }, |
---|
14 | _refresh: function(initial) { |
---|
15 | Deferred.when( this._store.query() ) |
---|
16 | .then(lang.hitch(this,function(results){ |
---|
17 | array.forEach(results,lang.hitch(this,'_addQuestion')); |
---|
18 | initial && this.accordion.selectChild(true); |
---|
19 | })); |
---|
20 | }, |
---|
21 | _addQuestion: function(q) { |
---|
22 | var uid = q.getUid(); |
---|
23 | var question = this.questions[uid]; |
---|
24 | if ( !question ) { |
---|
25 | var qw = new LineWithActionsWidget({ |
---|
26 | title: q.title, |
---|
27 | userObject: q, |
---|
28 | actions: { |
---|
29 | 'Edit': lang.hitch(this,'_editQuestion') |
---|
30 | } |
---|
31 | }); |
---|
32 | qw.startup(); |
---|
33 | question = this.questions[uid] = { |
---|
34 | question: q, |
---|
35 | widget: qw |
---|
36 | } |
---|
37 | } else { |
---|
38 | // update info |
---|
39 | question.question = q; |
---|
40 | question.widget.title = q.title; |
---|
41 | question.widget.userObject = q; |
---|
42 | question.widget.refresh(); |
---|
43 | } |
---|
44 | var container = this._getContainerForQuestion(q); |
---|
45 | question.widget.placeAt(container.containerNode); |
---|
46 | this._cleanupEmptyContainers(); |
---|
47 | }, |
---|
48 | _getContainerForQuestion: function(q) { |
---|
49 | var category = q.category || 'Unsorted'; |
---|
50 | var widgets = this.accordion.getChildren(); |
---|
51 | var containerWidget = null; |
---|
52 | var placeNode = this.accordion.domNode; |
---|
53 | var placePos = 'last'; |
---|
54 | array.some(widgets,lang.hitch(this,function(widget) { |
---|
55 | if ( widget.title == category ) { |
---|
56 | containerWidget = widget; |
---|
57 | return true; |
---|
58 | } else if ( widget.title > category ) { |
---|
59 | placeNode = widget.domNode; |
---|
60 | placePos = "before"; |
---|
61 | return true; |
---|
62 | } |
---|
63 | return false; |
---|
64 | })); |
---|
65 | if ( !containerWidget ) { |
---|
66 | containerWidget = new TitlePane({ |
---|
67 | title:category |
---|
68 | }); |
---|
69 | containerWidget.startup(); |
---|
70 | domConstruct.place(containerWidget.domNode,placeNode,placePos); |
---|
71 | } |
---|
72 | return containerWidget; |
---|
73 | }, |
---|
74 | _cleanupEmptyContainers: function() { |
---|
75 | var widgets = this.accordion.getChildren(); |
---|
76 | array.forEach(widgets,lang.hitch(this,function(widget){ |
---|
77 | if ( !widget.getChildren().length ) { |
---|
78 | this.accordion.removeChild(widget).destroy(); |
---|
79 | } |
---|
80 | })); |
---|
81 | }, |
---|
82 | onNewQuestion: function() { |
---|
83 | Deferred.when( this._store.add({}) ) |
---|
84 | .then(lang.hitch(this,function(question){ |
---|
85 | this._editQuestion(question); |
---|
86 | })); |
---|
87 | }, |
---|
88 | _editQuestion: function(question) { |
---|
89 | this.questionForm.reset(); |
---|
90 | this.questionWidget.set('value',question); |
---|
91 | this.questionDialog.show(); |
---|
92 | }, |
---|
93 | onSaveQuestion: function(evt) { |
---|
94 | var value = this.questionWidget.get('value'); |
---|
95 | Deferred.when( this._store.put(value) ) |
---|
96 | .then(lang.hitch(this,function(){ |
---|
97 | this.questionDialog.hide(); |
---|
98 | this.questionForm.reset(); |
---|
99 | this._refresh(); |
---|
100 | })); |
---|
101 | event.stop(evt); |
---|
102 | return false; |
---|
103 | }, |
---|
104 | onCancelQuestion: function() { |
---|
105 | this.questionDialog.hide(); |
---|
106 | this.questionForm.reset(); |
---|
107 | } |
---|
108 | }); |
---|
109 | }); |
---|