forked from mozilla/mozilla-club-activity-story-of-us
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
executable file
·235 lines (171 loc) · 6.34 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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
(function() {
// Used to hold information from aside.md and activity.md
var data = {
config: {},
activity: {},
currentLocale: "en"
};
// Returns the path for a given file with locale.
function getPathForLocale(inPath, inLocale) {
var locale = inLocale || data.currentLocale;
return "activity-data/content/" + locale + "/" + inPath;
}
function isLocaleAvailable(inLocale) {
console.log("checking if locale: " + inLocale + " is available");
if (!data.locales) {
console.log("no locales found");
return false;
}
for(var i = 0, len = data.locales.length; i < len; i++) {
if (data.locales[i].code == inLocale) {
console.log("locale found!");
return true;
}
}
return false;
}
function switchLocale(inLocale) {
if (isLocaleAvailable(inLocale)) {
console.log("switching current locale to", inLocale);
data.currentLocale = inLocale;
bindActivityData();
return true;
} else {
return false;
}
}
function isDefaultBrowserLanguageLocaleAvailable() {
var locale = window.navigator.language;
console.log("isDefaultBrowserLanguageLocaleAvailable:", locale);
// check for full locale available.
if (isLocaleAvailable(locale)) {
return locale;
}
// check if language locale is available
if (isLocaleAvailable(locale.substring(0,2))) {
return locale.substring(0,2);
}
return false;
}
function isLocaleSetOnAddress() {
var locale = window.location.hash.substring(1);
console.log("isLocaleSetOnAddress:", locale);
// check for full locale available.
if (isLocaleAvailable(locale)) {
return locale;
}
// check if language locale is available
if (isLocaleAvailable(locale.substring(0,2))) {
return locale.substring(0,2);
}
return false;
}
function setDefaultLocale() {
data.currentLocale = isLocaleSetOnAddress() || isDefaultBrowserLanguageLocaleAvailable() || "en";
console.log("locale set to:", data.currentLocale);
}
// Builds the top menu
function bindNavigationData() {
var source = $("#navigation-template").html();
var template = Handlebars.compile(source);
console.log("Binding navigation data...");
$("#navigation").html(template(data));
$("#locales-switch").change(function() {
var newLocale = $(this).val();
var text = $(this).text();
console.log("Switching locale callback");
switchLocale(newLocale);
return true;
});
}
// Builds the left sidebar
function bindAsideData() {
var source = $("#aside-template").html();
var template = Handlebars.compile(source);
console.log("Loading aside.md markdown file...");
function successfulyLoadedMarkdownFile(markdownData) {
var asideData = metaMarked(markdownData);
data.activity.aside = asideData.html;
console.log("Binding aside data...");
$(".aside").html(template(data));
}
$.ajax({
url: getPathForLocale("aside.md", null),
type: 'get',
dataType: 'html',
success: successfulyLoadedMarkdownFile
});
}
function bindLocaleData() {
var source = $("#locales-bar-template").html();
var template = Handlebars.compile(source);
console.log("Loading locales.json file...");
function successfulyLoadedLocalesFile(locales) {
data.locales = locales;
setDefaultLocale();
bindActivityData();
}
$.ajax({
url: "activity-data/locales.json",
type: 'get',
dataType: 'json',
success: successfulyLoadedLocalesFile
});
}
// Builds the main content area
function bindActivityData() {
var source = $("#activity-template").html();
var template = Handlebars.compile(source);
console.log("Loading activity.md markdown file...");
function successfulyLoadedMarkdownFile(markdownData) {
var activityData = metaMarked(markdownData);
data.activity.activity = activityData.html;
data.activity.meta = activityData.meta;
console.log("Binding activity data...");
console.log("meta", activityData.meta);
document.title = "Activity: " + activityData.meta.title;
$(".main").html(template(data));
// Fix some CSS stuff because markdown can't handle paragraph classes
$(".activity-instructions > p:nth-child(4)").addClass("lead");
$(".activity-instructions > p:nth-child(5)").addClass("leadTime");
$(".activity-instructions > hr + p").addClass("time");
// All other data is only inserted after the succesful insertion of the main content area
bindNavigationData();
bindAsideData();
bindFooterData();
}
$.ajax({
url: getPathForLocale("activity.md", null),
type: 'get',
dataType: 'html',
success: successfulyLoadedMarkdownFile
});
}
// Build the footer data
function bindFooterData() {
var source = $("#footer-template").html();
var template = Handlebars.compile(source);
console.log("Binding footer data...");
$(".footer").html(template(data));
}
// Handlebar helpers
Handlebars.registerHelper('competencyButton', function (competency) {
var source = $("#competency-button-template").html();
var template = Handlebars.compile(source);
var data = {competency: competency};
console.log("competency: ", competency);
return template(data);
});
Handlebars.registerHelper('toLowerCase', function (str) {
return str.toLowerCase();
});
Handlebars.registerHelper('selectIfCurrentLocale', function(str) {
if (str == data.currentLocale) {
return "selected";
} else {
return "";
}
});
// Start the app.
bindLocaleData();
}());