X-Git-Url: https://git.mdrn.pl/redakcja.git/blobdiff_plain/7a12b550f4a8366c8ace2f9c55706aba7c4b17f7..061f517181630ee982c33aee63965b18965fd5aa:/platforma/static/js/views/html.js?ds=inline diff --git a/platforma/static/js/views/html.js b/platforma/static/js/views/html.js index 3696bdc8..0b1234ce 100755 --- a/platforma/static/js/views/html.js +++ b/platforma/static/js/views/html.js @@ -6,26 +6,47 @@ var HTMLView = View.extend({ template: 'html-view-template', init: function(element, model, parent, template) { - this._super(element, model, template); - this.parent = parent; + var submodel = model.contentModels['html']; + this._super(element, submodel, template); + this.parent = parent; this.model - .addObserver(this, 'data', this.modelDataChanged.bind(this)) + .addObserver(this, 'data', this.modelDataChanged.bind(this)) .addObserver(this, 'state', this.modelStateChanged.bind(this)); - - $('.htmlview', this.element).html(this.model.get('data')); + this.modelStateChanged('state', this.model.get('state')); - this.model.load(); + this.modelDataChanged('data', this.model.get('data')); + + this.model.load(); + + this.currentOpen = null; + this.currentFocused = null; + this.themeBoxes = []; }, - modelDataChanged: function(property, value) { - $('.htmlview', this.element).html(value); + modelDataChanged: function(property, value) + { + if(!value) return; + + // the xml model changed + var container = $('.htmlview', this.element); + container.empty(); + container.append(value); + this.updatePrintLink(); + + /* mark themes */ + /* $(".theme-ref", this.$docbase).each(function() { + var id = $(this).attr('x-theme-class'); - $("*[x-editable]").each(function() { - var e = $('
'); - e.appendTo(this); - }); + var end = $("span.theme-end[x-theme-class = " + id+"]"); + var begin = $("span.theme-begin[x-theme-class = " + id+"]"); + + var h = $(this).outerHeight(); + + h = Math.max(h, end.offset().top - begin.offset().top); + $(this).css('height', h); + }); */ }, updatePrintLink: function() { @@ -40,6 +61,7 @@ var HTMLView = View.extend({ if (value == 'synced' || value == 'dirty') { this.unfreeze(); } else if (value == 'unsynced') { + if(this.currentOpen) this.closeWithoutSave(this.currentOpen); this.freeze('Niezsynchronizowany...'); } else if (value == 'loading') { this.freeze('Åadowanie...'); @@ -48,33 +70,77 @@ var HTMLView = View.extend({ } else if (value == 'error') { this.freeze(this.model.get('error')); $('.xml-editor-ref', this.overlay).click( - function(event) { - console.log("Sending scroll rq.", this); - try { - var href = $(this).attr('href').split('-'); - var line = parseInt(href[1]); - var column = parseInt(href[2]); + function(event) { + console.log("Sending scroll rq.", this); + try { + var href = $(this).attr('href').split('-'); + var line = parseInt(href[1]); + var column = parseInt(href[2]); - $(document).trigger('xml-scroll-request', {line:line, column:column}); - } catch(e) { - console.log(e); - } + $(document).trigger('xml-scroll-request', { + line:line, + column:column + }); + } catch(e) { + console.log(e); + } - return false; - }); + return false; + }); } }, render: function() { - this.element.unbind('click'); + if(this.$docbase) + this.$docbase.unbind('click'); + + if(this.$printLink) + this.$printLink.unbind(); + + if(this.$addThemeButton) + this.$addThemeButton.unbind(); - if(this.$printLink) this.$printLink.unbind(); this._super(); - this.$printLink = $('.html-print-link', this.element); + + this.$printLink = $('.htmlview-toolbar .html-print-link', this.element); + this.$docbase = $('.htmlview', this.element); + this.$addThemeButton = $('.htmlview-toolbar .html-add-motive', this.element); + + this.$debugButton = $('.htmlview-toolbar .html-serialize', this.element); + this.updatePrintLink(); + this.$docbase.bind('click', this.itemClicked.bind(this)); + this.$addThemeButton.click( this.addTheme.bind(this) ); + this.$debugButton.click( this.serialized.bind(this) ); + }, + + serialized: function() { + this.model.set('state', 'dirty'); + console.log( this.model.serializer.serializeToString(this.model.get('data')) ); + }, - this.element.bind('click', this.itemClicked.bind(this)); - // this.element.bind('mouseover', this.itemHover.bind(this)); + renderPart: function($e, html) { + // exceptions aren't good, but I don't have a better idea right now + if($e.attr('x-annotation-box')) { + // replace the whole annotation + var $p = $e.parent(); + $p.html(html); + var $box = $('*[x-annotation-box]', $p); + $box.append( this.$menuTemplate.clone() ); + + if(this.currentFocused && $p[0] == this.currentFocused[0]) + { + this.currentFocused = $p; + $box.css({ + 'display': 'block' + }); + } + + return; + } + + $e.html(html); + $e.append( this.$menuTemplate.clone() ); }, reload: function() { @@ -86,84 +152,272 @@ var HTMLView = View.extend({ this._super(); }, - itemHover: function(event) + itemClicked: function(event) { + var self = this; + + console.log('click:', event, event.ctrlKey, event.target); var $e = $(event.target); - if( $e.attr('x-editable') == 'editable' ) { - console.log('over:', $e[0]); - $e.css({'background-color': 'grey'}); + + if($e.hasClass('annotation')) + { + if(this.currentOpen) return false; + + var $p = $e.parent(); + if(this.currentFocused) + { + console.log(this.currentFocused, $p); + if($p[0] == this.currentFocused[0]) { + console.log('unfocus of current'); + this.unfocusAnnotation(); + return false; + } + + console.log('switch unfocus'); + this.unfocusAnnotation(); + } + + this.focusAnnotation($p); + return false; } + /* + * Clicking outside of focused area doesn't unfocus by default + * - this greatly simplifies the whole click check + */ + + if( $e.hasClass('theme-ref') ) + { + console.log($e); + this.selectTheme($e.attr('x-theme-class')); + return false; + } + + /* other buttons */ + try { + if($e.hasClass('edit-button')) + this.openForEdit( this.editableFor($e) ); + + if($e.hasClass('accept-button')) + this.closeWithSave( this.editableFor($e) ); + + if($e.hasClass('reject-button')) + this.closeWithoutSave( this.editableFor($e) ); + } catch(e) { + messageCenter.addMessage('error', "wlsave", 'BÅÄ d:' + e.text); + } + + return false; }, - itemClicked: function(event) + unfocusAnnotation: function() { - var self = this; + if(!this.currentFocused) + { + console.log('Redundant unfocus'); + return false; + } + + if(this.currentOpen + && this.currentOpen.is("*[x-annotation-box]") + && this.currentOpen.parent()[0] == this.currentFocused[0]) + { + console.log("Can't unfocus open box"); + return false; + } + + var $box = $("*[x-annotation-box]", this.currentFocused); + $box.css({ + 'display': 'none' + }); + // this.currentFocused.removeAttr('x-focused'); + // this.currentFocused.hide(); + this.currentFocused = null; + }, + + focusAnnotation: function($e) { + this.currentFocused = $e; + var $box = $("*[x-annotation-box]", $e); + $box.css({ + 'display': 'block' + }); - console.log('click:', event, event.ctrlKey, event.target); - var editableContent = null; - var $e = $(event.target); + // $e.attr('x-focused', 'focused'); + }, + + closeWithSave: function($e) { + var $edit = $e.data('edit-overlay'); + var newText = $('textarea', $edit).val(); - if($e.hasClass('edit-button')) - this.openForEdit($e); + this.model.updateWithWLML($e, newText); + $edit.remove(); + this.currentOpen = null; }, - openForEdit: function($e) - { - var n = 0; + closeWithoutSave: function($e) { + var $edit = $e.data('edit-overlay'); + $edit.remove(); + $e.removeAttr('x-open'); + this.currentOpen = null; + }, - while( ($e[0] != this.element[0]) && !($e.attr('x-editable')) - && n < 50) + editableFor: function($button) + { + var $e = $button; + var n = 0; + + while( ($e[0] != this.element[0]) && !($e.attr('x-editable')) && n < 50) { // console.log($e, $e.parent(), this.element); $e = $e.parent(); n += 1; } - + if(!$e.attr('x-editable')) - return true; + throw Exception("Click outside of editable") - var $origin = $e; - console.log("editable: ", $e); - - // start edition on this node - var $overlay = $( - '