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