1 /*global View render_template panels */
2 var HTMLView = View.extend({
3 _className: 'HTMLView',
6 template: 'html-view-template',
8 init: function(element, model, parent, template) {
9 var submodel = model.contentModels['html'];
10 this._super(element, submodel, template);
14 .addObserver(this, 'data', this.modelDataChanged.bind(this))
15 .addObserver(this, 'state', this.modelStateChanged.bind(this));
17 this.modelStateChanged('state', this.model.get('state'));
18 this.modelDataChanged('data', this.model.get('data'));
22 this.currentOpen = null;
23 this.currentFocused = null;
27 modelDataChanged: function(property, value)
31 // the xml model changed
32 var container = $('.htmlview', this.element);
34 container.append(value);
36 this.updatePrintLink();
39 /* $(".theme-ref", this.$docbase).each(function() {
40 var id = $(this).attr('x-theme-class');
42 var end = $("span.theme-end[x-theme-class = " + id+"]");
43 var begin = $("span.theme-begin[x-theme-class = " + id+"]");
45 var h = $(this).outerHeight();
47 h = Math.max(h, end.offset().top - begin.offset().top);
48 $(this).css('height', h);
52 updatePrintLink: function() {
53 var base = this.$printLink.attr('ui:baseref');
54 this.$printLink.attr('href', base + "?user="+this.model.document.get('user')+"&revision=" + this.model.get('revision'));
57 modelStateChanged: function(property, value)
61 if (value == 'synced' || value == 'dirty') {
63 } else if (value == 'unsynced') {
64 if(this.currentOpen) this.closeWithoutSave(this.currentOpen);
65 this.freeze('Niezsynchronizowany...');
66 } else if (value == 'loading') {
67 this.freeze('Ładowanie...');
68 } else if (value == 'saving') {
69 this.freeze('Zapisywanie...');
70 } else if (value == 'error') {
71 this.freeze(this.model.get('error'));
72 $('.xml-editor-ref', this.overlay).click(
74 console.log("Sending scroll rq.", this);
76 var href = $(this).attr('href').split('-');
77 var line = parseInt(href[1]);
78 var column = parseInt(href[2]);
80 $(document).trigger('xml-scroll-request', {
95 this.$docbase.unbind('click');
98 this.$printLink.unbind();
100 if(this.$addThemeButton)
101 this.$addThemeButton.unbind();
105 this.$printLink = $('.htmlview-toolbar .html-print-link', this.element);
106 this.$docbase = $('.htmlview', this.element);
107 this.$addThemeButton = $('.htmlview-toolbar .html-add-motive', this.element);
109 this.$debugButton = $('.htmlview-toolbar .html-serialize', this.element);
111 this.updatePrintLink();
112 this.$docbase.bind('click', this.itemClicked.bind(this));
113 this.$addThemeButton.click( this.addTheme.bind(this) );
114 this.$debugButton.click( this.serialized.bind(this) );
117 serialized: function() {
118 this.model.set('state', 'dirty');
119 console.log( this.model.serializer.serializeToString(this.model.get('data')) );
122 renderPart: function($e, html) {
123 // exceptions aren't good, but I don't have a better idea right now
124 if($e.attr('x-annotation-box')) {
125 // replace the whole annotation
126 var $p = $e.parent();
128 var $box = $('*[x-annotation-box]', $p);
129 $box.append( this.$menuTemplate.clone() );
131 if(this.currentFocused && $p[0] == this.currentFocused[0])
133 this.currentFocused = $p;
143 $e.append( this.$menuTemplate.clone() );
147 this.model.load(true);
150 dispose: function() {
151 this.model.removeObserver(this);
155 itemClicked: function(event)
159 console.log('click:', event, event.ctrlKey, event.target);
160 var $e = $(event.target);
162 if($e.hasClass('annotation'))
164 if(this.currentOpen) return false;
166 var $p = $e.parent();
167 if(this.currentFocused)
169 console.log(this.currentFocused, $p);
170 if($p[0] == this.currentFocused[0]) {
171 console.log('unfocus of current');
172 this.unfocusAnnotation();
176 console.log('switch unfocus');
177 this.unfocusAnnotation();
180 this.focusAnnotation($p);
185 * Clicking outside of focused area doesn't unfocus by default
186 * - this greatly simplifies the whole click check
189 if( $e.hasClass('theme-ref') )
192 this.selectTheme($e.attr('x-theme-class'));
198 if($e.hasClass('edit-button'))
199 this.openForEdit( this.editableFor($e) );
201 if($e.hasClass('accept-button'))
202 this.closeWithSave( this.editableFor($e) );
204 if($e.hasClass('reject-button'))
205 this.closeWithoutSave( this.editableFor($e) );
207 messageCenter.addMessage('error', "wlsave", 'Błąd:' + e.text);
213 unfocusAnnotation: function()
215 if(!this.currentFocused)
217 console.log('Redundant unfocus');
222 && this.currentOpen.is("*[x-annotation-box]")
223 && this.currentOpen.parent()[0] == this.currentFocused[0])
225 console.log("Can't unfocus open box");
229 var $box = $("*[x-annotation-box]", this.currentFocused);
233 // this.currentFocused.removeAttr('x-focused');
234 // this.currentFocused.hide();
235 this.currentFocused = null;
238 focusAnnotation: function($e) {
239 this.currentFocused = $e;
240 var $box = $("*[x-annotation-box]", $e);
245 // $e.attr('x-focused', 'focused');
248 closeWithSave: function($e) {
249 var $edit = $e.data('edit-overlay');
250 var newText = $('textarea', $edit).val();
252 this.model.updateWithWLML($e, newText);
254 this.currentOpen = null;
257 closeWithoutSave: function($e) {
258 var $edit = $e.data('edit-overlay');
260 $e.removeAttr('x-open');
261 this.currentOpen = null;
264 editableFor: function($button)
269 while( ($e[0] != this.element[0]) && !($e.attr('x-editable')) && n < 50)
271 // console.log($e, $e.parent(), this.element);
276 if(!$e.attr('x-editable'))
277 throw Exception("Click outside of editable")
279 console.log("Trigger", $button, " yields editable: ", $e);
283 openForEdit: function($origin)
285 if(this.currentOpen && this.currentOpen != $origin) {
286 this.closeWithSave(this.currentOpen);
291 // annotations overlay their sub box - not their own box //
292 if($origin.is(".annotation-inline-box"))
293 $box = $("*[x-annotation-box]", $origin);
297 var x = $box[0].offsetLeft;
298 var y = $box[0].offsetTop;
299 var w = $box.outerWidth();
300 var h = $box.innerHeight();
302 console.log("Edit origin:", $origin, " box:", $box);
303 console.log("offsetParent:", $box[0].offsetParent);
304 console.log("Dimensions: ", x, y, w , h);
306 // start edition on this node
307 var $overlay = $('<div class="html-editarea"><textarea></textarea></div>');
310 position: 'absolute',
318 $('textarea', $overlay).val( this.model.asWLML($origin[0]) );
320 if($origin.is(".annotation-inline-box"))
322 if(this.currentFocused) {
323 // if some other is focused
324 if($origin[0] != this.currentFocused[0]) {
325 this.unfocusAnnotation();
326 this.focusAnnotation($origin);
330 else { // nothing was focused
331 this.focusAnnotation($origin);
334 else { // this item is not focusable
335 if(this.currentFocused) this.unfocusAnnotation();
338 $($box[0].offsetParent).append($overlay);
339 $origin.data('edit-overlay', $overlay);
341 this.currentOpen = $origin;
342 $origin.attr('x-open', 'open');
345 console.log("Can't open", e);
353 var selection = window.getSelection();
354 var n = selection.rangeCount;
356 console.log("Range count:", n);
359 window.alert("Nie zaznaczono żadnego obszaru");
361 // for now allow only 1 range
363 window.alert("Zaznacz jeden obszar");
366 // from this point, we will assume that the ranges are disjoint
367 for(var i=0; i < n; i++)
369 var range = selection.getRangeAt(i);
370 console.log(i, range.startContainer, range.endContainer);
371 var date = (new Date()).getTime();
372 var random = Math.floor(4000000000*Math.random());
373 var id = (''+date) + '-' + (''+random);
375 var spoint = document.createRange();
376 var epoint = document.createRange();
378 spoint.setStart(range.startContainer, range.startOffset);
379 epoint.setStart(range.endContainer, range.endOffset);
383 var elem = $('<span x-editable="true" x-node="motyw" class="motyw">Nowy motyw</span>');
384 elem.attr('x-attr-qname-'+id, 'id');
385 elem.attr('x-attr-value-'+id, 'm'+id);
386 spoint.insertNode(elem[0]);
388 // insert theme-begin
389 elem = $('<span x-node="begin" class="begin"></span>');
390 elem.attr('x-attr-qname-'+id, 'id');
391 elem.attr('x-attr-value-'+id, 'b'+id);
392 spoint.insertNode(elem[0]);
394 elem = $('<span x-node="end" class="end"></span>');
395 elem.attr('x-attr-qname-'+id, 'id');
396 elem.attr('x-attr-value-'+id, 'e'+id);
397 epoint.insertNode(elem[0]);
400 //selection.removeAllRanges();
403 selectTheme: function(themeId)
405 var selection = document.getSelection();
407 // remove current selection
408 selection.removeAllRanges();
410 var range = document.createRange();
411 var s = $('#m'+themeId)[0];
412 var e = $('#e'+themeId)[0];
413 console.log('Selecting range:', themeId, range, s, e);
416 range.setStartAfter(s);
417 range.setEndBefore(e);
418 selection.addRange(range);
424 panels['html'] = HTMLView;