Skip to content

Commit 06c0771

Browse files
WebRatio Emulation Stub for Ripple
This is required to support activity notifications, which are not natively emulated in the current Ripple 0.9.23.
1 parent 6c1a835 commit 06c0771

File tree

1 file changed

+204
-0
lines changed

1 file changed

+204
-0
lines changed

webratio/emulation_stub.js

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
function createStubs() {
2+
3+
var notifications = window.top.ripple && window.top.ripple('platform/cordova/2.0.0/bridge/notification');
4+
5+
var audioContext = (function() {
6+
// Determine if the Audio API is supported by this browser
7+
var AudioApi = window.AudioContext;
8+
if (!AudioApi) {
9+
AudioApi = window.webkitAudioContext;
10+
}
11+
12+
if (AudioApi) {
13+
// The Audio API is supported, so create a singleton instance of the
14+
// AudioContext
15+
return new AudioApi();
16+
}
17+
18+
return undefined;
19+
}());
20+
21+
var activityUI = new function () {
22+
var act = {};
23+
act.cancelable = false;
24+
25+
act.activityElement = document.createElement("DIV");
26+
act.activityElement.style.background = "rgba(0, 0, 0, 0.5)";
27+
act.activityElement.style.position = "fixed";
28+
act.activityElement.style.width = "100%";
29+
act.activityElement.style.height = "100%";
30+
act.activityElement.style.zIndex = "10000";
31+
/* > Create div wrapper */
32+
act.div = document.createElement("DIV");
33+
act.div.style.background = "#000";
34+
act.div.style.padding = "10px";
35+
act.div.style.borderRadius = "5px";
36+
act.div.style.margin = "0 auto";
37+
act.div.style.width = "200px";
38+
act.div.style.maxWidth = "80%";
39+
act.div.style.color = "#fff";
40+
act.div.style.textAlign = "center";
41+
act.div.style.top = "50%";
42+
act.div.style.position = "relative";
43+
act.div.style.marginTop = "-30px";
44+
act.activityElement.appendChild(act.div);
45+
/* > Create title */
46+
act.title = document.createElement("DIV");
47+
act.title.style.fontWeight = "700";
48+
act.div.appendChild(act.title);
49+
/* > Create description */
50+
act.description = document.createElement("DIV");
51+
act.div.appendChild(act.description);
52+
53+
54+
act.setTitle = function(title) {
55+
act.title.innerHTML = title;
56+
}
57+
58+
act.setDescription = function(description) {
59+
act.description.innerHTML = description;
60+
}
61+
62+
act.setCancelable = function(cancelable) {
63+
act.cancelable = cancelable;
64+
}
65+
66+
act.show = function (title, description, cancelable) {
67+
act.setTitle(title || "");
68+
act.setDescription(description || "");
69+
act.setCancelable(cancelable || false);
70+
if (!act.activityElement.parentNode) {
71+
document.body.appendChild(act.activityElement);
72+
}
73+
};
74+
act.hide = function () {
75+
if (act.activityElement.parentNode) {
76+
act.activityElement.parentNode.removeChild(act.activityElement);
77+
}
78+
};
79+
80+
return act;
81+
};
82+
83+
function log() {
84+
var args = [].slice.call(arguments, 0);
85+
args.unshift("[notification]");
86+
console.log.apply(console, args);
87+
}
88+
89+
return {
90+
Notification : {
91+
alert : function(message, title, buttonLabel) {
92+
if (title) {
93+
message = title + "\n" + message;
94+
}
95+
if (notifications) {//Ripple Notifications
96+
return notifications.alert(undefined, undefined, [message]);
97+
} else {
98+
window.alert(message);
99+
}
100+
},
101+
102+
confirm : function(message, title, buttonLabels) {
103+
104+
if (notifications) {//Ripple Notifications
105+
var promise = new Promise(function(resolve, reject) {
106+
var callback = function(index) {resolve(index)}
107+
notifications.confirm(callback, undefined, [message, title, buttonLabels]);
108+
});
109+
110+
return promise;
111+
} else {
112+
if (title) {
113+
message = title + "\n" + message;
114+
}
115+
if (window.confirm(message)) {
116+
return 1
117+
} else {
118+
return 2; // Cancel
119+
}
120+
}
121+
122+
},
123+
124+
prompt : function(message, title, buttonLabels, defaultText) {
125+
if (title) {
126+
message = title + "\n" + message;
127+
}
128+
var result = window.prompt(message, defaultText || '');
129+
130+
if (result === null) {
131+
return {
132+
buttonIndex : 2,
133+
input1 : ''
134+
}; // Cancel
135+
} else {
136+
return {
137+
buttonIndex : 1,
138+
input1 : result
139+
}; // OK
140+
}
141+
142+
},
143+
144+
activityStart: function (title, description, cancelable) {
145+
activityUI.show(title, description, cancelable);
146+
},
147+
148+
activityStop: function () {
149+
activityUI.hide();
150+
},
151+
152+
progressStart : function(title, message) {
153+
log("progressStart");
154+
},
155+
156+
progressStop : function() {
157+
log("progressStop");
158+
},
159+
160+
progressValue : function(value) {
161+
log("progressValue");
162+
},
163+
164+
vibrate : function(success, error, args) {
165+
log("vibrate");
166+
},
167+
168+
beep : function(times) {
169+
if (times > 0) {
170+
var BEEP_DURATION = 700;
171+
var BEEP_INTERVAL = 300;
172+
173+
if (audioContext) {
174+
// Start a beep, using the Audio API
175+
var osc = audioContext.createOscillator();
176+
osc.type = 0; // sounds like a "beep"
177+
osc.connect(audioContext.destination);
178+
osc.start(0);
179+
180+
setTimeout(function() {
181+
// Stop the beep after the BEEP_DURATION
182+
osc.stop(0);
183+
184+
if (--times > 0) {
185+
// Beep again, after a pause
186+
setTimeout(function() {
187+
navigator.notification.beep(times);
188+
}, BEEP_INTERVAL);
189+
}
190+
191+
}, BEEP_DURATION);
192+
} else if (typeof (console) !== 'undefined'
193+
&& typeof (console.log) === 'function') {
194+
// Audio API isn't supported, so just write `beep` to
195+
// the console
196+
for (var i = 0; i < times; i++) {
197+
log('Beep!');
198+
}
199+
}
200+
}
201+
}
202+
}
203+
};
204+
};

0 commit comments

Comments
 (0)