/
demo.js
294 lines (259 loc) · 9.11 KB
/
demo.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
requirejs.config({
shim:{
bootstrap:['jquery'],
hashchange:['jquery']
},
paths:{
jquery:'jquery-1.7.2.min',
underscore:'lodash.min',
knockout:'knockout-2.1.0',
pager:'pager.min',
bootstrap:'bootstrap/js/bootstrap.min',
hashchange:'jquery.ba-hashchange.min',
"zoidberg":'character/zoidberg'
}
});
requirejs(['jquery', 'knockout', 'underscore', 'pager', 'bootstrap', 'hashchange'], function ($, ko, _, pager) {
pager.PageAccordionItem = function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
pager.Page.apply(this, arguments);
};
pager.PageAccordionItem.prototype = new pager.Page();
// get second child
pager.PageAccordionItem.prototype.getAccordionBody = function () {
return $(this.element).children()[1];
};
// hide second child
pager.PageAccordionItem.prototype.hideElement = function (callback) {
// use hide if it is the first time the page is hidden
if (!this.pageAccordionItemHidden) {
this.pageAccordionItemHidden = true;
$(this.getAccordionBody()).hide();
if (callback) {
callback();
}
} else { // else use a slideUp animation
$(this.getAccordionBody()).slideUp();
if (callback) {
callback();
}
}
};
// show the second child using a slideDown animation
pager.PageAccordionItem.prototype.showElement = function (callback) {
$(this.getAccordionBody()).slideDown();
if (callback) {
callback();
}
};
ko.bindingHandlers['page-accordion-item'] = {
init:function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
var pageAccordionItem = new pager.PageAccordionItem(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext);
pageAccordionItem.init();
},
update:function () {
}
};
ko.bindingHandlers['prettyprint'] = {
init:function(element) {
var $element = $(element);
$element.html(prettyPrintOne($element.html(), undefined, true));
}
};
ko.bindingHandlers['tooltip'] = {
init:function(element) {
$(element).tooltip();
}
};
var viewModel = {
question: ko.observable('How many roads must a man walk down before you can call him a man?'),
closePage: function(page) {
return function() {
page.hideElementWrapper();
return true;
};
},
theID: ko.observable(''),
name:ko.observable("Pelle"),
description:ko.observable('pl'),
externalContentLoaded:function (page) {
},
externalContentLazyLoaded: function(page) {
},
afterFryIsDisplayed:function () {
$('body').css("background-color", "#FF9999");
},
beforeFryIsHidden:function () {
$('body').stop().css("background-color", "#FFFFFF");
},
showFry:function (page, callback) {
$(page.element).fadeIn(1000, callback);
},
hideFry:function (page, callback) {
$(page.element).fadeOut(1000, function () {
$(page.element).hide();
if (callback) {
callback();
}
});
},
textLoader:function (page, element) {
var loader = {};
var txt = $('<h3></h3>', {text:'Loading ' + page.getValue().title});
loader.load = function () {
$(element).prepend(txt);
};
loader.unload = function () {
txt.remove();
};
return loader;
},
spinnerLoader: function(page, element) {
var loader = {};
var txt = $('<img src="ajax-loader.gif"/>');
loader.load = function () {
$(element).empty();
$(element).append(txt);
};
loader.unload = function () {
txt.remove();
};
return loader;
},
eternalSource: function() {
// do nothing :)
},
randomFailed:function (page, route) {
viewModel.newChildren.push({
childId:route[0]
});
page.showPage(route);
},
loggedIn:ko.observable(false),
isLoggedIn:function (page, route, callback) {
if (viewModel.loggedIn()) {
callback();
} else {
window.location.href = "#!/navigation/guards/login";
}
},
logout:function () {
viewModel.loggedIn(false);
return true;
},
newChildren:ko.observableArray([])
};
window.delaySource = function(url) {
var iframe = $('<iframe width="300" height="380" frameborder="0" allowtransparency="true"></iframe>');
return function(page, callback) {
setTimeout(function() {
$(page.element).append(iframe);
iframe.attr('src', url);
callback();
}, 2000);
};
};
window.requireVM = function (module) {
return function (callback) {
require([module], function (mod) {
callback(mod.getVM());
});
};
};
window.requireView = function (viewModule) {
return function (page, callback) {
require([viewModule], function (viewString) {
$(page.element).html(viewString);
callback();
});
};
};
window.recapLoaded = function (page) {
$('.tt').tooltip();
// for each h3
var h3s = $('h3', $(page.element));
$.each(h3s, function (h3Index, h3) {
var $h3 = $(h3);
// get all siblings until another h3 or h2
var siblings = $([]);
var sibling = $h3.next();
while (sibling && sibling.length > 0 && sibling[0].tagName.toLocaleLowerCase() !== 'h3' && sibling[0].tagName.toLocaleLowerCase() !== 'h2') {
siblings.push(sibling);
sibling = sibling.next();
}
// hide all siblings
siblings.toggle();
// on h3-click toggle all siblings
$h3.click(function () {
siblings.toggle();
});
});
};
pager.onBindingError.add(function(event) {
var page = event.page;
$(page.element).empty().append('<div class="alert"> Error Loading Page</div>');
});
pager.onSourceError.add(function(event) {
var page = event.page;
var url = event.url;
$(page.element).empty().append($('<div></div>', {text: 'Error loading page ' + url, "class" : 'alert'}));
});
var indexOfCurrentPage = function(page) {
return page.parentPage.children.indexOf(page);
};
var pageStep = function(page, step) {
return ko.computed(function() {
var rawStep = ko.utils.unwrapObservable(step);
return page.parentPage.children()[(Math.abs(indexOfCurrentPage(page))) + rawStep] || page.nullObject;
});
};
var NextPage = function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
this.element = element;
this.bindingContext = bindingContext;
this.path = ko.observable();
var parent = pager.getParentPage(bindingContext);
this.siblingPage = pageStep(parent, valueAccessor());
this.path = ko.computed(function () {
var value = this.siblingPage();
return value.getFullRoute()().join('/');
}, this);
};
NextPage.prototype.bind = function () {
var text = $(this.element).text();
ko.computed(function () {
var page = this.siblingPage();
if (text.length === 0) {
var newText = page.val('title');
ko.applyBindingsToNode(this.element, {
text:newText
});
} else if (text.indexOf('{0}')) {
var replacedText = text.replace('{0}', page.val('title'));
ko.applyBindingsToNode(this.element, {
text:replacedText
});
}
}, this);
var hash = ko.computed(function () {
return pager.Href.hash + this.path();
}, this);
ko.applyBindingsToNode(this.element, {
attr:{
'href':hash
}
});
};
ko.bindingHandlers['page-step'] = {
init:function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
var nextPage = new NextPage(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext);
nextPage.bind();
}
};
$(function () {
pager.Href.hash = '#!/';
pager.extendWithPage(viewModel);
window.VM = viewModel;
ko.applyBindings(viewModel);
pager.startHashChange();
$('.dropdown-toggle').dropdown();
});
});