-
-
Save pochi/3c49285fc85b1e828749 to your computer and use it in GitHub Desktop.
notification.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
var __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }; | |
window.WEB_SOCKET_SWF_LOCATION = "/plugin_assets/squall_notification/javascripts/WebSocketMain.swf"; | |
window.WEB_SOCKET_FORCE_FLASH = true; | |
window.WEB_SOCKET_DEBUG = true; | |
if (window.console == null) { | |
window.console = { | |
log: function(str) { | |
return str; | |
}, | |
error: function(str) { | |
return str; | |
} | |
}; | |
} | |
this.SquallClient = (function() { | |
var authenticated, auto_cancel, create_notification, set_event_cb, supported_popup; | |
function SquallClient(url, auth_key) { | |
this.url = url; | |
this.auth_key = auth_key; | |
try { | |
this.socket = new WebSocket(this.url + "?auth_key=" + this.auth_key); | |
this.heartbeats = []; | |
this.register_event(false); | |
} catch (error) { | |
console.log("[ERROR] " + error.message); | |
} | |
} | |
SquallClient.prototype.reconnect = function() { | |
console.log("reconnect called"); | |
clearInterval(this.ping_request); | |
try { | |
this.socket = new WebSocket(this.url + "?auth_key=" + this.auth_key + "&reconnect=true"); | |
this.heartbeats = []; | |
console.log("New websocket has taken"); | |
return this.register_event(true); | |
} catch (error) { | |
return console.log("[ERROR] " + error.message); | |
} | |
}; | |
SquallClient.prototype.register_event = function(reconnect) { | |
var self; | |
self = this; | |
this.socket.onopen = function(event) { | |
if (self.onopen == null) { | |
return false; | |
} | |
self.ping_request = setInterval(function() { | |
var currentTime; | |
currentTime = new Date().toString(); | |
self.heartbeats.push(currentTime); | |
self.socket.send(currentTime); | |
if (self.heartbeats.length >= 2) { | |
return self.reconnect(); | |
} | |
}, 30000); | |
if (!reconnect) { | |
return self.onopen(event); | |
} | |
}; | |
this.socket.onmessage = function(event) { | |
var parse_data, squall_ping_data; | |
if (self.onmessage == null) { | |
return false; | |
} | |
parse_data = event.data; | |
squall_ping_data = __indexOf.call(self.heartbeats, parse_data) >= 0; | |
if (squall_ping_data) { | |
return self.heartbeats = self.heartbeats.filter(function(e) { | |
return e !== parse_data; | |
}); | |
} else { | |
return self.onmessage(event); | |
} | |
}; | |
this.socket.onerror = function(event) { | |
if (self.onerror == null) { | |
return false; | |
} | |
clearInterval(self.ping_request); | |
return self.onerror(event); | |
}; | |
return this.socket.onclose = function(event) { | |
if (self.onclose == null) { | |
return false; | |
} | |
clearInterval(self.ping_request); | |
return self.onclose(event); | |
}; | |
}; | |
SquallClient.prototype.close = function() { | |
if (this.socket == null) { | |
return false; | |
} | |
return this.socket.close(); | |
}; | |
SquallClient.prototype.popup_request_auth = function() { | |
if (!supported_popup()) { | |
return false; | |
} | |
if (window.Notification.permission !== "granted") { | |
return window.Notification.requestPermission(); | |
} | |
}; | |
SquallClient.prototype.popup = function(title, content, options) { | |
var image_url, notify, tag; | |
if (options == null) { | |
options = {}; | |
} | |
if (!supported_popup()) { | |
return false; | |
} | |
if (!authenticated(window.Notification)) { | |
console.log("---Not authenticated popup---"); | |
return false; | |
} | |
image_url = options.image != null ? options.image : ""; | |
tag = options.tag != null ? options.tag : ""; | |
notify = create_notification(title, { | |
body: content, | |
icon: image_url, | |
tag: tag | |
}); | |
set_event_cb(notify, options); | |
notify.show(); | |
if (options.time != null) { | |
auto_cancel(notify, options.time); | |
} | |
return notify; | |
}; | |
SquallClient.prototype.popup_cancel = function(notify) { | |
if (!supported_popup()) { | |
return false; | |
} | |
return notify != null ? notify.cancel() : void 0; | |
}; | |
SquallClient.prototype.popup_check_permission = function() { | |
if (!supported_popup()) { | |
return false; | |
} | |
return window.Notification.permission; | |
}; | |
SquallClient.prototype.check_supported_popup = function() { | |
if (window.Notification != null) { | |
return true; | |
} else { | |
return false; | |
} | |
}; | |
supported_popup = function() { | |
if (window.Notification != null) { | |
return true; | |
} | |
console.log("---Not supported popup---"); | |
return false; | |
}; | |
authenticated = function() { | |
if (window.Notification.permission === "granted") { | |
return true; | |
} | |
return false; | |
}; | |
set_event_cb = function(notify, options) { | |
if ((notify == null) || (options == null)) { | |
return false; | |
} | |
if (options.onshow != null) { | |
notify.onshow = options.onshow; | |
} | |
if (options.onclick != null) { | |
notify.onclick = options.onclick; | |
} | |
if (options.onerror != null) { | |
notify.onerror = options.onerror; | |
} | |
if (options.onclose != null) { | |
return notify.onclose = options.onclose; | |
} | |
}; | |
auto_cancel = function(notify, time) { | |
if ((notify == null) || (time == null)) { | |
return false; | |
} | |
return setTimeout((function() { | |
return notify.cancel(); | |
}), time); | |
}; | |
create_notification = function(title, options) { | |
var notify; | |
notify = new window.Notification(title, options); | |
if (!notify.cancel) { | |
notify.cancel = function() { | |
return notify.close(); | |
}; | |
} | |
return notify; | |
}; | |
return SquallClient; | |
})(); | |
}).call(this); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment