forked from kudago/smart-app-banner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
162 lines (145 loc) · 4.4 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
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
var extend = require('xtend/mutable');
var q = require('component-query');
var doc = require('get-doc');
var root = doc && doc.documentElement;
var cookie = require('cookie-cutter');
var ua = require('ua-parser-js');
// platform dependent functionality
var mixins = {
ios: {
appMeta: 'apple-itunes-app',
iconRels: ['apple-touch-icon-precomposed', 'apple-touch-icon'],
getStoreLink: function() {
return 'https://itunes.apple.com/' + this.options.appStoreLanguage + '/app/id' + this.appId;
}
},
android: {
appMeta: 'google-play-app',
iconRels: ['android-touch-icon', 'apple-touch-icon-precomposed', 'apple-touch-icon'],
getStoreLink: function() {
return 'http://play.google.com/store/apps/details?id=' + this.appId;
}
},
windows: {
appMeta: 'msApplication-ID',
iconRels: ['windows-touch-icon', 'apple-touch-icon-precomposed', 'apple-touch-icon'],
getStoreLink: function() {
return 'http://www.windowsphone.com/s?appid=' + this.appId;
}
}
};
var SmartBanner = function(options) {
var agent = ua(navigator.userAgent);
this.options = extend({}, {
daysHidden: 15,
daysReminder: 90,
appStoreLanguage: 'us', // Language code for App Store
button: 'OPEN', // Text for the install button
store: {
ios: 'On the App Store',
android: 'In Google Play',
windows: 'In the Windows Store'
},
price: {
ios: 'FREE',
android: 'FREE',
windows: 'FREE'
},
force: false // put platform type (ios, android, etc.) here for emulation
}, options || {});
if (this.options.force) {
this.type = this.options.force;
} else if (agent.os.name === 'Windows Phone' || agent.os.name === 'Windows Mobile') {
this.type = 'windows';
} else if (agent.os.name === 'iOS') {
this.type = 'ios';
} else if (agent.os.name === 'Android') {
this.type = 'android';
}
// Don't show banner if device isn't iOS or Android, website is loaded in app, user dismissed banner, or we have no app id in meta
if (!this.type
|| navigator.standalone
|| cookie.get('smartbanner-closed')
|| cookie.get('smartbanner-installed')) {
return;
}
extend(this, mixins[this.type]);
if (!this.parseAppId()) {
return;
}
this.create();
this.show();
};
SmartBanner.prototype = {
constructor: SmartBanner,
create: function() {
var link = this.getStoreLink();
var inStore = this.options.price[this.type] + ' - ' + this.options.store[this.type];
var icon;
for (var i = 0; i < this.iconRels.length; i++) {
var rel = q('link[rel="'+this.iconRels[i]+'"]');
if (rel) {
icon = rel.getAttribute('href');
break;
}
}
var sb = doc.createElement('div');
sb.className = 'smartbanner smartbanner-' + this.type;
sb.innerHTML = '<div class="smartbanner-container">' +
'<a href="javascript:void(0);" class="smartbanner-close">×</a>' +
'<span class="smartbanner-icon" style="background-image: url('+icon+')"></span>' +
'<div class="smartbanner-info">' +
'<div class="smartbanner-title">'+this.options.title+'</div>' +
'<div>'+this.options.author+'</div>' +
'<span>'+inStore+'</span>' +
'</div>' +
'<a href="'+link+'" class="smartbanner-button">' +
'<span class="smartbanner-button-text">'+this.options.button+'</span>' +
'</a>' +
'</div>';
//there isn’t neccessary a body
if (doc.body) {
doc.body.appendChild(sb);
}
else if (doc) {
doc.addEventListener('DOMContentLoaded', function(){
doc.body.appendChild(sb);
});
}
q('.smartbanner-button', sb).addEventListener('click', this.install.bind(this), false);
q('.smartbanner-close', sb).addEventListener('click', this.close.bind(this), false);
},
hide: function() {
root.classList.remove('smartbanner-show');
},
show: function() {
root.classList.add('smartbanner-show');
},
close: function() {
this.hide();
cookie.set('smartbanner-closed', 'true', {
path: '/',
expires: +new Date() + this.options.daysHidden * 1000 * 60 * 60 * 24
});
},
install: function() {
this.hide();
cookie.set('smartbanner-installed', 'true', {
path: '/',
expires: +new Date() + this.options.daysReminder * 1000 * 60 * 60 * 24
});
},
parseAppId: function() {
var meta = q('meta[name="' + this.appMeta + '"]');
if (!meta) {
return;
}
if (this.type === 'windows') {
this.appId = meta.getAttribute('content');
} else {
this.appId = /app-id=([^\s,]+)/.exec(meta.getAttribute('content'))[1];
}
return this.appId;
}
};
module.exports = SmartBanner;