3 var noop = function() { };
10 $.wiki.activePerspective = function() {
11 return this.perspectives[$("#tabs li.active").attr('id')];
14 $.wiki.exitContext = function() {
15 var ap = this.activePerspective();
20 $.wiki.enterContext = function(ap) {
24 $.wiki.isDirty = function() {
25 var ap = this.activePerspective();
26 return (!!CurrentDocument && CurrentDocument.has_local_changes) || ap.dirty();
29 $.wiki.newTab = function(doc, title, klass) {
30 var base_id = 'id' + Math.floor(Math.random()* 5000000000);
31 var id = (''+klass)+'_' + base_id;
32 var $tab = $('<li id="'+id+'" data-ui-related="'+base_id+'" data-ui-jsclass="'+klass+'" >'
33 + title + '<img src="/static/icons/close.png" class="tabclose"></li>');
34 var $view = $('<div class="editor '+klass+'" id="'+base_id+'"> </div>');
36 this.perspectives[id] = new $.wiki[klass]({
42 $('#tabs').append($tab);
43 $view.hide().appendTo('#editor');
50 $.wiki.initTab = function(options) {
51 var klass = $(options.tab).attr('data-ui-jsclass');
53 return new $.wiki[klass]({
55 id: $(options.tab).attr('id'),
56 callback: function() {
57 $.wiki.perspectives[this.perspective_id] = this;
59 options.callback.call(this);
64 $.wiki.perspectiveForTab = function(tab) { // element or id
65 return this.perspectives[ $(tab).attr('id')];
68 $.wiki.switchToTab = function(tab){
73 $tab = $(DEFAULT_PERSPECTIVE);
75 var $old = $('#tabs li').filter('.active');
78 $(this).removeClass('active');
79 $('#' + $(this).attr('data-ui-related')).hide();
80 self.perspectives[$(this).attr('id')].onExit();
84 $tab.addClass('active');
85 $('#' + $tab.attr('data-ui-related')).show();
88 console.log($.wiki.perspectives);
90 $.wiki.perspectives[$tab.attr('id')].onEnter();
96 $.wiki.Perspective = function(options) {
99 this.doc = options.doc;
101 this.perspective_id = options.id;
104 this.perspective_id = '';
108 options.callback.call(this);
111 $.wiki.Perspective.prototype.toString = function() {
112 return this.perspective_id;
115 $.wiki.Perspective.prototype.dirty = function() {
119 $.wiki.Perspective.prototype.onEnter = function () {
120 // called when perspective in initialized
121 if (this.perspective_id) {
122 document.location.hash = '#' + this.perspective_id;
125 console.log(document.location.hash);
128 $.wiki.Perspective.prototype.onExit = function () {
129 // called when user switches to another perspective
130 document.location.hash = '';
133 $.wiki.Perspective.prototype.destroy = function() {
137 $.wiki.Perspective.prototype.freezeState = function () {
138 // free UI state (don't store data here)
141 $.wiki.Perspective.prototype.unfreezeState = function (frozenState) {
146 * Stub rendering (used in generating history)
148 $.wiki.renderStub = function($container, $stub, data)
150 var $elem = $stub.clone();
151 $elem.removeClass('row-stub');
152 $container.append($elem);
154 $('*[data-stub-value]', $elem).each(function() {
156 var field = $this.attr('data-stub-value');
157 var value = data[field];
159 if(value === null || value === undefined) return;
161 if(!$this.attr('data-stub-target')) {
165 $this.attr($this.attr('data-stub-target'), value);
166 $this.removeAttr('data-stub-target');
167 $this.removeAttr('data-stub-value');
178 function GenericDialog(element) {
183 self.$elem = $(element);
185 if(!self.$elem.attr('data-ui-initialized')) {
186 console.log("Initializing dialog", this);
188 self.$elem.attr('data-ui-initialized', true);
194 GenericDialog.prototype = {
197 * Steps to follow when the dialog in first loaded on page.
199 initialize: function(){
203 $('button[data-ui-action]', self.$elem).click(function(event) {
204 event.preventDefault();
206 var action = $(this).attr('data-ui-action');
207 console.log("Button pressed, action: ", action);
210 self[action + "Action"].call(self);
212 console.log("Action failed:", e);
213 // always hide on cancel
214 if(action == 'cancel')
221 * Prepare dialog for user. Clear any unnessary data.
238 cancelAction: function() {
242 doneAction: function() {
246 clearForm: function() {
247 $("*[data-ui-error-for]", this.$elem).text('');
250 reportErrors: function(errors) {
251 var global = $("*[data-ui-error-for='__all__']", this.$elem);
254 for (var field_name in errors)
256 var span = $("*[data-ui-error-for='"+field_name+"']", this.$elem);
259 unassigned.push(field_name);
263 span.text(errors[field_name].join(' '));
266 if(unassigned.length > 0)
267 global.text( global.text() + 'W formularzu wystąpiły błędy');
271 $.wiki.cls.GenericDialog = GenericDialog;
273 $.wiki.showDialog = function(selector) {
274 var elem = $(selector);
276 if(elem.length != 1) {
277 console.log("Failed to show dialog:", selector, elem);
282 var klass = elem.attr('data-ui-jsclass')
283 return new $.wiki.cls[klass](elem);
285 console.log("Failed to show dialog", selector, klass, e);