forked from mbaezpy/design4all
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
130 lines (113 loc) · 3.98 KB
/
app.js
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
/* @author Marcos baez <[email protected]> */
$(document).ready(function(){
// loading papers
/* Paper.loadPapers(function(data){
PaperView.renderSummary({
collection : data,
tmpl : $("#summary-tmpl"),
el : $(".summary")
});
PaperView.renderPapers({
collection : data,
tmpl : $("#paper-tmpl"),
el : $(".papers"),
});
}); */
// loading guidelines with no filters by default
Guide.loadGuidelines(function(data){
GuideView.renderSummary({
collection : data,
tmpl : $("#summary-tmpl"),
el : $(".summary")
});
GuideView.renderGuidelines({
collection : data,
tmpl : $("#guide-tmpl"),
el : $(".guidelines"),
});
});
// any change in the filters
$(".filter-simple select").change(function(e){
var filters = [];
// we build automatically the filters array from existing form-controls
$(".filter-simple .form-control").each(function(i,el){
filters.push({ key : this.id.replace("filter-",""), value : $(this).val()});
});
// and we filter the guidelines
Guide.filter(filters, function(data){
GuideView.renderSummary({
collection : data,
tmpl : $("#summary-tmpl"),
el : $(".summary")
});
GuideView.renderGuidelines({
collection : data,
tmpl : $("#guide-tmpl"),
el : $(".guidelines"),
});
});
});
});
/* Basic Guidelins data manipulation functions */
var Guide = {
_guidelines : [],
/* Load the list of guidelines
* @param callback function fn(collection) that recieve an array of guideline objects
*/
loadGuidelines : function(callback){
$.getJSON("guidelines.php", function(data){
callback(data);
Guide._guidelines = data;
});
},
/* Filters the original guideline list
* @param f an array of {key : <json attr>, value : <attr value>} filters over the json attributes
* @param callback a function fn(collection) that received the filtered array
*/
filter : function(f, callback){
var list = $.grep(this._guidelines, function(el,i){
var r=true;
for(k=0; k<f.length; k++){
r = r && (f[k].value == "" || f[k].value.toLowerCase() == el[f[k].key].toLowerCase());
}
return r;
});
callback(list);
}
};
/* Basic rendering functions */
GuideView = {
/* Renders the search results summary
* @param opt object {collection : <guidelines>, tmpl : <template>, el : <target dom>} elements
*/
renderSummary : function(opt){
var tot = opt.collection.length;
//var cat = $.map(opt.collection, function(n){ return n.design_2});
//var ppr = $.map(opt.collection, function(n){ return n.ref});
var summary = opt.tmpl.text()
.replace(/{npapers}/g, tot);
$(opt.el).empty().append(summary);
},
/* Renders the search results
* @param opt object {collection : <guidelines>, tmpl : <template>, el : <target dom>} elements
*/
renderGuidelines : function(opt){
$(opt.el).empty();
opt.collection.forEach(function(post){
var item = opt.tmpl.text()
.replace(/{flag_evaluation}/g, post.flag_evaluation)
.replace(/{tested_in}/g, post.tested_in)
.replace(/{flag_design}/g, post.flag_design)
.replace(/{designed_in}/g, post.designed_in)
.replace(/{url}/g, post.url)
.replace(/{year}/g, post.year)
.replace(/{tech_name}/g, post.tech_name)
.replace(/{title}/g, post.title)
.replace(/{benefits}/g, post.benefits)
.replace(/{technology_1}/g, post.technology_1)
.replace(/{technology_2}/g, post.technology_2)
.replace(/{scale}/g, post.scale)
$(opt.el).append(item);
});
}
};