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;
27 modelDataChanged: function(property, value) {
28 $('.htmlview', this.element).html(value);
29 this.updatePrintLink();
32 /* upgrade editable elements */
33 $("*[x-editable]").each(function() {
34 $(this).append( self.$menuTemplate.clone() );
39 var doc_base = $('.htmlview .utwor', this.element);
42 $(".theme-ref").each(function() {
43 var id = $(this).attr('x-theme-class');
45 var end = $("span.theme-end[x-theme-class = " + id+"]");
46 var begin = $("span.theme-begin[x-theme-class = " + id+"]");
48 var h = $(this).outerHeight();
50 h = Math.max(h, end.offset().top - begin.offset().top);
51 $(this).css('height', h);
55 updatePrintLink: function() {
56 var base = this.$printLink.attr('ui:baseref');
57 this.$printLink.attr('href', base + "?user="+this.model.document.get('user')+"&revision=" + this.model.get('revision'));
60 modelStateChanged: function(property, value)
64 if (value == 'synced' || value == 'dirty') {
66 } else if (value == 'unsynced') {
67 if(this.currentOpen) this.closeWithoutSave(this.currentOpen);
68 this.freeze('Niezsynchronizowany...');
69 } else if (value == 'loading') {
70 this.freeze('Ćadowanie...');
71 } else if (value == 'saving') {
72 this.freeze('Zapisywanie...');
73 } else if (value == 'error') {
74 this.freeze(this.model.get('error'));
75 $('.xml-editor-ref', this.overlay).click(
77 console.log("Sending scroll rq.", this);
79 var href = $(this).attr('href').split('-');
80 var line = parseInt(href[1]);
81 var column = parseInt(href[2]);
83 $(document).trigger('xml-scroll-request', {line:line, column:column});
94 this.element.unbind('click');
96 if(this.$printLink) this.$printLink.unbind();
98 this.$printLink = $('.html-print-link', this.element);
99 this.updatePrintLink();
101 this.element.bind('click', this.itemClicked.bind(this));
102 // this.element.bind('mouseover', this.itemHover.bind(this));
106 this.model.load(true);
109 dispose: function() {
110 this.model.removeObserver(this);
114 itemClicked: function(event)
118 console.log('click:', event, event.ctrlKey, event.target);
119 var $e = $(event.target);
121 if($e.hasClass('edit-button'))
122 this.openForEdit( this.editableFor($e) );
124 if($e.hasClass('accept-button'))
125 this.closeWithSave( this.editableFor($e) );
127 if($e.hasClass('reject-button'))
128 this.closeWithoutSave( this.editableFor($e) );
131 closeWithSave: function($e) {
132 var $edit = $e.data('edit-overlay');
133 var newText = $('textarea', $edit).val();
135 this.model.putXMLPart($e, newText, function($e, html) {
136 this.renderPart($e, html);
138 $e.removeAttr('x-open');
140 this.currentOpen = null;
143 closeWithoutSave: function($e) {
144 var $edit = $e.data('edit-overlay');
146 $e.removeAttr('x-open');
147 this.currentOpen = null;
150 renderPart: function($e, html) {
152 $e.append( this.$menuTemplate.clone() );
155 editableFor: function($button)
160 while( ($e[0] != this.element[0]) && !($e.attr('x-editable')) && n < 50)
162 // console.log($e, $e.parent(), this.element);
167 if(!$e.attr('x-editable'))
168 throw Exception("Click outside of editable")
173 openForEdit: function($origin)
175 if(this.currentOpen && this.currentOpen != $origin) {
176 this.closeWithSave(this.currentOpen);
180 // start edition on this node
181 var $overlay = $('<div class="html-editarea"><textarea></textarea></div>');
183 var x = $origin[0].offsetLeft;
184 var y = $origin[0].offsetTop;
185 var w = $origin.outerWidth();
186 var h = $origin.innerHeight();
188 $overlay.css({position: 'absolute', height: h, left: x, top: y, width: '95%'});
190 $origin.offsetParent().append($overlay);
191 $origin.data('edit-overlay', $overlay);
193 this.model.getXMLPart($origin, function(path, data) {
194 $('textarea', $overlay).val(data);
197 this.currentOpen = $origin;
198 $origin.attr('x-open', 'open');
207 panels['html'] = HTMLView;