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);
108 // this.$debugButton = $('.htmlview-toolbar .html-serialize', this.element);
110 this.updatePrintLink();
111 this.$docbase.bind('click', this.itemClicked.bind(this));
112 this.$addThemeButton.click( this.addTheme.bind(this) );
113 // this.$debugButton.click( this.serialized.bind(this) );
116 /* serialized: function() {
117 this.model.set('state', 'dirty');
118 console.log( this.model.serializer.serializeToString(this.model.get('data')) );
121 renderPart: function($e, html) {
122 // exceptions aren't good, but I don't have a better idea right now
123 if($e.attr('x-annotation-box')) {
124 // replace the whole annotation
125 var $p = $e.parent();
127 var $box = $('*[x-annotation-box]', $p);
128 $box.append( this.$menuTemplate.clone() );
130 if(this.currentFocused && $p[0] == this.currentFocused[0])
132 this.currentFocused = $p;
142 $e.append( this.$menuTemplate.clone() );
146 this.model.load(true);
149 dispose: function() {
150 this.model.removeObserver(this);
154 itemClicked: function(event)
158 console.log('click:', event, event.ctrlKey, event.target);
159 var $e = $(event.target);
161 if($e.hasClass('annotation'))
163 if(this.currentOpen) return false;
165 var $p = $e.parent();
166 if(this.currentFocused)
168 console.log(this.currentFocused, $p);
169 if($p[0] == this.currentFocused[0]) {
170 console.log('unfocus of current');
171 this.unfocusAnnotation();
175 console.log('switch unfocus');
176 this.unfocusAnnotation();
179 this.focusAnnotation($p);
184 * Clicking outside of focused area doesn't unfocus by default
185 * - this greatly simplifies the whole click check
188 if( $e.hasClass('motyw') )
191 this.selectTheme($e.attr('theme-class'));
197 if($e.hasClass('delete-button'))
198 this.deleteElement( this.editableFor($e) );
200 if($e.hasClass('edit-button'))
201 this.openForEdit( this.editableFor($e) );
203 if($e.hasClass('accept-button'))
204 this.closeWithSave( this.editableFor($e) );
206 if($e.hasClass('reject-button'))
207 this.closeWithoutSave( this.editableFor($e) );
209 messageCenter.addMessage('error', "wlsave", 'Błąd:' + e.toString());
215 unfocusAnnotation: function()
217 if(!this.currentFocused)
219 console.log('Redundant unfocus');
224 && this.currentOpen.is("*[x-annotation-box]")
225 && this.currentOpen.parent()[0] == this.currentFocused[0])
227 console.log("Can't unfocus open box");
231 var $box = $("*[x-annotation-box]", this.currentFocused);
235 // this.currentFocused.removeAttr('x-focused');
236 // this.currentFocused.hide();
237 this.currentFocused = null;
240 focusAnnotation: function($e) {
241 this.currentFocused = $e;
242 var $box = $("*[x-annotation-box]", $e);
247 // $e.attr('x-focused', 'focused');
250 closeWithSave: function($e) {
251 var $edit = $e.data('edit-overlay');
252 var newText = $('textarea', $edit).val();
255 errors = this.model.updateInnerWithWLML($e, newText);
258 messageCenter.addMessage('error', 'render', errors);
261 this.currentOpen = null;
265 closeWithoutSave: function($e) {
266 var $edit = $e.data('edit-overlay');
268 $e.removeAttr('x-open');
269 this.currentOpen = null;
272 editableFor: function($button)
277 while( ($e[0] != this.element[0]) && !($e.attr('x-editable')) && n < 50)
279 // console.log($e, $e.parent(), this.element);
284 if(!$e.attr('x-editable'))
285 throw Exception("Click outside of editable")
287 console.log("Trigger", $button, " yields editable: ", $e);
291 openForEdit: function($origin)
293 if(this.currentOpen && this.currentOpen != $origin) {
294 this.closeWithSave(this.currentOpen);
299 // annotations overlay their sub box - not their own box //
300 if($origin.is(".annotation-inline-box"))
301 $box = $("*[x-annotation-box]", $origin);
305 var x = $box[0].offsetLeft;
306 var y = $box[0].offsetTop;
307 var w = $box.outerWidth();
308 var h = $box.innerHeight();
310 console.log("Edit origin:", $origin, " box:", $box);
311 console.log("offsetParent:", $box[0].offsetParent);
312 console.log("Dimensions: ", x, y, w , h);
314 // start edition on this node
315 var $overlay = $('<div class="html-editarea"><textarea></textarea></div>');
317 h = Math.max(h, 2*parseInt($box.css('line-height')));
320 position: 'absolute',
328 $('textarea', $overlay).val( this.model.innerAsWLML($origin[0]) );
330 if($origin.is(".annotation-inline-box"))
332 if(this.currentFocused) {
333 // if some other is focused
334 if($origin[0] != this.currentFocused[0]) {
335 this.unfocusAnnotation();
336 this.focusAnnotation($origin);
340 else { // nothing was focused
341 this.focusAnnotation($origin);
344 else { // this item is not focusable
345 if(this.currentFocused) this.unfocusAnnotation();
348 $($box[0].offsetParent).append($overlay);
349 $origin.data('edit-overlay', $overlay);
351 this.currentOpen = $origin;
352 $origin.attr('x-open', 'open');
355 console.log("Can't open", e);
361 deleteElement: function($editable)
363 var relatedThemes = $("*[x-node='begin'], *[x-node='end']", $editable);
365 var themeMarks = relatedThemes.map(function() {
366 return $(".motyw[theme-class='"+$(this).attr('theme-class')+"']");
369 if($editable.is("*.motyw"))
371 console.log($editable);
372 var selector = "[theme-class='"+$editable.attr('theme-class')+"']";
373 relatedThemes = relatedThemes.add("*[x-node='begin']"+selector+", *[x-node='end']"+selector);
376 console.log(relatedThemes, themeMarks);
378 var del = confirm("Usunięcie elementu jest nieodwracalne.\n"
379 +" Czy na pewno chcesz usunąć ten element, wraz z zawartymi motywami ?\n");
382 relatedThemes.remove();
388 // Theme related stuff
389 verifyThemeInsertPoint: function(node) {
391 if(node.nodeType == 3) { // Text Node
392 node = node.parentNode;
395 if(node.nodeType != 1) return false;
397 console.log('Selection point:', node);
400 var xtype = node.attr('x-node');
402 if(!xtype || (xtype.search(':') >= 0) ||
403 xtype == 'motyw' || xtype == 'begin' || xtype == 'end')
406 // this is hopefully redundant
407 //if(! node.is('*.utwor *') )
410 // don't allow themes inside annotations
411 if( node.is('*[x-annotation-box] *') )
419 var selection = window.getSelection();
420 var n = selection.rangeCount;
422 console.log("Range count:", n);
425 window.alert("Nie zaznaczono żadnego obszaru");
429 // for now allow only 1 range
431 window.alert("Zaznacz jeden obszar");
435 // from this point, we will assume that the ranges are disjoint
436 for(var i=0; i < n; i++)
438 var range = selection.getRangeAt(i);
439 console.log(i, range.startContainer, range.endContainer);
441 // verify if the start/end points make even sense -
442 // they must be inside a x-node (otherwise they will be discarded)
443 // and the x-node must be a main text
444 if(! this.verifyThemeInsertPoint(range.startContainer) ) {
445 window.alert("Motyw nie może się zaczynać w tym miejscu.");
449 if(! this.verifyThemeInsertPoint(range.endContainer) ) {
450 window.alert("Motyw nie może się kończyć w tym miejscu.");
455 var date = (new Date()).getTime();
456 var random = Math.floor(4000000000*Math.random());
457 var id = (''+date) + '-' + (''+random);
459 var spoint = document.createRange();
460 var epoint = document.createRange();
462 spoint.setStart(range.startContainer, range.startOffset);
463 epoint.setStart(range.endContainer, range.endOffset);
465 var mtag, btag, etag, errors;
468 mtag = $('<span></span>');
469 spoint.insertNode(mtag[0]);
470 errors = this.model.updateWithWLML(mtag, '<motyw id="m'+id+'">Nowy Motyw</motyw>');
472 messageCenter.addMessage('error', null, 'Błąd przy dodawaniu motywu :' + errors);
476 // insert theme-begin
477 btag = $('<span></span>');
478 spoint.insertNode(btag[0]);
479 errors = this.model.updateWithWLML(btag, '<begin id="b'+id+'" />');
482 messageCenter.addMessage('error', null, 'Błąd przy dodawaniu motywu :' + errors);
487 etag = $('<span></span>');
488 epoint.insertNode(etag[0]);
489 result = this.model.updateWithWLML(etag, '<end id="e'+id+'" />');
493 messageCenter.addMessage('error', null, 'Błąd przy dodawaniu motywu :' + errors);
498 selection.removeAllRanges();
501 selectTheme: function(themeId)
503 var selection = window.getSelection();
505 // remove current selection
506 selection.removeAllRanges();
508 var range = document.createRange();
509 var s = $(".motyw[theme-class='"+themeId+"']")[0];
510 var e = $(".end[theme-class='"+themeId+"']")[0];
511 console.log('Selecting range:', themeId, range, s, e);
514 range.setStartAfter(s);
515 range.setEndBefore(e);
516 selection.addRange(range);
522 panels['html'] = HTMLView;