From 0b1a53da37386c601f1426255a28b808374d9af6 Mon Sep 17 00:00:00 2001 From: Simon Eisenmann Date: Wed, 5 Mar 2014 18:12:26 +0100 Subject: [PATCH] Implemented Angular Module support in plugins. Added better plugin examle. Added support to serve static resources from extra subfolder static. --- doc/plugin-example.js | 88 +++++++++++++++++++++++ src/app/spreed-speakfreely-server/main.go | 10 +++ static/js/app.js | 11 ++- static/js/main.js | 15 +++- static/js/plugins/example1.js | 8 --- 5 files changed, 119 insertions(+), 13 deletions(-) create mode 100644 doc/plugin-example.js delete mode 100644 static/js/plugins/example1.js diff --git a/doc/plugin-example.js b/doc/plugin-example.js new file mode 100644 index 00000000..9f4f1ea4 --- /dev/null +++ b/doc/plugin-example.js @@ -0,0 +1,88 @@ +/* + * Spreed Speak Freely. + * Copyright (C) 2013-2014 struktur AG + * + * This file is part of Spreed Speak Freely. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ +define(['angular'], function(angular) { + + return { + + // Angular modules - either overwrite existing modules or add new + // ones. All app dependencies are already loaded so modules defined + // here will be used instead of the default ones. + // To load add a new module to the angular app, append the name of + // the module to the modules array. + // Defining a module in a plugin is optional. + module: function(modules) { + + // Create and add a new module. + // See http://docs.angularjs.org/guide/module for details on Angular. + var module = angular.module('myExamplePluginModule', []). + config([function() { // provider-injector + // This is an example of config block. + // You can have as many of these as you want. + // You can only inject Providers (not instances) + // into the config blocks. + console.log("Configuring myExamplePluginModule plugin ...") + }]). + run(["$rootScope", function($rootScope) { // instance-injector + // This is an example of a run block. + // You can have as many of these as you want. + // You can only inject instances (not Providers) + // into the run blocks. + console.log("Initializing myExamplePluginModule plugin ..."); + }]); + + // Add an example service on module level. + module.service('myExamplePluginModuleService', [function() { + return { + nice: "plugin" + } + }]); + + // Inject new module into modules array so that it gets loaded + // with the App. + modules.push(module.name); + + }, + + // Initialize function for this module. + // Add your angular stuff here. The app is the base Angular module for + // the web application. The plugin initialize function is called after + // the app initialize function. + // Defining the initialize function is optional. + initialize: function(app) { + + console.log("Initializing plugin-example ..."); + + // Add some directives and services. + app.service('myExamplePluginInitService', ["$window", function($window) { + // Service code here ... + return { + awesome: "plugin service" + } + }]); + app.directive('myExamplePluginInitDirective', [function() { + // Directive code here ... + }]); + + } + + } + +}); \ No newline at end of file diff --git a/src/app/spreed-speakfreely-server/main.go b/src/app/spreed-speakfreely-server/main.go index b7ad91ee..8c9ec9de 100644 --- a/src/app/spreed-speakfreely-server/main.go +++ b/src/app/spreed-speakfreely-server/main.go @@ -292,6 +292,16 @@ func runner(runtime phoenix.Runtime) error { r.Handle("/ws", makeWsHubHandler(hub)) r.HandleFunc("/{room}", httputils.MakeGzipHandler(roomHandler)) makeApiHandler(r, tokenProvider) + + // Add extra/static support if configured and exists. + if extraFolder != "" { + extraFolderStatic := path.Join(extraFolder, "static") + if _, err = os.Stat(extraFolderStatic); err == nil { + r.Handle("/extra/static/{path:.*}", http.StripPrefix(fmt.Sprintf("%sextra", basePath), httputils.FileStaticServer(http.Dir(extraFolder)))) + log.Printf("Added URL handler /extra/static/... for static files in %s/...\n", extraFolderStatic) + } + } + runtime.DefaultHTTPHandler(r) return runtime.Start() diff --git a/static/js/app.js b/static/js/app.js index 183f5e24..02bfa9bd 100644 --- a/static/js/app.js +++ b/static/js/app.js @@ -41,9 +41,16 @@ define([ ], function(require, $, _, angular, modernizr, moment, services, directives, filters, controllers) { - var initialize = function() { + var initialize = function(ms) { - var app = angular.module('app', ['ui.bootstrap', 'ngSanitize', 'ngAnimate', 'ngHumanize', 'ngRoute', 'dialogs']); + var modules = ['ui.bootstrap', 'ngSanitize', 'ngAnimate', 'ngHumanize', 'ngRoute', 'dialogs']; + if (ms && ms.length) { + _.each(ms, function(module) { + modules.push(module); + }); + } + + var app = angular.module('app', modules); services.initialize(app); directives.initialize(app); filters.initialize(app); diff --git a/static/js/main.js b/static/js/main.js index 477884d9..8f381110 100644 --- a/static/js/main.js +++ b/static/js/main.js @@ -149,13 +149,22 @@ define([ } }); require(load, function(App) { - var app = App.initialize(); var args = Array.prototype.slice.call(arguments, 1); + // Add Angular modules from plugins. + var modules = []; + _.each(args, function(plugin) { + if (plugin && plugin.module) { + plugin.module(modules); + } + }); + // Init Angular app. + var app = App.initialize(modules); + // Init plugins. _.each(args, function(plugin) { if (plugin && plugin.initialize) { - plugin.initialize(app) + plugin.initialize(app); } - }) + }); }); }); diff --git a/static/js/plugins/example1.js b/static/js/plugins/example1.js deleted file mode 100644 index de993d14..00000000 --- a/static/js/plugins/example1.js +++ /dev/null @@ -1,8 +0,0 @@ -// Example plugin shows the basic API. -define([], function() { - return { - initialize: function(app) { - console.log("Example plugin 1 loaded.", app); - } - } -});