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.updatePrintLink();
110 this.$docbase.bind('click', this.itemClicked.bind(this));
111 this.$addThemeButton.click( this.addTheme.bind(this) );
114 renderPart: function($e, html) {
115 // exceptions aren't good, but I don't have a better idea right now
116 if($e.attr('x-annotation-box')) {
117 // replace the whole annotation
118 var $p = $e.parent();
120 var $box = $('*[x-annotation-box]', $p);
121 $box.append( this.$menuTemplate.clone() );
123 if(this.currentFocused && $p[0] == this.currentFocused[0])
125 this.currentFocused = $p;
135 $e.append( this.$menuTemplate.clone() );
139 this.model.load(true);
142 dispose: function() {
143 this.model.removeObserver(this);
147 itemClicked: function(event)
151 console.log('click:', event, event.ctrlKey, event.target);
152 var $e = $(event.target);
154 if($e.hasClass('annotation'))
156 if(this.currentOpen) return false;
158 var $p = $e.parent();
159 if(this.currentFocused)
161 console.log(this.currentFocused, $p);
162 if($p[0] == this.currentFocused[0]) {
163 console.log('unfocus of current');
164 this.unfocusAnnotation();
168 console.log('switch unfocus');
169 this.unfocusAnnotation();
172 this.focusAnnotation($p);
177 * Clicking outside of focused area doesn't unfocus by default
178 * - this greatly simplifies the whole click check
181 if( $e.hasClass('theme-ref') )
184 this.selectTheme($e.attr('x-theme-class'));
190 if($e.hasClass('edit-button'))
191 this.openForEdit( this.editableFor($e) );
193 if($e.hasClass('accept-button'))
194 this.closeWithSave( this.editableFor($e) );
196 if($e.hasClass('reject-button'))
197 this.closeWithoutSave( this.editableFor($e) );
199 messageCenter.addMessage('error', "wlsave", 'Błąd:' + e.text);
205 unfocusAnnotation: function()
207 if(!this.currentFocused)
209 console.log('Redundant unfocus');
214 && this.currentOpen.is("*[x-annotation-box]")
215 && this.currentOpen.parent()[0] == this.currentFocused[0])
217 console.log("Can't unfocus open box");
221 var $box = $("*[x-annotation-box]", this.currentFocused);
225 // this.currentFocused.removeAttr('x-focused');
226 // this.currentFocused.hide();
227 this.currentFocused = null;
230 focusAnnotation: function($e) {
231 this.currentFocused = $e;
232 var $box = $("*[x-annotation-box]", $e);
237 // $e.attr('x-focused', 'focused');
240 closeWithSave: function($e) {
241 var $edit = $e.data('edit-overlay');
242 var newText = $('textarea', $edit).val();
244 this.model.updateWithWLML($e, newText);
246 this.currentOpen = null;
249 closeWithoutSave: function($e) {
250 var $edit = $e.data('edit-overlay');
252 $e.removeAttr('x-open');
253 this.currentOpen = null;
256 editableFor: function($button)
261 while( ($e[0] != this.element[0]) && !($e.attr('x-editable')) && n < 50)
263 // console.log($e, $e.parent(), this.element);
268 if(!$e.attr('x-editable'))
269 throw Exception("Click outside of editable")
271 console.log("Trigger", $button, " yields editable: ", $e);
275 openForEdit: function($origin)
277 if(this.currentOpen && this.currentOpen != $origin) {
278 this.closeWithSave(this.currentOpen);
283 // annotations overlay their sub box - not their own box //
284 if($origin.is(".annotation-inline-box"))
285 $box = $("*[x-annotation-box]", $origin);
289 var x = $box[0].offsetLeft;
290 var y = $box[0].offsetTop;
291 var w = $box.outerWidth();
292 var h = $box.innerHeight();
294 console.log("Edit origin:", $origin, " box:", $box);
295 console.log("offsetParent:", $box[0].offsetParent);
296 console.log("Dimensions: ", x, y, w , h);
298 // start edition on this node
299 var $overlay = $('<div class="html-editarea"><textarea></textarea></div>');
302 position: 'absolute',
310 $('textarea', $overlay).val( this.model.asWLML($origin[0]) );
312 if($origin.is(".annotation-inline-box"))
314 if(this.currentFocused) {
315 // if some other is focused
316 if($origin[0] != this.currentFocused[0]) {
317 this.unfocusAnnotation();
318 this.focusAnnotation($origin);
322 else { // nothing was focused
323 this.focusAnnotation($origin);
326 else { // this item is not focusable
327 if(this.currentFocused) this.unfocusAnnotation();
330 $($box[0].offsetParent).append($overlay);
331 $origin.data('edit-overlay', $overlay);
333 this.currentOpen = $origin;
334 $origin.attr('x-open', 'open');
337 console.log("Can't open", e);
345 var selection = window.getSelection();
346 var n = selection.rangeCount;
348 console.log("Range count:", n);
351 window.alert("Nie zaznaczono żadnego obszaru");
353 // for now allow only 1 range
355 window.alert("Zaznacz jeden obszar");
358 // from this point, we will assume that the ranges are disjoint
359 for(var i=0; i < n; i++)
361 var range = selection.getRangeAt(i);
362 console.log(i, range.startContainer, range.endContainer);
363 var date = Date.now();
364 var random = Math.floor(4000000000*Math.random());
365 var id = (''+date) + '-' + (''+random);
367 var spoint = document.createRange();
368 var epoint = document.createRange();
370 spoint.setStart(range.startContainer, range.startOffset);
371 epoint.setStart(range.endContainer, range.endOffset);
375 var elem = $('<span x-node="motyw" class="theme-ref">Nowy motyw</span>');
376 elem.attr('x-attrib-id', 'm'+id);
377 spoint.insertNode(elem[0]);
379 // insert theme-begin
380 elem = $('<span x-node="begin"></span>');
381 elem.attr('x-attrib-id', 'b'+id);
382 spoint.insertNode(elem[0]);
384 elem = $('<span x-node="end" class="theme-end"></span>');
385 elem.attr('x-attrib-id', 'e'+id);
386 epoint.insertNode(elem[0]);
389 //selection.removeAllRanges();
392 selectTheme: function(themeId)
394 var selection = document.getSelection();
396 // remove current selection
397 selection.removeAllRanges();
399 var range = document.createRange();
400 var s = $('#m'+themeId)[0];
401 var e = $('#e'+themeId)[0];
402 console.log('Selecting range:', themeId, range, s, e);
405 range.setStartAfter(s);
406 range.setEndBefore(e);
407 selection.addRange(range);
413 panels['html'] = HTMLView;