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 this._super(element, model, template);
13 .addObserver(this, 'data', this.modelDataChanged.bind(this))
14 .addObserver(this, 'state', this.modelStateChanged.bind(this));
16 this.$menuTemplate = $(render_template('html-view-frag-menu-template', 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) {
28 $('.htmlview', this.element).html(value);
29 this.updatePrintLink();
32 /* upgrade editable elements */
33 $("*[x-editable]", this.$docbase).each(function() {
34 $(this).append( self.$menuTemplate.clone() );
38 /* $(".theme-ref", this.$docbase).each(function() {
39 var id = $(this).attr('x-theme-class');
41 var end = $("span.theme-end[x-theme-class = " + id+"]");
42 var begin = $("span.theme-begin[x-theme-class = " + id+"]");
44 var h = $(this).outerHeight();
46 h = Math.max(h, end.offset().top - begin.offset().top);
47 $(this).css('height', h);
51 updatePrintLink: function() {
52 var base = this.$printLink.attr('ui:baseref');
53 this.$printLink.attr('href', base + "?user="+this.model.document.get('user')+"&revision=" + this.model.get('revision'));
56 modelStateChanged: function(property, value)
60 if (value == 'synced' || value == 'dirty') {
62 } else if (value == 'unsynced') {
63 if(this.currentOpen) this.closeWithoutSave(this.currentOpen);
64 this.freeze('Niezsynchronizowany...');
65 } else if (value == 'loading') {
66 this.freeze('Ładowanie...');
67 } else if (value == 'saving') {
68 this.freeze('Zapisywanie...');
69 } else if (value == 'error') {
70 this.freeze(this.model.get('error'));
71 $('.xml-editor-ref', this.overlay).click(
73 console.log("Sending scroll rq.", this);
75 var href = $(this).attr('href').split('-');
76 var line = parseInt(href[1]);
77 var column = parseInt(href[2]);
79 $(document).trigger('xml-scroll-request', {line:line, column:column});
91 this.$docbase.unbind('click');
94 this.$printLink.unbind();
96 if(this.$addThemeButton)
97 this.$addThemeButton.unbind();
101 this.$printLink = $('.htmlview-toolbar .html-print-link', this.element);
102 this.$docbase = $('.htmlview', this.element);
103 this.$addThemeButton = $('.htmlview-toolbar .html-add-motive', this.element);
105 this.updatePrintLink();
106 this.$docbase.bind('click', this.itemClicked.bind(this));
107 this.$addThemeButton.click( this.addTheme.bind(this) );
110 renderPart: function($e, html) {
111 // exceptions aren't good, but I don't have a better idea right now
112 if($e.attr('x-annotation-box')) {
113 // replace the whole annotation
114 var $p = $e.parent();
116 var $box = $('*[x-annotation-box]', $p);
117 $box.append( this.$menuTemplate.clone() );
119 if(this.currentFocused && $p[0] == this.currentFocused[0])
121 this.currentFocused = $p;
122 $box.css({'display': 'block'});
129 $e.append( this.$menuTemplate.clone() );
133 this.model.load(true);
136 dispose: function() {
137 this.model.removeObserver(this);
141 itemClicked: function(event)
145 console.log('click:', event, event.ctrlKey, event.target);
146 var $e = $(event.target);
148 if($e.hasClass('annotation'))
150 if(this.currentOpen) return false;
152 var $p = $e.parent();
153 if(this.currentFocused)
155 console.log(this.currentFocused, $p);
156 if($p[0] == this.currentFocused[0]) {
157 console.log('unfocus of current');
158 this.unfocusAnnotation();
162 console.log('switch unfocus');
163 this.unfocusAnnotation();
166 this.focusAnnotation($p);
171 * Clicking outside of focused area doesn't unfocus by default
172 * - this greatly simplifies the whole click check
175 if( $e.hasClass('theme-ref') )
178 this.selectTheme($e.attr('x-theme-class'));
182 if($e.hasClass('edit-button'))
183 this.openForEdit( this.editableFor($e) );
185 if($e.hasClass('accept-button'))
186 this.closeWithSave( this.editableFor($e) );
188 if($e.hasClass('reject-button'))
189 this.closeWithoutSave( this.editableFor($e) );
192 unfocusAnnotation: function()
194 if(!this.currentFocused)
196 console.log('Redundant unfocus');
201 && this.currentOpen.is("*[x-annotation-box]")
202 && this.currentOpen.parent()[0] == this.currentFocused[0])
204 console.log("Can't unfocus open box");
208 var $box = $("*[x-annotation-box]", this.currentFocused);
209 $box.css({'display': 'none'});
210 // this.currentFocused.removeAttr('x-focused');
211 // this.currentFocused.hide();
212 this.currentFocused = null;
215 focusAnnotation: function($e) {
216 this.currentFocused = $e;
217 var $box = $("*[x-annotation-box]", $e);
218 $box.css({'display': 'block'});
220 // $e.attr('x-focused', 'focused');
223 closeWithSave: function($e) {
224 var $edit = $e.data('edit-overlay');
225 var newText = $('textarea', $edit).val();
227 this.model.putXMLPart($e, newText, function($e, html) {
228 this.renderPart($e, html);
230 $e.removeAttr('x-open');
232 this.currentOpen = null;
235 closeWithoutSave: function($e) {
236 var $edit = $e.data('edit-overlay');
238 $e.removeAttr('x-open');
239 this.currentOpen = null;
242 editableFor: function($button)
247 while( ($e[0] != this.element[0]) && !($e.attr('x-editable')) && n < 50)
249 // console.log($e, $e.parent(), this.element);
254 if(!$e.attr('x-editable'))
255 throw Exception("Click outside of editable")
257 console.log("Trigger", $button, " yields editable: ", $e);
261 openForEdit: function($origin)
263 if(this.currentOpen && this.currentOpen != $origin) {
264 this.closeWithSave(this.currentOpen);
267 var x = $origin[0].offsetLeft;
268 var y = $origin[0].offsetTop;
269 var w = $origin.outerWidth();
270 var h = $origin.innerHeight();
272 console.log("Editable:", $origin, " offsetParent:", $origin[0].offsetParent);
273 console.log("Dimensions: ", x, y, w , h);
275 // start edition on this node
276 var $overlay = $('<div class="html-editarea"><textarea></textarea></div>');
278 $overlay.css({position: 'absolute', height: h, left: x, top: y, width: '95%'});
279 $($origin[0].offsetParent).append($overlay);
280 $origin.data('edit-overlay', $overlay);
282 this.model.getXMLPart($origin, function(path, data) {
283 $('textarea', $overlay).val(data);
286 if($origin.is("*[x-annotation-box]"))
288 var $b = $origin.parent();
289 if(this.currentFocused) {
290 // if some other is focused
291 if($b[0] != this.currentFocused[0]) {
292 this.unfocusAnnotation();
293 this.focusAnnotation($b);
297 else { // nothing was focused
298 this.focusAnnotation($b);
301 else { // this item is not focusable
302 if(this.currentFocused) this.unfocusAnnotation();
305 this.currentOpen = $origin;
306 $origin.attr('x-open', 'open');
313 var selection = document.getSelection();
314 var n = selection.rangeCount;
317 window.alert("Nie zaznaczono żadnego obszaru");
319 // for now allow only 1 range
321 window.alert("Zaznacz jeden obszar");
323 // from this point, we will assume that the ranges are disjoint
324 for(var i=0; i < n; i++) {
325 var range = selection.getRangeAt(i);
326 console.log(i, range.startContainer, range.endContainer);
327 var date = Date.now();
328 var random = Math.floor(4000000000*Math.random());
329 var id = (''+date) + '-' + (''+random);
331 var ipoint = document.createRange();
333 // Firefox alters the later node when inserting, so
335 ipoint.setStart(range.endContainer, range.endOffset);
336 elem = $('<span class="theme-end" x-theme-class="'+id+'" id="e'+id+'"></span>')[0];
337 ipoint.insertNode(elem);
340 ipoint.setStart(range.startContainer, range.startOffset);
341 var elem = $('<span class="theme-ref" x-theme-class="'+id+'" id="m'+id+'">Nowy motyw</span>')[0];
342 ipoint.insertNode(elem);
343 ipoint.setStartBefore(elem);
345 // insert theme-begin
346 elem = $('<span class="theme-begin" x-theme-class="'+id+'" id="b'+id+'"></span>')[0];
347 ipoint.insertNode(elem);
350 selection.removeAllRanges();
353 selectTheme: function(themeId)
355 var selection = document.getSelection();
357 // remove current selection
358 selection.removeAllRanges();
360 var range = document.createRange();
361 var s = $('#m'+themeId)[0];
362 var e = $('#e'+themeId)[0];
363 console.log('Selecting range:', themeId, range, s, e);
366 range.setStartAfter(s);
367 range.setEndBefore(e);
368 selection.addRange(range);
374 panels['html'] = HTMLView;