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 $('.htmlview', this.element).html(this.model.get('data'));
18 this.$menuTemplate = $(render_template('html-view-frag-menu-template', this));
20 this.modelStateChanged('state', this.model.get('state'));
23 this.currentOpen = null;
24 this.currentFocused = null;
28 modelDataChanged: function(property, value) {
29 $('.htmlview', this.element).html(value);
30 this.updatePrintLink();
33 /* upgrade editable elements */
34 $("*[x-editable]").each(function() {
35 $(this).append( self.$menuTemplate.clone() );
40 var doc_base = $('.htmlview .utwor', this.element);
43 $(".theme-ref").each(function() {
44 var id = $(this).attr('x-theme-class');
46 var end = $("span.theme-end[x-theme-class = " + id+"]");
47 var begin = $("span.theme-begin[x-theme-class = " + id+"]");
49 var h = $(this).outerHeight();
51 h = Math.max(h, end.offset().top - begin.offset().top);
52 $(this).css('height', h);
56 updatePrintLink: function() {
57 var base = this.$printLink.attr('ui:baseref');
58 this.$printLink.attr('href', base + "?user="+this.model.document.get('user')+"&revision=" + this.model.get('revision'));
61 modelStateChanged: function(property, value)
65 if (value == 'synced' || value == 'dirty') {
67 } else if (value == 'unsynced') {
68 if(this.currentOpen) this.closeWithoutSave(this.currentOpen);
69 this.freeze('Niezsynchronizowany...');
70 } else if (value == 'loading') {
71 this.freeze('Ćadowanie...');
72 } else if (value == 'saving') {
73 this.freeze('Zapisywanie...');
74 } else if (value == 'error') {
75 this.freeze(this.model.get('error'));
76 $('.xml-editor-ref', this.overlay).click(
78 console.log("Sending scroll rq.", this);
80 var href = $(this).attr('href').split('-');
81 var line = parseInt(href[1]);
82 var column = parseInt(href[2]);
84 $(document).trigger('xml-scroll-request', {line:line, column:column});
95 this.element.unbind('click');
97 if(this.$printLink) this.$printLink.unbind();
99 this.$printLink = $('.html-print-link', this.element);
100 this.updatePrintLink();
102 this.element.bind('click', this.itemClicked.bind(this));
103 // this.element.bind('mouseover', this.itemHover.bind(this));
107 this.model.load(true);
110 dispose: function() {
111 this.model.removeObserver(this);
115 itemClicked: function(event)
119 console.log('click:', event, event.ctrlKey, event.target);
120 var $e = $(event.target);
122 if($e.hasClass('annotation'))
124 var $box = $e.parent();
126 if(this.currentFocused)
128 if($box[0] == this.currentFocused[0]) {
129 console.log('unfocus of current');
130 this.unfocusAnnotation();
134 console.log('switch unfocus');
135 this.unfocusAnnotation();
138 this.focusAnnotation($box);
143 * Clicking outside of focused area doesn't unfocus by default
144 * - this greatly simplifies the whole click check
148 if($e.hasClass('edit-button'))
149 this.openForEdit( this.editableFor($e) );
151 if($e.hasClass('accept-button'))
152 this.closeWithSave( this.editableFor($e) );
154 if($e.hasClass('reject-button'))
155 this.closeWithoutSave( this.editableFor($e) );
158 unfocusAnnotation: function() {
159 if(!this.currentFocused)
163 && this.currentOpen.is("*[x-annotation-box]")
164 && this.currentOpen.parent()[0] == this.currentFocused[0])
167 this.currentFocused.removeAttr('x-focused');
168 this.currentFocused = null;
171 focusAnnotation: function($e) {
172 this.currentFocused = $e;
173 $e.attr('x-focused', 'focused');
176 closeWithSave: function($e) {
177 var $edit = $e.data('edit-overlay');
178 var newText = $('textarea', $edit).val();
180 this.model.putXMLPart($e, newText, function($e, html) {
181 this.renderPart($e, html);
183 $e.removeAttr('x-open');
185 this.currentOpen = null;
188 closeWithoutSave: function($e) {
189 var $edit = $e.data('edit-overlay');
191 $e.removeAttr('x-open');
192 this.currentOpen = null;
195 renderPart: function($e, html) {
197 $e.append( this.$menuTemplate.clone() );
200 editableFor: function($button)
205 while( ($e[0] != this.element[0]) && !($e.attr('x-editable')) && n < 50)
207 // console.log($e, $e.parent(), this.element);
212 if(!$e.attr('x-editable'))
213 throw Exception("Click outside of editable")
215 console.log("Trigger", $button, " yields editable: ", $e);
219 openForEdit: function($origin)
221 if(this.currentOpen && this.currentOpen != $origin) {
222 this.closeWithSave(this.currentOpen);
225 var x = $origin[0].offsetLeft;
226 var y = $origin[0].offsetTop;
227 var w = $origin.outerWidth();
228 var h = $origin.innerHeight();
230 console.log("Editable:", $origin, " offsetParent:", $origin[0].offsetParent);
231 console.log("Dimensions: ", x, y, w , h);
233 // start edition on this node
234 var $overlay = $('<div class="html-editarea"><textarea></textarea></div>');
236 $overlay.css({position: 'absolute', height: h, left: x, top: y, width: '95%'});
237 $($origin[0].offsetParent).append($overlay);
238 $origin.data('edit-overlay', $overlay);
240 this.model.getXMLPart($origin, function(path, data) {
241 $('textarea', $overlay).val(data);
244 this.currentOpen = $origin;
245 $origin.attr('x-open', 'open');
247 if($origin.is("*[x-annotation-box]"))
249 var $b = $origin.parent();
250 if(this.currentFocused) {
251 if($b[0] != this.currentFocused[0])
252 this.unfocusAnnotation();
255 this.focusAnnotation($origin);
258 if(this.currentFocused) this.unfocusAnnotation();
267 panels['html'] = HTMLView;