+(function($)
+{
+ var noop = function() { };
+
+ $.wiki = {
+ perspectives: {},
+ cls: {},
+ state: {
+ "version": 1,
+ "perspectives": {
+ "ScanGalleryPerspective": {
+ "show": true,
+ "page": 1
+ },
+ "CodeMirrorPerspective": {}
+ /*
+ "VisualPerspective": {},
+ "HistoryPerspective": {},
+ "SummaryPerspective": {}
+ */
+ }
+ }
+ };
+
+ $.wiki.loadConfig = function() {
+ if(!window.localStorage)
+ return;
+
+ try {
+ var value = window.localStorage.getItem(CurrentDocument.id) || "{}";
+ var config = JSON.parse(value);
+
+ if (config.version == $.wiki.state.version) {
+ $.wiki.state.perspectives = $.extend($.wiki.state.perspectives, config.perspectives);
+ }
+ } catch(e) {
+ console.log("Failed to load config, using default.");
+ }
+
+ console.log("Loaded:", $.wiki.state, $.wiki.state.version);
+ };
+
+ $(window).bind('unload', function() {
+ if(window.localStorage)
+ window.localStorage.setItem(CurrentDocument.id, JSON.stringify($.wiki.state));
+ })
+
+
+ $.wiki.activePerspective = function() {
+ return this.perspectives[$("#tabs li.active").attr('id')];
+ };
+
+ $.wiki.exitContext = function() {
+ var ap = this.activePerspective();
+ if(ap) ap.onExit();
+ return ap;
+ };
+
+ $.wiki.enterContext = function(ap) {
+ if(ap) ap.onEnter();
+ };
+
+ $.wiki.isDirty = function() {
+ var ap = this.activePerspective();
+ return (!!CurrentDocument && CurrentDocument.has_local_changes) || ap.dirty();
+ };
+
+ $.wiki.newTab = function(doc, title, klass) {
+ var base_id = 'id' + Math.floor(Math.random()* 5000000000);
+ var id = (''+klass)+'_' + base_id;
+ var $tab = $('<li id="'+id+'" data-ui-related="'+base_id+'" data-ui-jsclass="'+klass+'" >'
+ + title + '<img src="/static/icons/close.png" class="tabclose"></li>');
+ var $view = $('<div class="editor '+klass+'" id="'+base_id+'"> </div>');
+
+ this.perspectives[id] = new $.wiki[klass]({
+ doc: doc,
+ id: id,
+ base_id: base_id,
+ });
+
+ $('#tabs').append($tab);
+ $view.hide().appendTo('#editor');
+ return {
+ tab: $tab[0],
+ view: $view[0],
+ };
+ };
+
+ $.wiki.initTab = function(options) {
+ var klass = $(options.tab).attr('data-ui-jsclass');
+
+ return new $.wiki[klass]({
+ doc: options.doc,
+ id: $(options.tab).attr('id'),
+ callback: function() {
+ $.wiki.perspectives[this.perspective_id] = this;
+ if(options.callback)
+ options.callback.call(this);
+ }
+ });
+ };
+
+ $.wiki.perspectiveForTab = function(tab) { // element or id
+ return this.perspectives[ $(tab).attr('id')];