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;
26 modelDataChanged: function(property, value) {
27 $('.htmlview', this.element).html(value);
28 this.updatePrintLink();
31 $("*[x-editable]").each(function() {
32 $(this).append( self.$menuTemplate.clone() );
36 updatePrintLink: function() {
37 var base = this.$printLink.attr('ui:baseref');
38 this.$printLink.attr('href', base + "?user="+this.model.document.get('user')+"&revision=" + this.model.get('revision'));
41 modelStateChanged: function(property, value)
45 if (value == 'synced' || value == 'dirty') {
47 } else if (value == 'unsynced') {
48 if(this.currentOpen) this.closeWithoutSave(this.currentOpen);
49 this.freeze('Niezsynchronizowany...');
50 } else if (value == 'loading') {
51 this.freeze('Ładowanie...');
52 } else if (value == 'saving') {
53 this.freeze('Zapisywanie...');
54 } else if (value == 'error') {
55 this.freeze(this.model.get('error'));
56 $('.xml-editor-ref', this.overlay).click(
58 console.log("Sending scroll rq.", this);
60 var href = $(this).attr('href').split('-');
61 var line = parseInt(href[1]);
62 var column = parseInt(href[2]);
64 $(document).trigger('xml-scroll-request', {line:line, column:column});
75 this.element.unbind('click');
77 if(this.$printLink) this.$printLink.unbind();
79 this.$printLink = $('.html-print-link', this.element);
80 this.updatePrintLink();
82 this.element.bind('click', this.itemClicked.bind(this));
83 // this.element.bind('mouseover', this.itemHover.bind(this));
87 this.model.load(true);
91 this.model.removeObserver(this);
95 itemClicked: function(event)
99 console.log('click:', event, event.ctrlKey, event.target);
100 var $e = $(event.target);
102 if($e.hasClass('edit-button'))
103 this.openForEdit( this.editableFor($e) );
105 if($e.hasClass('accept-button'))
106 this.closeWithSave( this.editableFor($e) );
108 if($e.hasClass('reject-button'))
109 this.closeWithoutSave( this.editableFor($e) );
112 closeWithSave: function($e) {
113 var $edit = $e.data('edit-overlay');
114 var newText = $('textarea', $edit).val();
116 this.model.putXMLPart($e, newText, function($e, html) {
117 this.renderPart($e, html);
119 $e.removeAttr('x-open');
121 this.currentOpen = null;
124 closeWithoutSave: function($e) {
125 var $edit = $e.data('edit-overlay');
127 $e.removeAttr('x-open');
128 this.currentOpen = null;
131 renderPart: function($e, html) {
133 $e.append( this.$menuTemplate.clone() );
136 editableFor: function($button)
141 while( ($e[0] != this.element[0]) && !($e.attr('x-editable')) && n < 50)
143 // console.log($e, $e.parent(), this.element);
148 if(!$e.attr('x-editable'))
149 throw Exception("Click outside of editable")
154 openForEdit: function($origin)
156 if(this.currentOpen && this.currentOpen != $origin) {
157 this.closeWithSave(this.currentOpen);
161 // start edition on this node
162 var $overlay = $('<div class="html-editarea"><textarea></textarea></div>');
164 var x = $origin[0].offsetLeft;
165 var y = $origin[0].offsetTop;
166 var w = $origin.outerWidth();
167 var h = $origin.innerHeight();
169 $overlay.css({position: 'absolute', height: h, left: x, top: y, width: '95%'});
171 $origin.offsetParent().append($overlay);
172 $origin.data('edit-overlay', $overlay);
174 this.model.getXMLPart($origin, function(path, data) {
175 $('textarea', $overlay).val(data);
178 this.currentOpen = $origin;
179 $origin.attr('x-open', 'open');
188 panels['html'] = HTMLView;