-
Notifications
You must be signed in to change notification settings - Fork 1
/
editor.directive.js
97 lines (73 loc) · 1.88 KB
/
editor.directive.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
;(function () {
'use strict';
angular.module('editor-app' , [])
.directive('storageEditor', ['$interval', function ($interval) {
return {
scope : {},
bindToController: true,
templateUrl : 'editor.directive.html',
controllerAs: 'vmEditor',
controller: editorController
};
function editorController () {
var vm = this;
vm.storage_types = ['localStorage', 'sessionStorage'];
vm.type = vm.storage_types[1]; // Sets the default
vm.changeType = changeType;
vm.addItem = addItem;
vm.deleteItem = deleteItem;
vm.save = save;
vm.clear = clear;
vm.backup = backup;
vm.restore = restore;
// On load
activate();
////////////////////////////////////
function activate() {
setStorageType(vm.type);
vm.__backup = vm.storage;
}
////////////////////////////////////
// #region Internal Methods
////////////////////////////////////
function save () {
var actual_storage = vm.type === 'localStorage'
? localStorage
: sessionStorage;
for (var key in vm.storage) {
actual_storage.setItem(key, vm.storage[key]);
}
alert('Saved!');
};
function deleteItem (key) {
delete vm.storage[key];
};
function changeType () {
setStorageType(vm.type);
};
function addItem (item) {
vm.storage[item.key] = item.val;
vm.newItem = [];
};
function clear () {
var response = confirm('Are you sure?\n\nThis will Clear -ALL- data within ' + vm.type);
if (response) {
vm.storage.clear();
return;
}
};
function setStorageType (type) {
vm.storage = type === 'localStorage'
? localStorage
: sessionStorage;
}
function backup () {
vm.__backup = vm.storage;
};
function restore () {
vm.storage = vm.__backup;
};
// #endregion
}
}]);
}());