-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpanels.js
138 lines (116 loc) · 4.33 KB
/
panels.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
var stylesheetIdPrefix = '__$_panel_css_';
function Panel(panelContainer, title) {
this.title = ko.observable(title);
this.body = $('<div class="panels panel panelBody"></div>');
this.container = $('<div class="panels panel"><h2 data-bind="text: title()"></h2></div>');
this.container.append(this.body);
ko.applyBindings(this, this.container[0]);
panelContainer.append(this.container);
}
var containerDiv = $('<div class="panels container"></div>');
var headerDiv = $('<div class="panels top"></div>');
var leftDiv = $('<div class="panels left"></div>');
var rightDiv = $('<div class="panels right"></div>');
var panelsDiv = $('<div class="panels body"></div>');
var footerDiv = $('<div class="panels bottom"></div>');
function skin(skinName) {
return ObjectMemory.checkout.readFile("skin:" + skinName).instance.bodyText;
}
function boundSkin(skinName, viewModel) {
var node = $(skin(skinName));
var count = node.size();
for (var i = 0; i < count; i++) {
ko.applyBindings(viewModel, node[i]);
}
return node;
}
// Based on https://gist.github.com/973973 "Zepto + Mustache + KnockoutJS starting point"
// Uses JQuery and our repository instead of Zepto and the document's own DOM.
ko.mustacheTemplateEngine = function () {
var $elf = this;
$elf.cache = {};
ObjectMemory.checkout.changeListeners.name.push(function (event) {
if (event.name.substring(0, 5) === "skin:") {
delete $elf.cache[event.name.substring(5)];
}
});
$elf['getTemplateNode'] = function (template) {
if (!(template in $elf.cache)) {
$elf.cache[template] = { text: skin(template), isRewritten: false };
}
return $elf.cache[template];
}
$elf['renderTemplate'] = function (templateId, data, options) {
options = options || {};
var templateOptions = options['templateOptions'] || {};
var context = $.extend({$data: data}, data, templateOptions);
var template = $elf['getTemplateNode'](templateId).text,
html = Mustache.to_html(template, context),
resultNodes = $(html);
return resultNodes;
};
$elf['isTemplateRewritten'] = function (templateId) {
return $elf['getTemplateNode'](templateId).isRewritten === true;
};
$elf['rewriteTemplate'] = function (template, rewriterCallback) {
var templateNode = $elf['getTemplateNode'](template);
var rewritten = rewriterCallback(templateNode.text);
templateNode.text = rewritten;
templateNode.isRewritten = true;
};
$elf['createJavaScriptEvaluatorBlock'] = function (script) {
/* This is a nonstandard extension to Mustache. */
return "{{@ " + script + "}}";
};
};
ko.mustacheTemplateEngine.prototype = new ko.templateEngine();
function installRepoTemplateEngine() {
ko.setTemplateEngine(new ko.mustacheTemplateEngine());
}
function onFileChanged(name) {
var sheetId = stylesheetIdPrefix + name;
var sheet = ObjectMemory.checkout.readFile(name).instance;
if (sheet.mimeType === "text/css") {
if (sheet.enabled) {
var s = $('#' + sheetId);
if (!s.length) {
s = $('<style type="text/css"></style>');
s[0].id = sheetId;
$("head").append(s);
}
s.text(sheet.bodyText);
} else {
$('#' + sheetId).remove();
}
}
}
function resetStylesheets() {
$("head style").each(function (index, e) {
if (e.id.substring(0, stylesheetIdPrefix.length) === stylesheetIdPrefix) {
$(e).remove();
}
});
ObjectMemory.checkout.forEachFileOfType("textFile", onFileChanged);
}
$(document).ready(main);
function main() {
resetStylesheets();
ObjectMemory.checkout.changeListeners.name.push(function (event) {
if (event.kind === 'write') {
onFileChanged(event.name);
}
});
ObjectMemory.checkout.changeListeners.commit.push(function (event) {
resetStylesheets();
});
installRepoTemplateEngine();
containerDiv.append(headerDiv);
containerDiv.append(leftDiv);
containerDiv.append(rightDiv);
containerDiv.append(panelsDiv);
containerDiv.append(footerDiv);
$("body").append(containerDiv);
/* TODO: Make font links be documents like style sheets are */
$("head").append($("<link href='http://fonts.googleapis.com/css?family=Lora&v1' rel='stylesheet' type='text/css'>"));
$("head").append($("<link href='http://fonts.googleapis.com/css?family=Mako&v1' rel='stylesheet' type='text/css'>"));
}