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(params)
150 params = $.extend({ 'filters': {} }, params);
151 var $elem = params.stub.clone();
152 $elem.removeClass('row-stub');
153 params.container.append($elem);
155 $('*[data-stub-value]', $elem).each(function() {
157 var field = $this.attr('data-stub-value');
159 var value = params.data[field];
161 if(params.filters[field])
162 value = params.filters[field](value);
164 if(value === null || value === undefined) return;
166 if(!$this.attr('data-stub-target')) {
170 $this.attr($this.attr('data-stub-target'), value);
171 $this.removeAttr('data-stub-target');
172 $this.removeAttr('data-stub-value');
183 function GenericDialog(element) {
188 self.$elem = $(element);
190 if(!self.$elem.attr('data-ui-initialized')) {
191 console.log("Initializing dialog", this);
193 self.$elem.attr('data-ui-initialized', true);
199 GenericDialog.prototype = {
202 * Steps to follow when the dialog in first loaded on page.
204 initialize: function(){
208 $('button[data-ui-action]', self.$elem).click(function(event) {
209 event.preventDefault();
211 var action = $(this).attr('data-ui-action');
212 console.log("Button pressed, action: ", action);
215 self[action + "Action"].call(self);
217 console.log("Action failed:", e);
218 // always hide on cancel
219 if(action == 'cancel')
226 * Prepare dialog for user. Clear any unnessary data.
243 cancelAction: function() {
247 doneAction: function() {
251 clearForm: function() {
252 $("*[data-ui-error-for]", this.$elem).text('');
255 reportErrors: function(errors) {
256 var global = $("*[data-ui-error-for='__all__']", this.$elem);
259 for (var field_name in errors)
261 var span = $("*[data-ui-error-for='"+field_name+"']", this.$elem);
264 unassigned.push(field_name);
268 span.text(errors[field_name].join(' '));
271 if(unassigned.length > 0)
272 global.text( global.text() + 'W formularzu wystąpiły błędy');
276 $.wiki.cls.GenericDialog = GenericDialog;
278 $.wiki.showDialog = function(selector, options) {
279 var elem = $(selector);
281 if(elem.length != 1) {
282 console.log("Failed to show dialog:", selector, elem);
287 var klass = elem.attr('data-ui-jsclass');
288 return new $.wiki.cls[klass](elem, options);
290 console.log("Failed to show dialog", selector, klass, e);