Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- define([
- 'jquery',
- 'underscore',
- 'backbone',
- 'handlebars',
- 'text!' + spotlet_name + '_templates/matrix/page.html',
- //'text!' + spotlet_name + '_templates/results/freetext.html',
- //spotlet_name + '/views/export',
- 'models/generic_doc',
- 'collections/type_collection',
- 'spotmeutils'
- ], function($, _, Backbone, Handlebars,
- PageTemplate,
- //FreetextTemplate,
- //Exporter,
- GenericDoc,
- Collection,
- spotmeutils
- ){
- //var config = settings.get('surveys').toObject();
- var Page = Backbone.View.extend({
- el: '#page',
- answer_map: {
- 'Care': 'care',
- 'Advanced-Care': 'advanced_care',
- 'Integrated-Care': 'integrated_care'
- },
- template: Handlebars.compile(PageTemplate),
- //freetext_template: Handlebars.compile(FreetextTemplate),
- //exporter: Exporter,
- events: {
- 'click #download-survey': 'download_survey',
- },
- initialize : function (options) {
- _.bindAll(this);
- var me = this;
- console.log('loaded: ',me.options.sid);
- this.resultsVisible = false;
- this.can_refresh = false;
- $(document).on("keydown", this.keydown_event);
- var after_x = _.after(1, this.fetch_survey);
- this.survey = new GenericDoc();
- this.survey.fetch({
- 'fp_type': 'survey',
- 'fp_ext_id': me.options.sid,
- 'success': after_x,
- 'error': this.render_404
- });
- this.eventBus.on('willdisappear', this.save, this);
- this.eventBus.on('willdisappear', this.onClose, this);
- },
- fetch_survey : function (model, object, options, callback) {
- console.log("FETCHING SURVEY...");
- var me = this;
- var _finish_results = function (results) {
- var keys, startkey, endkey;
- if (me.survey.get("reusable")) {
- startkey = [me.survey.get("id"), null]
- endkey = [me.survey.get("id"), {}]
- }
- else {
- keys = results.models.map(function (model) {
- var key = model.get("__key");
- key.push(model.get("max"));
- return key;
- });
- }
- me.results = new Collection([], {
- fp_type: "surveyresponse",
- db: {
- ddoc: "surveylist",
- view: "response_lookup",
- keys: keys,
- startkey: startkey,
- endkey: endkey,
- changes: false
- }
- });
- if (keys && keys.length === 0) {
- console.log("rendering if....");
- me.fetch_pax();
- } else {
- console.log("rendering else....");
- me.results.fetch({
- success: (callback) ? callback : me.fetch_pax
- });
- }
- };
- var after_x = _.after(1, _finish_results);
- this.results = new Collection([], {
- fp_type: "surveyresponse",
- db: {
- ddoc: "surveylist",
- view: "responses",
- startkey: [me.options.sid, null],
- endkey: [me.options.sid, {}],
- changes: false,
- reduce: true,
- group_level: 3,
- include_docs: undefined
- }
- });
- this.results.fetch({
- success: after_x
- });
- },
- fetch_pax: function() {
- var me = this;
- this.pax_map = {};
- this.pax_list = new Collection([], {
- fp_type: 'person'
- });
- var _process_pax_list = function (results) {
- results.each(function(pax) {
- //console.log("pax: " + pax.get("_id") + " - " + pax.get("GM")+" - " + pax.get("country"));
- if(true){
- me.pax_map[pax.get("_id")] = pax.get("patient_support_display");
- }
- });
- me.render();
- }
- var after_x = _.after(1, _process_pax_list);
- this.pax_list.fetch({
- success: after_x
- });
- },
- render_404: function() {
- this.$el.html('Survey not found: ' + this.survey);
- this.eventBus.trigger('kill:spinner');
- return;
- },
- parseSurvey: function() {
- var me = this;
- this.results_map = [];
- this.results.each(function(r) {
- console.log("result: ",r);
- var country = me.pax_map[r.get('fp_owner')];
- _.each(r.get("responses"), function(answer, aID){
- console.log("answer: " + answer + " from "+ country);
- me.results_map.push({country: country, answer: answer, answer_code: me.answer_map[answer]});
- });
- });
- console.log('final map: ', this.results_map);
- },
- render: function () {
- var me = this;
- console.log("pax map",this.pax_map);
- this.parseSurvey();
- this.results_map.sort(function(a, b){
- var nameA=a.country.toLowerCase(), nameB=b.country.toLowerCase()
- if (nameA < nameB) //sort string ascending
- return -1
- if (nameA > nameB)
- return 1
- return 0 //default return value (no sorting)
- })
- var data = {list: this.results_map};
- this.$el.html(
- this.template(data)
- );
- if (!this.resultsVisible){
- $('#content').hide();
- $('#legend').hide();
- }
- this.can_refresh = true;
- this.eventBus.trigger("kill:spinner");
- },
- keydown_event : function (ev) {
- var me = this;
- // space = 32
- if (ev.which === 32) {
- if (!this.resultsVisible) {
- this.resultsVisible = true;
- $('#content').fadeIn(500);
- $('#legend').fadeIn(500);
- return;
- }
- if(this.can_refresh){
- this.results = null;
- this.can_refresh = false;
- console.log('refreshing...');
- var after_x = _.after(1, this.fetch_survey);
- this.survey = new GenericDoc();
- this.survey.fetch({
- 'fp_type': 'survey',
- 'fp_ext_id': me.options.sid,
- 'success': after_x,
- 'error': this.render_404
- });
- }
- else{
- console.log('cant refresh...');
- }
- return;
- }
- },
- onClose : function () {
- clearTimeout(this.refresh_timer)
- }
- });
- return Page;
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement