-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
118 lines (100 loc) · 2.96 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
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
var cerebral = require('cerebral');
var angular = global.angular || require('angular');
var Store = require('immutable-store');
var EventEmitter = require('events').EventEmitter;
var Value = cerebral.Value;
var Factory = function (state, defaultArgs) {
var eventHub = new EventEmitter();
var initialState = Store(state);
state = initialState;
var controller = cerebral.Controller({
defaultArgs: defaultArgs,
onReset: function () {
state = initialState;
},
onSeek: function (seek, isPlaying, currentRecording) {
state = state.import(currentRecording.initialState);
eventHub.emit('change', state);
},
onUpdate: function () {
eventHub.emit('change', state);
},
onRemember: function () {
eventHub.emit('remember', state);
},
onGet: function (path) {
return Value(path, state);
},
onSet: function (path, value) {
path = path.slice();
var key = path.pop();
state = Value(path, state).set(key, value);
},
onUnset: function (path) {
path = path.slice();
var key = path.pop();
state = Value(path, state).unset(key);
},
onPush: function (path, value) {
state = Value(path, state).push(value);
},
onSplice: function () {
var args = [].slice.call(arguments);
var value = Value(args.shift(), state);
state = value.splice.apply(value, args);
},
onMerge: function (path, value) {
state = Value(path, state).merge(value);
},
onConcat: function () {
var args = [].slice.call(arguments);
var value = Value(args.shift(), state);
state = value.concat.apply(value, args);
},
onPop: function (path) {
state = Value(path, state).pop();
},
onShift: function (path) {
state = Value(path, state).shift();
},
onUnshift: function (path, value) {
state = Value(path, state).unshift(value);
}
});
controller.injectState = function ($scope, paths) {
var update = function (newState) {
Object.keys(paths).forEach(function (key) {
$scope[key] = Value(paths[key], newState || state);
});
newState && $scope.$apply();
};
$scope.$on('$destroy', function () {
eventHub.off('change', update);
});
eventHub.on('change', update);
eventHub.on('remember', update);
update();
};
return controller;
};
angular.module('cerebral', [])
.provider('cerebral', function () {
var services = ['$http'];
var state = {};
var defaultArgs = {};
this.setState = function (initialState) {
state = initialState;
};
this.setDefaultArgs = function (args) {
defaultArgs = args;
};
this.$get = services.concat([function () {
var args = arguments;
defaultArgs.services = services.reduce(function (services, service, index) {
services[service] = args[index];
return services;
}, {});
return new Factory(state, defaultArgs);
}]);
});
module.exports = angular;