This repository has been archived by the owner on Apr 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
/
template_loader.js
76 lines (70 loc) · 2.55 KB
/
template_loader.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
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
TemplateLoader = function(filetree, spark) {
this.spark = spark;
this.filetree = filetree;
this.pendingWrites = 0;
this.callback = function() {};
}
TemplateLoader.prototype.loadTemplate = function(callback) {
this.pendingWrites = 8;
this.callback = callback;
this.readFile(chrome.runtime.getURL('sample_app/manifest_2.json'),
'manifest.json');
this.readFile(chrome.runtime.getURL('sample_app/index.html'), 'index.html');
this.readFile(chrome.runtime.getURL('sample_app/main.js'), 'main.js');
this.readFile(chrome.runtime.getURL('sample_app/main.css'), 'main.css');
this.readFile(chrome.runtime.getURL('sample_app/icon_16.png'), 'icon_16.png');
this.readFile(chrome.runtime.getURL('sample_app/README.md'), 'README.md');
this.readFile(chrome.runtime.getURL('sample_app/icon_128.png'),
'icon_128.png');
this.readFile(chrome.runtime.getURL('sample_app/hello_world.png'),
'hello_world.png');
}
TemplateLoader.prototype.readFile = function(url, name) {
var file = new XMLHttpRequest();
file.open("GET", url, true);
file.responseType = "blob";
var templateLoader = this;
file.onreadystatechange = function() {
if (file.readyState === 4)
if (file.status === 200)
templateLoader.writeFile(name, file.response);
}
file.send(null);
}
TemplateLoader.prototype.writeFiles = function(entries, callback) {
this.callback = callback;
this.pendingWrites = entries.length;
var templateLoader = this;
for (var i = 0; i < entries.length; ++i) {
entries[i].file(function(file) {
var reader = new FileReader();
reader.readAsArrayBuffer(file);
reader.onload = function(ev) {
templateLoader.writeFile(file.name, ev.target.result)
};
} , function() {});
}
}
TemplateLoader.prototype.writeFile = function(name, content) {
var templateLoader = this;
var activeProject = this.spark.projects[this.spark.ActiveProjectName];
activeProject.getFile(
name, {create: true},
function(entry) {
entry.createWriter(function(writer) {
writer.truncate(0);
writer.onwriteend = function() {
var blob = new Blob([content]);
writer.write(blob);
writer.onwriteend = function() {
templateLoader.pendingWrites--;
if (!templateLoader.pendingWrites)
templateLoader.callback();
};
};
});
});
}