Little js fixes.
[redakcja.git] / project / static / js / app.js
1 /*global Class*/
2 var editor;
3 var panel_hooks;
4
5
6 // prevent a console.log from blowing things up if we are on a browser that
7 // does not support it
8 if (typeof console === 'undefined') {
9   window.console = {} ;
10   console.log = console.info = console.warn = console.error = function(){};
11 }
12
13
14 (function(){
15   // Classes
16   var initializing = false, fnTest = /xyz/.test(function(){xyz;}) ? /\b_super\b/ : /.*/;
17   this.Class = function(){};
18   Class.extend = function(prop) {
19     var _super = this.prototype;
20     initializing = true;
21     var prototype = new this();
22     initializing = false;
23     for (var name in prop) {
24       prototype[name] = typeof prop[name] == "function" &&
25         typeof _super[name] == "function" && fnTest.test(prop[name]) ?
26         (function(name, fn){
27           return function() {
28             var tmp = this._super;
29             this._super = _super[name];
30             var ret = fn.apply(this, arguments);       
31             this._super = tmp;           
32             return ret;
33           };
34         })(name, prop[name]) :
35         prop[name];
36     }   
37     function Class() {
38       if ( !initializing && this.init )
39         this.init.apply(this, arguments);
40     }
41     Class.prototype = prototype;
42     Class.constructor = Class;
43     Class.extend = arguments.callee;   
44     return Class;
45   };
46   
47   // Templates
48   var cache = {};
49
50   this.render_template = function render_template(str, data){
51     // Figure out if we're getting a template, or if we need to
52     // load the template - and be sure to cache the result.
53     var fn = !/^[\d\s-_]/.test(str) ?
54       cache[str] = cache[str] ||
55         render_template(document.getElementById(str).innerHTML) :
56
57       // Generate a reusable function that will serve as a template
58       // generator (and which will be cached).
59       new Function("obj",
60         "var p=[],print=function(){p.push.apply(p,arguments);};" +
61
62         // Introduce the data as local variables using with(){}
63         "with(obj){p.push('" +
64
65         // Convert the template into pure JavaScript
66         str
67           .replace(/[\r\t\n]/g, " ")
68           .split("<%").join("\t")
69           .replace(/((^|%>)[^\t]*)'/g, "$1\r")
70           .replace(/\t=(.*?)%>/g, "',$1,'")
71           .split("\t").join("');")
72           .split("%>").join("p.push('")
73           .split("\r").join("\\'")
74       + "');}return p.join('');");
75
76       // Provide some basic currying to the user
77     return data ? fn( data ) : fn;
78   };
79 })();
80
81
82 (function() {
83   var slice = Array.prototype.slice;
84   
85   function update(array, args) {
86     var arrayLength = array.length, length = args.length;
87     while (length--) array[arrayLength + length] = args[length];
88     return array;
89   };
90   
91   function merge(array, args) {
92     array = slice.call(array, 0);
93     return update(array, args);
94   };
95   
96   Function.prototype.bind = function(context) {
97     if (arguments.length < 2 && typeof arguments[0] === 'undefined') {
98       return this;
99     } 
100     var __method = this;
101     var args = slice.call(arguments, 1);
102     return function() {
103       var a = merge(args, arguments);
104       return __method.apply(context, a);
105     }
106   }
107   
108 })();
109
110
111 var Editor = Editor || {};
112
113 // Obiekt implementujÄ…cy wzorzec KVC/KVO
114 Editor.Object = Class.extend({
115   _className: 'Editor.Object',
116   _observers: {},
117   _guid: null,
118   
119   init: function() {
120     this._observers = {};
121   },
122   
123   description: function() {
124     return this._className + '(guid = ' + this.guid() + ')';
125   },
126   
127   addObserver: function(observer, property, callback) {
128     // console.log('Add observer', observer.description(), 'to', this.description(), '[', property, ']');
129     if (!this._observers[property]) {
130       this._observers[property] = {}
131     }
132     this._observers[property][observer.guid()] = callback;
133     return this;
134   },
135   
136   removeObserver: function(observer, property) {
137     if (!property) {
138       for (var property in this._observers) {
139         this.removeObserver(observer, property)
140       }
141     } else {
142       // console.log('Remove observer', observer.description(), 'from', this.description(), '[', property, ']');
143       delete this._observers[property][observer.guid()];
144     }
145     return this;
146   },
147   
148   notifyObservers: function(property) {
149     var currentValue = this[property];
150     for (var guid in this._observers[property]) {
151       // console.log(this._observers[property][guid]);
152       // console.log('Notifying', guid, 'of', this.description(), '[', property, ']');
153       this._observers[property][guid](property, currentValue, this);
154     }
155     return this;
156   },
157   
158   guid: function() {
159     if (!this._guid) {
160       this._guid = ('editor-' + Editor.Object._lastGuid++);
161     }
162     return this._guid;
163   },
164   
165   get: function(property) {
166     return this[property];
167   },
168   
169   set: function(property, value) {
170     if (this[property] != value) {
171       this[property] = value;
172       this.notifyObservers(property);
173     }
174     return this;
175   },
176   
177   dispose: function() {
178     delete this._observers;
179   }
180 });
181
182 Editor.Object._lastGuid = 0;
183
184 var panels = [];