2013-10-09 01:07:55 +02:00
|
|
|
/*
|
|
|
|
* This file contains the plugin definitions
|
|
|
|
*/
|
|
|
|
|
2013-10-09 00:57:43 +02:00
|
|
|
plugins = angular.module('plugins', []);
|
2013-10-08 23:06:47 +02:00
|
|
|
|
2013-10-09 01:07:55 +02:00
|
|
|
/*
|
|
|
|
* Definition of a user provided plugin with sensible default values
|
|
|
|
*
|
|
|
|
* User plugins are created by providing a contentForMessage function
|
|
|
|
* that parses a string and return any additional content.
|
|
|
|
*/
|
2013-10-09 00:57:43 +02:00
|
|
|
var Plugin = function(contentForMessage) {
|
|
|
|
|
|
|
|
return {
|
|
|
|
contentForMessage: contentForMessage,
|
|
|
|
exclusive: false,
|
2013-10-13 20:57:54 +02:00
|
|
|
name: "additional content"
|
2013-10-09 00:57:43 +02:00
|
|
|
}
|
|
|
|
}
|
2013-10-08 23:11:05 +02:00
|
|
|
|
2013-10-09 01:07:55 +02:00
|
|
|
/*
|
|
|
|
* This service provides access to the plugin manager
|
|
|
|
*
|
|
|
|
* The plugin manager is where the various user provided plugins
|
|
|
|
* are registered. It is responsible for finding additional content
|
|
|
|
* to display when messages are received.
|
|
|
|
*
|
|
|
|
*/
|
2013-10-13 20:33:09 +02:00
|
|
|
plugins.service('plugins', ['userPlugins', '$sce', function(userPlugins, $sce) {
|
2013-10-08 23:11:05 +02:00
|
|
|
|
2013-10-14 17:02:15 +02:00
|
|
|
var nsfwRegexp = new RegExp('nsfw', 'i');
|
2013-10-13 20:44:37 +02:00
|
|
|
|
2013-10-09 01:07:55 +02:00
|
|
|
/*
|
|
|
|
* Defines the plugin manager object
|
|
|
|
*/
|
2013-10-08 23:55:30 +02:00
|
|
|
var PluginManagerObject = function() {
|
2013-10-09 01:07:55 +02:00
|
|
|
|
2013-10-08 23:55:30 +02:00
|
|
|
var plugins = [];
|
2013-10-09 01:07:55 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Register the user provides plugins
|
|
|
|
*
|
|
|
|
* @param userPlugins user provided plugins
|
|
|
|
*/
|
2013-10-09 00:57:43 +02:00
|
|
|
var registerPlugins = function(userPlugins) {
|
|
|
|
for (var i = 0; i < userPlugins.length; i++) {
|
|
|
|
plugins.push(userPlugins[i]);
|
|
|
|
};
|
2013-10-08 23:55:30 +02:00
|
|
|
}
|
2013-10-08 23:06:47 +02:00
|
|
|
|
2013-10-09 01:07:55 +02:00
|
|
|
/*
|
|
|
|
* Iterates through all the registered plugins
|
|
|
|
* and run their contentForMessage function.
|
|
|
|
*/
|
2013-10-08 23:55:30 +02:00
|
|
|
var contentForMessage = function(message) {
|
2013-10-08 23:06:47 +02:00
|
|
|
|
2013-10-08 23:55:30 +02:00
|
|
|
var content = [];
|
|
|
|
for (var i = 0; i < plugins.length; i++) {
|
2013-10-13 20:44:37 +02:00
|
|
|
|
2013-10-13 20:49:38 +02:00
|
|
|
var nsfw = false;
|
2013-10-13 20:44:37 +02:00
|
|
|
var visible = true;
|
|
|
|
if (message.match(nsfwRegexp)) {
|
2013-10-13 20:49:38 +02:00
|
|
|
var nsfw = true;
|
2013-10-13 20:44:37 +02:00
|
|
|
var visible = false;
|
|
|
|
}
|
|
|
|
|
2013-10-08 23:55:30 +02:00
|
|
|
var pluginContent = plugins[i].contentForMessage(message);
|
|
|
|
if (pluginContent) {
|
2013-10-13 20:44:37 +02:00
|
|
|
var pluginContent = {'visible': visible,
|
2013-10-13 20:49:38 +02:00
|
|
|
'content': $sce.trustAsHtml(pluginContent),
|
2013-10-13 20:57:54 +02:00
|
|
|
'nsfw': nsfw,
|
|
|
|
'name': plugins[i].name }
|
2013-10-13 20:33:09 +02:00
|
|
|
|
2013-10-08 23:55:30 +02:00
|
|
|
content.push(pluginContent);
|
2013-10-08 23:06:47 +02:00
|
|
|
|
2013-10-08 23:55:30 +02:00
|
|
|
if (plugins[i].exclusive) {
|
|
|
|
break;
|
|
|
|
}
|
2013-10-08 23:06:47 +02:00
|
|
|
}
|
|
|
|
}
|
2013-10-08 23:55:30 +02:00
|
|
|
return content;
|
2013-10-08 23:06:47 +02:00
|
|
|
}
|
|
|
|
|
2013-10-08 23:55:30 +02:00
|
|
|
return {
|
2013-10-09 00:57:43 +02:00
|
|
|
registerPlugins: registerPlugins,
|
2013-10-08 23:55:30 +02:00
|
|
|
contentForMessage: contentForMessage
|
|
|
|
}
|
2013-10-08 23:06:47 +02:00
|
|
|
}
|
|
|
|
|
2013-10-09 01:07:55 +02:00
|
|
|
// Instanciates and registers the plugin manager.
|
2013-10-08 23:55:30 +02:00
|
|
|
this.PluginManager = new PluginManagerObject();
|
2013-10-09 00:57:43 +02:00
|
|
|
this.PluginManager.registerPlugins(userPlugins.plugins);
|
2013-10-09 01:07:55 +02:00
|
|
|
|
2013-10-08 23:06:47 +02:00
|
|
|
}]);
|
|
|
|
|
2013-10-09 01:07:55 +02:00
|
|
|
/*
|
|
|
|
* This factory exposes the collection of user provided plugins.
|
|
|
|
*
|
|
|
|
* To create your own plugin, you need to:
|
|
|
|
*
|
|
|
|
* 1. Define it's contentForMessage function. The contentForMessage
|
|
|
|
* function takes a string as a parameter and returns a HTML string.
|
|
|
|
*
|
|
|
|
* 2. Instanciate a Plugin object with contentForMessage function as it's
|
|
|
|
* argument.
|
|
|
|
*
|
|
|
|
* 3. Add it to the plugins array.
|
|
|
|
*
|
|
|
|
*/
|
2013-10-09 00:57:02 +02:00
|
|
|
plugins.factory('userPlugins', function() {
|
|
|
|
|
2013-10-13 21:14:43 +02:00
|
|
|
var urlRegexp = RegExp(/(http|ftp|https):\/\/[\w-]+(\.[\w-]+)+([\w.,@?^=%&:\/~+#-]*[\w@?^=%&\/~+#-])?/);
|
|
|
|
|
2013-10-09 00:57:02 +02:00
|
|
|
var youtubePlugin = new Plugin(function(message) {
|
2013-10-08 23:06:47 +02:00
|
|
|
|
|
|
|
if (message.indexOf('youtube.com') != -1) {
|
|
|
|
var index = message.indexOf("?v=");
|
|
|
|
var token = message.substr(index+3);
|
|
|
|
return '<iframe width="560" height="315" src="http://www.youtube.com/embed/' + token + '" frameborder="0" allowfullscreen></iframe>'
|
|
|
|
}
|
|
|
|
|
2013-10-09 00:57:02 +02:00
|
|
|
return null;
|
|
|
|
});
|
2013-10-13 21:00:00 +02:00
|
|
|
youtubePlugin.name = 'youtube video';
|
2013-10-08 23:06:47 +02:00
|
|
|
|
2013-10-09 00:57:02 +02:00
|
|
|
var urlPlugin = new Plugin(function(message) {
|
2013-10-13 21:14:43 +02:00
|
|
|
var url = message.match(urlRegexp);
|
2013-10-08 23:06:47 +02:00
|
|
|
if (url) {
|
2013-10-14 21:18:00 +02:00
|
|
|
return '<a target="_blank" href="' + url[0] + '">' + url[0] + '</a>';
|
2013-10-08 23:06:47 +02:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
|
2013-10-09 00:57:02 +02:00
|
|
|
});
|
2013-10-13 21:00:00 +02:00
|
|
|
urlPlugin.name = 'url';
|
2013-10-08 23:06:47 +02:00
|
|
|
|
2013-10-09 00:57:02 +02:00
|
|
|
var imagePlugin = new Plugin(function(message) {
|
2013-10-13 21:14:43 +02:00
|
|
|
|
|
|
|
var url = message.match(urlRegexp);
|
|
|
|
|
|
|
|
if (url) {
|
|
|
|
var url = url[0]; /* Actually parse one url per message */
|
|
|
|
if (url.match(/png$|gif$|jpg$|jpeg$/)) {
|
|
|
|
return '<img src="' + url + '" height="300">';
|
|
|
|
}
|
2013-10-08 23:06:47 +02:00
|
|
|
}
|
|
|
|
return null;
|
2013-10-09 00:57:02 +02:00
|
|
|
});
|
2013-10-13 21:00:00 +02:00
|
|
|
imagePlugin.name = 'image';
|
2013-10-08 23:06:47 +02:00
|
|
|
|
|
|
|
return {
|
2013-10-09 00:57:02 +02:00
|
|
|
plugins: [youtubePlugin, urlPlugin, imagePlugin]
|
2013-10-08 23:06:47 +02:00
|
|
|
}
|
2013-10-09 00:57:02 +02:00
|
|
|
});
|