2013-10-09 00:57:43 +02:00
|
|
|
plugins = angular.module('plugins', []);
|
2013-10-08 23:06:47 +02:00
|
|
|
|
2013-10-09 00:57:43 +02:00
|
|
|
var Plugin = function(contentForMessage) {
|
|
|
|
|
|
|
|
return {
|
|
|
|
contentForMessage: contentForMessage,
|
|
|
|
exclusive: false,
|
|
|
|
}
|
|
|
|
}
|
2013-10-08 23:11:05 +02:00
|
|
|
|
2013-10-09 00:57:43 +02:00
|
|
|
plugins.service('plugins', ['userPlugins', function(userPlugins) {
|
2013-10-08 23:11:05 +02:00
|
|
|
|
2013-10-08 23:06:47 +02:00
|
|
|
|
2013-10-08 23:55:30 +02:00
|
|
|
var PluginManagerObject = function() {
|
2013-10-08 23:11:05 +02:00
|
|
|
|
2013-10-08 23:55:30 +02:00
|
|
|
var 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-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++) {
|
|
|
|
var pluginContent = plugins[i].contentForMessage(message);
|
|
|
|
if (pluginContent) {
|
2013-10-09 00:57:43 +02:00
|
|
|
var pluginContent = {'visible': false,
|
|
|
|
'content': pluginContent }
|
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-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-08 23:06:47 +02:00
|
|
|
}]);
|
|
|
|
|
2013-10-09 00:57:02 +02:00
|
|
|
plugins.factory('userPlugins', function() {
|
|
|
|
|
|
|
|
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-08 23:06:47 +02:00
|
|
|
|
2013-10-09 00:57:02 +02:00
|
|
|
var urlPlugin = new Plugin(function(message) {
|
2013-10-08 23:06:47 +02:00
|
|
|
var urlPattern = /(http|ftp|https):\/\/[\w-]+(\.[\w-]+)+([\w.,@?^=%&:\/~+#-]*[\w@?^=%&\/~+#-])?/;
|
|
|
|
var url = message.match(urlPattern);
|
|
|
|
if (url) {
|
|
|
|
return '<a href="' + url[0] + '">' + message + '</a>';
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
|
2013-10-09 00:57:02 +02:00
|
|
|
});
|
2013-10-08 23:06:47 +02:00
|
|
|
|
2013-10-09 00:57:02 +02:00
|
|
|
var imagePlugin = new Plugin(function(message) {
|
|
|
|
var urls = message.match(/https?:\/\/[^\s]*\.(jpg|png|gif)\b/)
|
|
|
|
if (urls != null) {
|
|
|
|
var url = urls[0]; /* Actually parse one url per message */
|
|
|
|
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-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
|
|
|
});
|