6 // prevent a console.log from blowing things up if we are on a browser that
8 if (typeof console === 'undefined') {
10 console.log = console.info = console.warn = console.error = function(){};
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;
21 var prototype = new this();
23 for (var name in prop) {
24 prototype[name] = typeof prop[name] == "function" &&
25 typeof _super[name] == "function" && fnTest.test(prop[name]) ?
28 var tmp = this._super;
29 this._super = _super[name];
30 var ret = fn.apply(this, arguments);
34 })(name, prop[name]) :
38 if ( !initializing && this.init )
39 this.init.apply(this, arguments);
41 Class.prototype = prototype;
42 Class.constructor = Class;
43 Class.extend = arguments.callee;
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) :
57 // Generate a reusable function that will serve as a template
58 // generator (and which will be cached).
61 "var p=[],print=function(){p.push.apply(p,arguments);};" +
63 // Introduce the data as local variables using with(){}
64 "with(obj){p.push('" +
66 // Convert the template into pure JavaScript
68 .replace(/[\r\t\n]/g, " ")
69 .split("<%").join("\t")
70 .replace(/((^|%>)[^\t]*)'/g, "$1\r")
71 .replace(/\t=(.*?)%>/g, "',$1,'")
72 .split("\t").join("');")
73 .split("%>").join("p.push('")
74 .split("\r").join("\\'")
75 + "');}return p.join('');");
77 // Provide some basic currying to the user
78 return data ? fn( data ) : fn;
84 var slice = Array.prototype.slice;
86 function update(array, args) {
87 var arrayLength = array.length, length = args.length;
88 while (length--) array[arrayLength + length] = args[length];
92 function merge(array, args) {
93 array = slice.call(array, 0);
94 return update(array, args);
97 Function.prototype.bind = function(context) {
98 if (arguments.length < 2 && typeof arguments[0] === 'undefined') {
102 var args = slice.call(arguments, 1);
104 var a = merge(args, arguments);
105 return __method.apply(context, a);
112 var Editor = Editor || {};
114 // Obiekt implementujący wzorzec KVC/KVO
115 Editor.Object = Class.extend({
116 _className: 'Editor.Object',
121 this._observers = {};
124 description: function() {
125 return this._className + '(guid = ' + this.guid() + ')';
128 addObserver: function(observer, property, callback) {
129 // console.log('Add observer', observer.description(), 'to', this.description(), '[', property, ']');
130 if (!this._observers[property]) {
131 this._observers[property] = {}
133 this._observers[property][observer.guid()] = callback;
137 removeObserver: function(observer, property) {
139 for (var property in this._observers) {
140 this.removeObserver(observer, property)
143 // console.log('Remove observer', observer.description(), 'from', this.description(), '[', property, ']');
144 delete this._observers[property][observer.guid()];
149 notifyObservers: function(property) {
150 var currentValue = this[property];
151 for (var guid in this._observers[property]) {
152 // console.log(this._observers[property][guid]);
153 // console.log('Notifying', guid, 'of', this.description(), '[', property, ']');
154 this._observers[property][guid](property, currentValue, this);
161 this._guid = ('editor-' + Editor.Object._lastGuid++);
166 get: function(property) {
167 return this[property];
170 set: function(property, value) {
171 if (this[property] != value) {
172 this[property] = value;
173 this.notifyObservers(property);
178 dispose: function() {
179 delete this._observers;
183 // Handle JSON error responses in uniform way
184 function parseXHRError(response)
188 var json = $.evalJSON(response.responseText);
190 if(json.reason == 'xml-parse-error') {
191 message = json_response.message.replace(/(line\s+)(\d+)(\s+)/i,
192 "<a class='xml-editor-ref' href='#xml-$2-1'>$1$2$3</a>");
194 message = message.replace(/(line\s+)(\d+)(\,\s*column\s+)(\d+)/i,
195 "<a class='xml-editor-ref' href='#xml-$2-$4'>$1$2$3$4</a>");
197 message = json_response.message || json_response.reason || "Nieznany błąd :((";
199 // not a valid JSON response
200 message = response.statusText;
205 Editor.Object._lastGuid = 0;