-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbithub-embed.js
More file actions
117 lines (95 loc) · 2.6 KB
/
bithub-embed.js
File metadata and controls
117 lines (95 loc) · 2.6 KB
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
import AppState from 'models/embed_appstate';
import Bit from 'models/bit';
import Hub from 'models/hub';
import BitList from 'bit-list/';
import Communicator from 'communicator/';
import 'can/route/';
import "style/embed.less!";
var params = can.deparam(window.location.search.substr(1));
var isLoadedFromIframe = window.parent !== window;
var appState = new AppState();
var bodyClasses = [(isLoadedFromIframe ? 'iframe-context' : 'page-context'), 'embed'];
var triggerPartition = (function(){
var partitionTimeout;
return function(bits){
clearTimeout(partitionTimeout);
partitionTimeout = setTimeout(function(){
can.trigger(bits, 'partition');
}, 100);
};
})();
var kickstart = function(hub){
var communicator = Communicator.bind(window.parent, {
updateAttrs : function(data){
appState.setAttrs(data);
},
reset : function(){
resetApp();
}
});
bodyClasses.push((params.theme || 'light') + '-theme');
//can.route.map(appState);
//can.route.ready();
appState.attr('hub', hub);
appState.setAttrs(params);
appState.connectLiveService();
if(!appState.isPublic()){
bodyClasses.push('admin-embed');
}
Bit.on('lifecycle', function(ev, bit){
var serviceIds = bit.attr('service_ids');
var bits = appState.attr('bits');
var isLive = appState.isLive();
if(appState.isPublic() && isLive && bit.isPublic()){
bits.place(bit);
} else {
if(isLive && bits.indexOf(bit) === -1 && bit.attr('decision') === appState.attr('decision')){
bits.unshift(bit);
}
}
triggerPartition(bits);
if( serviceIds && (params.view !== 'public') ){
communicator.send('loadedBits', serviceIds);
}
});
Bit.on('disapproved', function(ev, bit){
var bits = appState.attr('bits');
var index = bits.indexOf(bit);
if(appState.isPublic() && index > -1){
bits.splice(index, 1);
}
});
$('body').addClass(bodyClasses.join(' '));
var initApp = function(){
var div = $('<div id="app" />');
$('#app-wrapper').html(div);
console.log('INIT APP')
new BitList(div, {
state : appState
});
};
var resetApp = (function(){
var timeout;
return function(){
clearTimeout(timeout);
setTimeout(function(){
appState.reset();
initApp();
}, 1);
};
})();
appState.on('view', resetApp);
appState.on('order', resetApp);
appState.on('decision', resetApp);
appState.on('service_id', resetApp);
appState.on('image_only', resetApp);
appState.on('theme', function(ev, newTheme){
$('body').removeClass('dark-theme light-theme').addClass(newTheme + '-theme');
});
initApp();
};
Hub.findOne({id: params.hubId}).then(function(hub){
kickstart(hub);
}, function(){
kickstart();
});