Advertisement
Guest User

Untitled

a guest
Jul 31st, 2015
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.43 KB | None | 0 0
  1. define([
  2. 'jquery',
  3. 'underscore',
  4. 'backbone',
  5. 'handlebars',
  6. 'text!' + spotlet_name + '_templates/matrix/page.html',
  7. //'text!' + spotlet_name + '_templates/results/freetext.html',
  8. //spotlet_name + '/views/export',
  9. 'models/generic_doc',
  10. 'collections/type_collection',
  11. 'spotmeutils'
  12. ], function($, _, Backbone, Handlebars,
  13. PageTemplate,
  14. //FreetextTemplate,
  15. //Exporter,
  16. GenericDoc,
  17. Collection,
  18. spotmeutils
  19. ){
  20.  
  21. //var config = settings.get('surveys').toObject();
  22. var Page = Backbone.View.extend({
  23. el: '#page',
  24. answer_map: {
  25. 'Care': 'care',
  26. 'Advanced-Care': 'advanced_care',
  27. 'Integrated-Care': 'integrated_care'
  28. },
  29.  
  30. template: Handlebars.compile(PageTemplate),
  31. //freetext_template: Handlebars.compile(FreetextTemplate),
  32. //exporter: Exporter,
  33.  
  34. events: {
  35. 'click #download-survey': 'download_survey',
  36. },
  37.  
  38. initialize : function (options) {
  39. _.bindAll(this);
  40. var me = this;
  41. console.log('loaded: ',me.options.sid);
  42.  
  43. this.resultsVisible = false;
  44. this.can_refresh = false;
  45. $(document).on("keydown", this.keydown_event);
  46.  
  47. var after_x = _.after(1, this.fetch_survey);
  48. this.survey = new GenericDoc();
  49.  
  50. this.survey.fetch({
  51. 'fp_type': 'survey',
  52. 'fp_ext_id': me.options.sid,
  53. 'success': after_x,
  54. 'error': this.render_404
  55. });
  56.  
  57. this.eventBus.on('willdisappear', this.save, this);
  58. this.eventBus.on('willdisappear', this.onClose, this);
  59.  
  60. },
  61.  
  62. fetch_survey : function (model, object, options, callback) {
  63. console.log("FETCHING SURVEY...");
  64. var me = this;
  65.  
  66. var _finish_results = function (results) {
  67. var keys, startkey, endkey;
  68. if (me.survey.get("reusable")) {
  69. startkey = [me.survey.get("id"), null]
  70. endkey = [me.survey.get("id"), {}]
  71. }
  72. else {
  73. keys = results.models.map(function (model) {
  74. var key = model.get("__key");
  75. key.push(model.get("max"));
  76. return key;
  77. });
  78. }
  79.  
  80. me.results = new Collection([], {
  81. fp_type: "surveyresponse",
  82. db: {
  83. ddoc: "surveylist",
  84. view: "response_lookup",
  85. keys: keys,
  86. startkey: startkey,
  87. endkey: endkey,
  88. changes: false
  89. }
  90. });
  91.  
  92. if (keys && keys.length === 0) {
  93. console.log("rendering if....");
  94. me.fetch_pax();
  95. } else {
  96. console.log("rendering else....");
  97. me.results.fetch({
  98. success: (callback) ? callback : me.fetch_pax
  99. });
  100. }
  101. };
  102. var after_x = _.after(1, _finish_results);
  103.  
  104. this.results = new Collection([], {
  105. fp_type: "surveyresponse",
  106. db: {
  107. ddoc: "surveylist",
  108. view: "responses",
  109. startkey: [me.options.sid, null],
  110. endkey: [me.options.sid, {}],
  111. changes: false,
  112. reduce: true,
  113. group_level: 3,
  114. include_docs: undefined
  115. }
  116. });
  117.  
  118. this.results.fetch({
  119. success: after_x
  120. });
  121. },
  122.  
  123. fetch_pax: function() {
  124. var me = this;
  125. this.pax_map = {};
  126.  
  127. this.pax_list = new Collection([], {
  128. fp_type: 'person'
  129. });
  130.  
  131. var _process_pax_list = function (results) {
  132. results.each(function(pax) {
  133. //console.log("pax: " + pax.get("_id") + " - " + pax.get("GM")+" - " + pax.get("country"));
  134. if(true){
  135. me.pax_map[pax.get("_id")] = pax.get("patient_support_display");
  136. }
  137. });
  138. me.render();
  139. }
  140.  
  141. var after_x = _.after(1, _process_pax_list);
  142.  
  143. this.pax_list.fetch({
  144. success: after_x
  145. });
  146. },
  147.  
  148. render_404: function() {
  149. this.$el.html('Survey not found: ' + this.survey);
  150. this.eventBus.trigger('kill:spinner');
  151. return;
  152. },
  153.  
  154. parseSurvey: function() {
  155. var me = this;
  156. this.results_map = [];
  157. this.results.each(function(r) {
  158. console.log("result: ",r);
  159. var country = me.pax_map[r.get('fp_owner')];
  160. _.each(r.get("responses"), function(answer, aID){
  161. console.log("answer: " + answer + " from "+ country);
  162. me.results_map.push({country: country, answer: answer, answer_code: me.answer_map[answer]});
  163. });
  164. });
  165. console.log('final map: ', this.results_map);
  166. },
  167.  
  168. render: function () {
  169. var me = this;
  170. console.log("pax map",this.pax_map);
  171. this.parseSurvey();
  172.  
  173. this.results_map.sort(function(a, b){
  174. var nameA=a.country.toLowerCase(), nameB=b.country.toLowerCase()
  175. if (nameA < nameB) //sort string ascending
  176. return -1
  177. if (nameA > nameB)
  178. return 1
  179. return 0 //default return value (no sorting)
  180. })
  181.  
  182.  
  183. var data = {list: this.results_map};
  184.  
  185. this.$el.html(
  186. this.template(data)
  187. );
  188. if (!this.resultsVisible){
  189. $('#content').hide();
  190. $('#legend').hide();
  191. }
  192.  
  193. this.can_refresh = true;
  194. this.eventBus.trigger("kill:spinner");
  195. },
  196.  
  197. keydown_event : function (ev) {
  198. var me = this;
  199. // space = 32
  200. if (ev.which === 32) {
  201. if (!this.resultsVisible) {
  202. this.resultsVisible = true;
  203. $('#content').fadeIn(500);
  204. $('#legend').fadeIn(500);
  205. return;
  206. }
  207.  
  208. if(this.can_refresh){
  209. this.results = null;
  210. this.can_refresh = false;
  211. console.log('refreshing...');
  212.  
  213. var after_x = _.after(1, this.fetch_survey);
  214.  
  215. this.survey = new GenericDoc();
  216.  
  217. this.survey.fetch({
  218. 'fp_type': 'survey',
  219. 'fp_ext_id': me.options.sid,
  220. 'success': after_x,
  221. 'error': this.render_404
  222. });
  223. }
  224. else{
  225. console.log('cant refresh...');
  226. }
  227. return;
  228. }
  229. },
  230.  
  231. onClose : function () {
  232. clearTimeout(this.refresh_timer)
  233. }
  234. });
  235. return Page;
  236. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement