-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
74 lines (66 loc) · 1.65 KB
/
index.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
var settings = require('settings')
, angular = require('angularjs');
function keyName(name) {
return 'settings:' + name;
}
function clear(name, next) {
var key = keyName(name)
, obj = {};
obj[key] = {};
chrome.storage.sync.set(obj, function (obj) {
next && next();
});
}
function load(manager, next) {
var key = keyName(manager.name);
chrome.storage.sync.get(key, function (obj) {
manager.load(obj[key]);
next && next();
});
}
function save(manager, next) {
var key = keyName(manager.name)
, obj = {};
obj[key] = manager.json();
chrome.storage.sync.set(obj, function (obj) {
next && next();
});
}
var mod = angular.module('settings-chrome-storage', [])
.directive('clearChromeStorage', function () {
return {
restrict: 'A',
link: function (scope, el, attrs) {
var name = attrs.clearChromeStorage;
el[0].addEventListener('click', function () {
clear(name, function () {
location.reload();
});
});
}
};
})
.directive('settingsChromeStorage', function () {
return {
restrict: 'A',
link: function (scope, el, attrs) {
if (!attrs.settingsManager) {
console.warn('chrome-storage can only be used in connection with settings-manager');
return;
}
var name = attrs.settingsChromeStorage
, manager = settings.getSettings(name);
scope.$watch('settings', function (value) {
save(manager);
}, true);
load(manager, function () {
scope.$digest();
});
}
};
});
module.exports = {
load: load,
save: save,
module: mod
};