| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209 |
1
1
1
7
7
1
1
7
7
6
6
7
1
1
8
8
2
2
2
6
6
6
8
1
1
1
1
2
2
2
2
2
2
2
| /*global require, define, console, $ */
/*jslint nomen: true, debug: true */
/**
* @module populationView
* @extends module:defaultView
*/
define([
"framework",
"views/defaultView",
"text!templates/populationTpl.hbs",
"models/populationModel",
"underscore",
"utils",
"views/population/populationAvgChartView",
"views/population/populationYearChartView"
], function ($, defaultView, tpl, Model, _, utils, avgChartView, yearChartView) {
"use strict";
/**
* @name module:populationView
* @description Population informer
* @class Backbone.View
*
* @requires module:defaultView
* @requires module:populationModel
* @requires text!templates/populationTpl {String} Population hbs template
* @requires underscore
* @requires module:utils
* @requires module:populationAvgChartView
* @requires module:populationYearChartView
*
* @see module:defaultView
* @see module:utils
* @see module:populationModel
* @see module:populationYearChartView
* @see module:populationYearChartView
*
* @constructor
* @returns {Function} Backbone.View constructor
*/
var MENU_CLASS = ".enw__menu";
return defaultView.extend({
/**
* @name module:populationView#template
* @description Raw handlebars template
* @type {String}
*/
template: tpl,
/**
* @name module:populationView#name
* @description Template id
* @type {String}
*/
name: "populationTpl",
/**
* @name module:populationView#title
* @description Default widget title
* @type {String}
*/
title: "populationTitle",
/**
* @name module:populationView#ModelConstructor
* @see module:defaultView#ModelConstructor
* @see module:populationModel
* @description Population model class
*/
ModelConstructor: Model,
/**
* @name module:populationView#mode
* @description Current widget mode
* @type {object}
*/
mode: {
year: "avg"
},
/**
* @name module:populationView#renderData
* @description Chart update & widget menu render
* @function
*/
renderData: function () {
var collection = this.model.get("collection"),
relatedView = this.getRelatedView();
/* istanbul ignore else */
if (this.renderMenu) {
// Getting unique years from model
this.renderMenu(
["avg"].concat(_.uniq(_.pluck(collection, 'year'))),
MENU_CLASS
);
// There's no need to render another one;
this.renderMenu = undefined;
}
//this.switchModeDelayed();
this.showMsg();
// Foolproof
/* istanbul ignore else */
if (relatedView) {
relatedView.render(collection || this.model.get("population"));
this.switchRelatedView(relatedView.$el);
}
return this.model.attributes;
},
/**
* @name module:populationView#afterRender
* @description Menu event bindings
* @see module:defaultView#afterRender
* @function
*/
afterRender: function () {
var MENU_LINK_SELECTOR = ".enw__menu__link";
this.$el.on(
"click",
MENU_LINK_SELECTOR,
$.proxy(this.onMenuClick, this)
);
},
/**
* @name module:populationView#getRelatedView
* @description Gets related view by widget mode
* @function
*/
getRelatedView: function () {
var mode = this.mode,
SELECTOR,
hash,
// View constructor
View;
if (mode.year === "avg") {
hash = "avgChart";
SELECTOR = ".avgChart";
View = avgChartView;
} else {
hash = "yearChart";
SELECTOR = ".yearChart";
View = yearChartView;
}
// Delayed chart initialization
return this.initRelatedView(hash, View, SELECTOR);
},
/**
* @name module:populationView#onMenuClick
* @description Primary and secondary menu click handler.
* Switches widget mode by click
* @function
*/
onMenuClick: function (e) {
var $target = $(e.target),
mode = $target.data("href");
// "Throttling": suspends handler while previous data request is being proceed
/* istanbul ignore else */
Eif (this.status === 2) {
// Part of switchMode logic
// this.selectMenuItem($target);
this.switchMode(mode);
}
return mode;
},
/**
* @name module:populationView#switchMode
* @description Updates model state by selected mode
* @function
* @param [year] {String|Number} Year in xxxx format
*/
switchMode: function (year) {
var mode = this.setMode(year),
model = this.model,
url = model.host + "/" + model.path;
/* istanbul ignore else */
Eif (mode) {
// Menu class switch
this.selectMenuItem(mode.year, this.$el.find(MENU_CLASS));
// NB: As far as we have no pre-initialized related views
// DOM state may be switched in renderData callback only
// So we just form the request and wait. DDP as is.
/* istanbul ignore else */
Eif (mode.year !== "avg") {
url += "/" + mode.year;
}
this.fetch(url);
}
return mode;
}
});
}); |