- Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathangular-intercom.js
More file actions
Latest commit
299 lines (268 loc) · 8.58 KB
/
Copy pathangular-intercom.js
File metadata and controls
299 lines (268 loc) · 8.58 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/**
* @license angular-intercom
* (c) 2014-2015 PatrickJS gdi2290.com
* License: MIT
*/
(function(root,factory){
// AMD
if(typeofdefine==='function'&&define.amd){
define(['angular'],function(angular){
returnfactory(root,angular,root.Intercom);
});
}
// Node.js
elseif(typeofexports==='object'){
// Optional require in the intercom
varintercom=null;
try{
intercom=require('intercom')
}catch(error){
}
module.exports=factory({},require('angular'),intercom);
}
// Angular
elseif(angular){
factory(root,root.angular,root.Intercom);
}
}(this,function(global,angular,Intercom){
'use strict';
if(Intercom&&global&&!global.Intercom){global.Intercom=Intercom;}
function_capitalize_(string){
return(!string) ? '' : string.charAt(0).toUpperCase()+string.substring(1).toLowerCase();
}
varintercomSettings=global.IntercomSettings||global.intercomSettings;
varintercom_exist=false;
// if intercom exist then reattach
if(angular.isFunction(Intercom)){
global.Intercom('reattach_activator');
if(intercomSettings){
global.Intercom('update',intercomSettings);
}
intercom_exist=true;
}else{
// build Intercom for async loading
// see commented out code at the bottom for more details
varbuild_intercom=function(){
build_intercom.c(arguments);
};
build_intercom.q=[];
build_intercom.c=function(args){
build_intercom.q.push(args);
};
global.Intercom=build_intercom;
}
varinstance=false;
// Create a script tag with moment as the source
// and call our onScriptLoad callback when it
// has been loaded
functioncreateScript(url,appID){
if(!document){return;}
varscript=document.createElement('script');
script.type='text/javascript';
script.async=true;
script.src=url+appID;
// Attach the script tag to the document head
vars=document.getElementsByTagName('head')[0];
s.appendChild(script);
}
varconfig={
'asyncLoading': false,
'scriptUrl': 'https://widget.intercom.io/widget/',// <INSERT APP_ID HERE>
'appID': '',
'development': false
};
// Allow constructor to be used for both $intercom and Intercom services
function$IntercomProvider(){
varprovider=this;
angular.forEach(config,function(val,key){
provider[key]=function(newValue){
config[key]=newValue||val;
returnprovider;
};
});
provider.$get=['$rootScope','$log','IntercomSettings',function($rootScope,$log,IntercomSettings){
// warn the user if they inject both $intercom and Intercom
if(instance){
$log.warn('Please use consider using either $intercom or Intercom not both');
}
instance=true;
var_options;
varinitializeOptions=function(){
_options={};
// ensure appID is added to _options
if(config.appID){
_options.app_id=_options.app_id||config.appID;
}
angular.extend(_options,IntercomSettings);
};
initializeOptions();
if(intercom_exist){
global.Intercom('reattach_activator');
global.Intercom('update',_options);
}
if(config.asyncLoading){
createScript(config.scriptUrl,config.appID);
}
function$intercom(){
global.Intercom.apply(global.Intercom,arguments);
return$intercom;
}
varmethods={
boot: function(options){
angular.extend(_options,options||{});
if(!_options.app_id&&config.appID){
_options.app_id=config.appID;
}
if(options.app_id&&options.app_id!==_options.app_id){
_options.app_id=options.app_id;
}
global.Intercom('boot',_options);
return$intercom;
},
update: function(data){
if(data){
if(!data.app_id&&config.appID){
data.app_id=config.appID;
}
if(data.app_id&&data.app_id!==config.app_id){
config.app_id=data.app_id;
}
global.Intercom('update',data);
}else{
global.Intercom('update');
}
return$intercom;
},
trackEvent: function(eventName,data){
global.Intercom('trackEvent',eventName,data);
return$intercom;
},
showMessages: function(){
global.Intercom('showMessages');
return$intercom;
},
showNewMessage: function(data){
if(data){
global.Intercom('showNewMessage',data);
}else{
global.Intercom('showNewMessage');
}
return$intercom;
},
hideMessages: function(){
// Not sure this is even a real intercom api
global.Intercom('hideMessages');
return$intercom;
},
shutdown: function(){
global.Intercom('shutdown');
initializeOptions();
return$intercom;
},
hide: function(){
global.Intercom('hide');
return$intercom;
},
show: function(){
global.Intercom('show');
return$intercom;
},
getVisitorId: function(){
returnglobal.Intercom('getVisitorId');
},
reattachActivator: function(){
global.Intercom('reattach_activator');
return$intercom;
}
};
// this allows you to use methods prefixed with '$' to safe $apply on $rootScope
/*
$intercom.trackEvent( 'event', {data: 'data'});
$intercom.$trackEvent('event', {data: 'data'}); // digest loop has been triggered
*/
functionbuildMethod(func,method){
$intercom[method]=func;
$intercom['$'+method]=function(){
func.apply(Intercom,arguments);
if(!$rootScope.$$phase){$rootScope.$apply();}
return$intercom;
};
}
angular.forEach(methods,buildMethod);
// 3 events exposed by intercom allowing you to hook in callbacks
varisEvent={
'show': true,
'hide': true,
'activatorClick': true
};
$intercom.$on=function(event,callback){
if(!isEvent[event]){return;}
global.Intercom('on'+_capitalize_(event),function(){
if($rootScope.$$phase){
callback();
}else{
$rootScope.$apply(callback);
}
});
return$intercom;
};
$intercom.on=function(event,callback){
if(!isEvent[event]){return;}
global.Intercom('on'+_capitalize_(event),callback);
return$intercom;
};
// experimental '$$defineMethod' method to extend $intercom
$intercom.$$defineMethod=function(method){
if(!method){return;}
buildMethod(method,function(){
varargs=Array.prototype.slice.call(arguments);
global.Intercom.apply(global.Intercom,args.unshift(method));
return$intercom;
});
};
return$intercom;
}];// end $get
}
angular.module('ngIntercom',[])
// some people use .value pattern to config their services
.value('IntercomSettings',{})
.provider('$intercom',$IntercomProvider)
.provider('Intercom',$IntercomProvider);
// allow you to use either module
angular.module('angular-intercom',['ngIntercom']);
returnangular.module('ngIntercom');
// Intercom's script break down
/*
(function(){
// if intercom exist then reattach
var _intercom = window.Intercom;
if (typeof _intercom === "function" ) {
_global.intercom('reattach_activator');
_global.intercom('update', window.intercomSettings );
} else {
// build Intercom for async loading
var intercom = function() {
intercom.c(arguments);
};
intercom.q = [];
intercom.c = function(args) {
intercom.q.push(args);
};
window.Intercom = intercom;
function listener() {
var script = document.createElement('script');
script.type = 'text/javascript';
script.async = true;
script.src='https://widget.intercom.io/widget/<INSERT APP_ID HERE>';
var $script = document.getElementsByTagName('script')[0];
$script.parentNode.insertBefore(script, $script);
}
if (window.attachEvent) {
window.attachEvent('onload',listener);
} else {
window.addEventListener('load',listener,false);
}
}
})();
*/
}));