Commit f1e17289 authored by Marcel Rieger's avatar Marcel Rieger
Browse files

Fix templates, add basic shortcuts functionality.

parent 65dc7f80
......@@ -9,6 +9,7 @@ from vispa.controller.filesystem import FSAjaxController
from vispa.models.user import User
from vispa.models.workspace import Workspace, WorkspaceState
from vispa.models.preference import VispaPreference, ExtensionPreference
from vispa.models.shortcuts import VispaShortcuts, ExtensionShortcuts
from vispa import browser
import vispa
import logging
......@@ -26,9 +27,9 @@ class AjaxController(AbstractController):
@cherrypy.expose
@cherrypy.tools.user(on=False)
@cherrypy.tools.workspace(on=False)
def gettemplate(self, path, extension_name=None, **kwargs):
def gettemplate(self, path, **kwargs):
try:
makotemplate = cherrypy.engine.publish("lookup_template", path, extension_name).pop()
makotemplate = cherrypy.engine.publish("lookup_template", path).pop()
return makotemplate.render(**kwargs)
except Exception, e:
return fail(msg=str(e), encode_json=True)
......@@ -114,6 +115,32 @@ class AjaxController(AbstractController):
except Exception, e:
return fail(msg=str(e), encode_json=True)
@cherrypy.expose
@cherrypy.tools.workspace(on=False)
def setvispashortcuts(self, name, value=u"{}"):
try:
db = cherrypy.request.db
uid = self.get("user_id")
success = VispaShortcuts.set_value(db, uid, name, value)
if not success:
raise Exception("Couldn't update shortcuts section '%s'!" % section)
return success(encode_json=True)
except Exception, e:
return fail(msg=str(e), encode_json=True)
@cherrypy.expose
@cherrypy.tools.workspace(on=False)
def setextensionshortcuts(self, name, value=u"{}"):
try:
db = cherrypy.request.db
uid = self.get("user_id")
success = ExtensionShortcuts.set_value(db, uid, name, value)
if not success:
raise Exception("Couldn't update shortcuts section '%s'!" % section)
return success(encode_json=True)
except Exception, e:
return fail(msg=str(e), encode_json=True)
@cherrypy.expose
@cherrypy.tools.user()
@cherrypy.tools.workspace(on=False)
......
......@@ -94,7 +94,7 @@ class RootController(AbstractController):
@cherrypy.expose
@cherrypy.tools.allow(methods=["GET"])
@cherrypy.tools.stats(page="index")
@cherrypy.tools.render(template="sites/index.html", subfolder="html")
@cherrypy.tools.render(template="html/sites/index.html")
def index2(self, *args, **kwargs):
db = cherrypy.request.db
username = cherrypy.request.user.name
......@@ -162,7 +162,7 @@ class RootController(AbstractController):
@cherrypy.tools.user(reverse=True, path="/")
@cherrypy.tools.allow(methods=["GET"])
@cherrypy.tools.stats(page="login")
@cherrypy.tools.render(template="sites/login.html", subfolder="html")
@cherrypy.tools.render(template="html/sites/login.html")
def login(self, *args, **kwargs):
path = self.requested_path()
welcome_text = vispa.config("web", "welcome_text", "")
......@@ -187,7 +187,7 @@ class RootController(AbstractController):
@cherrypy.expose
@cherrypy.tools.user(reverse=True)
@cherrypy.tools.render(template="activate.html")
@cherrypy.tools.render(template="html/sites/activate.html")
def activate(self, hash, *args, **kwargs):
user = User.activate(cherrypy.request.db, hash)
if not isinstance(user, User):
......@@ -197,7 +197,7 @@ class RootController(AbstractController):
@cherrypy.expose
@cherrypy.tools.user(reverse=True)
@cherrypy.tools.render(template="forgot.html", subfolder="html")
@cherrypy.tools.render(template="html/sites/forgot.html")
def forgot(self, hash, *args, **kwargs):
user = User.get_by_hash(cherrypy.request.db, hash)
if isinstance(user, User):
......@@ -209,7 +209,7 @@ class RootController(AbstractController):
@cherrypy.tools.workspace(on=False)
@cherrypy.tools.allow(methods=["GET"])
@cherrypy.tools.stats(page="workspace")
@cherrypy.tools.render(template="sites/workspace.html", subfolder="html")
@cherrypy.tools.render(template="html/sites/workspace.html")
def workspace(self, *args, **kwargs):
path = self.requested_path()
show_add_form = vispa.config("workspace", "allow_new_workspaces", True)
......
......@@ -76,14 +76,14 @@ var DemoView = vispa.ExtensionView.Center.extend({
// the exact content of the preferences depends on the defaults set for this
// view in the extension above (in this case: {backgroundColor: "blue"})
// depending on the way this instance is created, there might be additional
// arguments (starting from index 1)
init: function(preferences) {
// arguments (starting from index 2)
init: function(preferences, shortcuts) {
// call the super constructor with 2 parameters
// 1. the name of this view (characters, numbers, and underscores only)
// 2. the preference object
// after this call, the preferences are automatically available via
// this.prefs and this.preferences
this._super("somename", preferences);
this._super("somename", preferences, shortcuts);
// attributes like e.g. the id are unknown at this point as they are set
// right after the init function
......
......@@ -16,9 +16,9 @@ var DummyExtension = vispa.Extension.extend({
var DummyView = vispa.ExtensionView.Center.extend({
init: function(preferences, path) {
init: function(preferences, shortcuts) {
var self = this;
this._super("somename", preferences);
this._super("somename", preferences, shortcuts);
this.addMenuEntry("My ID", function() {
self.alert("My ID is: " + self.getId());
......
......@@ -31,10 +31,5 @@ class MakoPlugin(SimplePlugin):
def stop(self):
self.lookup = None
def lookup_template(self, name, subfolder=None, extension_name=None):
subfolder = subfolder or "mako"
if not extension_name:
tmpl = os.path.join("templates", subfolder, name)
else:
tmpl = os.path.join("extensions", extension_name, "templates", name)
return self.lookup.get_template(tmpl)
def lookup_template(self, name):
return self.lookup.get_template(name)
......@@ -207,8 +207,9 @@ class Server(object):
from ws4py.server.cherrypyserver import WebSocketPlugin # @UnresolvedImport
WebSocketPlugin(cherrypy.engine).subscribe()
mako_lookup_dir = os.path.join(os.path.dirname(__file__), "templates")
vispa.plugins.template.MakoPlugin(cherrypy.engine,
base_dir=os.path.dirname(__file__),
base_dir=mako_lookup_dir,
module_dir=self.cache_dir
).subscribe()
sa_identifier = vispa.config('database', 'sqlalchemy_url',
......
......@@ -11,8 +11,8 @@
* TODO: description of the extension manager
*/
var ExtensionManager = VispaModule.extend({
init: function(preferences, extensionPreferences) {
this._super("extensions", preferences);
init: function(preferences, shortcuts, extensionPreferences, extensionShortcuts) {
this._super("extensions", preferences, shortcuts);
// set default preferences
this.setDefaultPreferences({
......@@ -22,12 +22,15 @@ var ExtensionManager = VispaModule.extend({
value: true
}
});
// set default shortcuts
this.setDefaultShortcuts({});
// attributes
this.settings = {};
this.varData = {};
this.extensionPreferences = extensionPreferences;
this.extensionShortcuts = extensionShortcuts;
this.extensions = {};
this.fileHandlers = {};
......@@ -41,10 +44,10 @@ var ExtensionManager = VispaModule.extend({
},
start: function() {
this.setupPreferences();
this.applyPreferences();
this._super();
this.logger.info("started");
return this;
},
......@@ -710,11 +713,12 @@ var Extension = Emitter.extend({
* * `render`
*/
var ExtensionView = Emitter.extend({
init: function(name, preferences) {
init: function(name, preferences, shortcuts) {
this._super();
this._name = name;
this.prefs = this.preferences = preferences;
this._shortcuts = shortcuts;
this._extension = null;
this._id = null;
......@@ -852,7 +856,8 @@ var ExtensionView = Emitter.extend({
self.alert(res.msg);
};
$.extend(true, data, {
_wid: this.getWorkspaceId()
_wid: this.getWorkspaceId(),
_viewId: this.getId()
});
return $[method](this.dynamic(url), data, callback);
},
......
// ExtensionView Base class for "center" window instances
var views = extensionClasses.ExtensionView;
views.Center = views.Base.extend({
init: function(name, preferences) {
init: function(name, preferences, shortcuts) {
var self = this;
this._super(name, preferences);
this._super(name, preferences, shortcuts);
this._menuEntries = {};
this._icon = null;
......
var Vispa = Emitter.extend({
init: function(preferences, workspaceData, extensionPreferences, shortcuts) {
var self = this;
this._super();
......@@ -21,6 +20,8 @@ var Vispa = Emitter.extend({
this.workspaceData = workspaceData;
this.extensionPreferences = extensionPreferences;
this.extensionShortcuts = shortcuts.extensionShortcuts;
this.shortcuts = shortcuts.vispaShortcuts;
// template store
this.templates = {};
......@@ -30,10 +31,11 @@ var Vispa = Emitter.extend({
this.logger.info("started");
this.url = new UrlHandler(this.prefs.url);
this.preferences = new PreferenceManager(this.prefs.preferences);
this.messenger = new Messenger(this.prefs.messenger);
this.workspaces = new WorkspaceManager(this.prefs.workspaces, this.workspaceData);
this.extensions = new ExtensionManager(this.prefs.extensions, this.extensionPreferences);
this.preferences = new PreferenceManager(this.prefs.preferences, this.shortcuts.preferences);
this.messenger = new Messenger(this.prefs.messenger, this.shortcuts.messenger);
this.workspaces = new WorkspaceManager(this.prefs.workspaces, this.shortcuts.workspaces, this.workspaceData);
this.extensions = new ExtensionManager(this.prefs.extensions, this.shortcuts.extensions, this.extensionPreferences,
this.extensionShortcuts);
this.setupGUI();
......@@ -211,17 +213,21 @@ var Vispa = Emitter.extend({
var VispaModule = Emitter.extend({
init: function(name, preferences) {
init: function(name, preferences, shortcuts) {
this._super();
this.name = name;
this.originalPreferences = preferences;
this.originalShortcuts = shortcuts;
this.prefs = {};
this.preferences = {};
this.defaultPreferences = {};
this.shortcuts = {};
this.defaultShortcuts = {};
this.logger = $.Logger("vispa." + name);
this.shortcuts = $.Shortcuts("vispa." + name).enable();
this.shortcutGroup = $.Shortcuts("vispa." + name).enable();
},
setDefaultPreferences: function(defaultPreferences) {
......@@ -230,11 +236,32 @@ var VispaModule = Emitter.extend({
return this;
},
setDefaultShortcuts: function(defaultShortcuts) {
if (defaultShortcuts)
this.defaultShortcuts = defaultShortcuts;
return this;
},
start: function() {
this.setupPreferences();
this.setupShortcuts();
this.applyPreferences();
this.applyShortcuts();
return this;
},
applyPreferences: function() {
return this;
},
applyShortcuts: function() {
return this;
},
setupPreferences: function() {
var prefs = vispa.parsePreferences(this.originalPreferences, this.defaultPreferences);
this.prefs = prefs;
var preferences = vispa.parsePreferences(this.originalPreferences, this.defaultPreferences);
this.preferences = preferences;
// add a preference observable
// test preferences
var settings = {
type: "set",
submitAction: function(section, name, preferences) {
......@@ -249,20 +276,41 @@ var VispaModule = Emitter.extend({
},
applyCallback: this.applyPreferences.bind(this)
};
vispa.preferences.addObservable("vispa", this.name, prefs, this.defaultPreferences, settings);
return prefs;
vispa.preferences.addObservable("vispaPreference", this.name, preferences,
this.defaultPreferences, settings);
return preferences;
},
start: function() {
return this;
setupShortcuts: function() {
var shortcuts = vispa.parsePreferences(this.originalShortcuts, this.defaultShortcuts);
this.shortcuts = shortcuts;
// add a preference observable
var settings = {
type: "set",
submitAction: function(section, name, shortcuts) {
return $.ajax({
url: vispa.url.dynamic("ajax/setvispashortcuts"),
type: "POST",
data: {
name: name,
value: JSON.stringify(shortcuts)
}
});
},
applyCallback: this.applyShortcuts.bind(this)
};
vispa.preferences.addObservable("vispaShortcuts", this.name, shortcuts, this.defaultShortcuts,
settings);
return shortcuts;
},
applyPreferences: function() {
pushPreferences: function(preferences, callback) {
vispa.preferences.push("vispaPreference", this.name, preferences, callback);
return this;
},
pushPreferences: function(preferences, callback) {
vispa.preferences.push("vispa", this.name, preferences, callback);
pushShortcuts: function(shortcuts, callback) {
vispa.preferences.push("vispaShortcuts", this.name, preferences, callback);
return this;
}
});
\ No newline at end of file
var Messenger = VispaModule.extend({
init: function(preferences) {
this._super("messenger", preferences);
init: function(preferences, shortcuts) {
this._super("messenger", preferences, shortcuts);
// set default preferences
this.setDefaultPreferences({
......@@ -23,6 +23,8 @@ var Messenger = VispaModule.extend({
// value: "info"
// }
});
// set default shortcuts
this.setDefaultShortcuts({});
this.settings = {
defaultContext: "global",
......@@ -51,8 +53,7 @@ var Messenger = VispaModule.extend({
},
start: function() {
this.setupPreferences();
this.applyPreferences();
this._super();
this.logger.info("started");
......@@ -68,6 +69,21 @@ var Messenger = VispaModule.extend({
return this;
},
applyShortcuts: function() {
// disable and empty all shortcuts
this.shortcutGroup.disable().empty();
// test
this.shortcutGroup.add(this.shortcuts.test, this.test.bind(this));
// enable again
this.shortcutGroup.enable();
},
test: function() {
console.log("test called");
},
addContext: function(context, fetch) {
if (!~this.varData.contexts.indexOf(context))
this.varData.contexts.push(context);
......
......@@ -28,12 +28,14 @@ var defaultSettings = {
};
var PreferenceManager = VispaModule.extend({
init: function(preferences) {
init: function(preferences, shortcuts) {
var self = this;
this._super("preferences", preferences);
this._super("preferences", preferences, shortcuts);
// set default preferences
this.setDefaultPreferences({});
// set default shortcuts
this.setDefaultShortcuts({});
// attributes
this.observables = {};
......@@ -42,10 +44,10 @@ var PreferenceManager = VispaModule.extend({
},
start: function() {
this.setupPreferences();
this.applyPreferences();
this._super();
this.logger.info("started");
return this;
},
......
var WorkspaceManager = VispaModule.extend({
init: function(preferences, workspaceData) {
init: function(preferences, shortcuts, workspaceData) {
var self = this;
this._super("workspaces", preferences);
this._super("workspaces", preferences, shortcuts);
// set default preferences
this.setDefaultPreferences({
......@@ -17,6 +17,8 @@ var WorkspaceManager = VispaModule.extend({
// value: 2
// }
});
// set default shortcuts
this.setDefaultShortcuts({});
// attributes
this.varData = {
......@@ -34,13 +36,11 @@ var WorkspaceManager = VispaModule.extend({
start: function() {
var self = this;
this.setupPreferences();
this._super();
// fetch templates before setting up workspaces
var selectedId = parseInt($.cookie("currentWorkspaceId"));
vispa.getTemplate("static/html/index/menuentry.html", function(err,
tmpl) {
vispa.getTemplate("static/html/index/menuentry.html", function(err, tmpl) {
// setup workspaces in a special order to maintain
// the order of menu entries
var sortedIds = Object.keys(self.workspaceData.data).sort();
......
......@@ -9,11 +9,11 @@ class MakoTool(cherrypy.Tool):
def __init__(self):
cherrypy.Tool.__init__(self, "before_handler", self._render, priority=100)
def _render(self, template=None, subfolder=None, extension_name=None, common_data={}):
def _render(self, template=None, common_data={}):
request = cherrypy.serving.request
# get template
makotemplate = cherrypy.engine.publish("lookup_template", template, subfolder, extension_name).pop()
makotemplate = cherrypy.engine.publish("lookup_template", template).pop()
if not makotemplate:
return exceptions.html_error_template().render(error="Template not present")
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment