From d6611c1029576734267438c8255b51bc0f1fc39a Mon Sep 17 00:00:00 2001 From: Dan Helfman Date: Mon, 12 Jan 2009 14:19:32 -0800 Subject: [PATCH] Fix for several note focusing problems. Also converted more of Wiki.js not to rely on an editor having an iframe. --- static/js/Editor.js | 71 +++++++++++++++++++++++++++------------------ static/js/Wiki.js | 34 +++++++++------------- 2 files changed, 56 insertions(+), 49 deletions(-) diff --git a/static/js/Editor.js b/static/js/Editor.js index 156e098..edefb59 100644 --- a/static/js/Editor.js +++ b/static/js/Editor.js @@ -149,8 +149,6 @@ Editor.prototype.create_iframe = function ( position_after ) { this.connect_note_controls( true ); disconnectAll( this.div ); - addElementClass( static_note, "focused_note_frame" ); - var frame_height = elementDimensions( static_note ).h; insertSiblingNodesAfter( static_note, this.iframe ); @@ -265,6 +263,7 @@ Editor.prototype.create_div = function ( position_after ) { this.div = createDOM( "div", { "class": "static_note_div", "id": "static_note_" + this.id }, static_contents ); + this.div.editor = this; // if there is already an iframe open for this editor, replace it with the new static note div if ( getElement( "note_" + this.id ) ) { @@ -311,7 +310,7 @@ Editor.prototype.connect_handlers = function () { var self = this; // necessary so that the member functions of this editor object are used if ( this.div ) { - connect( this.div, "onclick", function ( event ) { self.focused( event ); self.mouse_clicked( event ); } ); + connect( this.div, "onclick", function ( event ) { self.focus( event ); self.mouse_clicked( event ); } ); connect( this.div, "onmouseover", function ( event ) { self.mouse_hovered( event ); } ); connect( this.div, "ondragover", function ( event ) { self.mouse_dragged( event ); } ); } else { @@ -319,10 +318,7 @@ Editor.prototype.connect_handlers = function () { connect( this.document, "onkeydown", function ( event ) { self.key_pressed( event ); } ); connect( this.document, "onkeyup", function ( event ) { self.key_released( event ); } ); } - connect( this.document, "onfocus", function ( event ) { self.focused( event ); } ); - connect( this.document.body, "onfocus", function ( event ) { self.focused( event ); } ); - connect( this.iframe.contentWindow, "onfocus", function ( event ) { self.focused( event ); } ); - connect( this.document, "onclick", function ( event ) { self.mouse_clicked( event ); } ); + connect( this.document, "onclick", function ( event ) { self.focus(); self.mouse_clicked( event ); } ); connect( this.document, "onmouseover", function ( event ) { self.mouse_hovered( event ); } ); connect( this.document, "ondragover", function ( event ) { self.mouse_dragged( event ); } ); connect( this.iframe.contentWindow, "onpaste", function ( event ) { setTimeout( function () { self.resize() }, 50 ); } ); @@ -366,11 +362,13 @@ Editor.prototype.connect_handlers = function () { this.exec_command( "insertbronreturn", true ); } - if ( this.init_highlight ) self.highlight(); + if ( this.init_highlight && this.iframe ) + this.highlight(); this.scrape_title(); if ( this.init_focus ) { - this.focus(); + if ( this.iframe ) + this.focus(); // special-case: focus any username field found within this div if ( this.div ) { @@ -702,19 +700,6 @@ Editor.prototype.scrape_title = function () { this.title = title; } -Editor.prototype.focused = function () { - if ( this.edit_enabled ) - this.create_iframe(); - - signal( this, "focused", this ); -} - -Editor.prototype.blurred = function () { - this.scrape_title(); - - this.create_div(); -} - Editor.title_placeholder_char = "\u200b"; Editor.title_placeholder_pattern = /\u200b/g; Editor.title_placeholder_html = "​​"; @@ -866,13 +851,26 @@ Editor.prototype.find_link_at_cursor = function () { } Editor.prototype.focus = function () { - if ( this.div ) - return; + if ( this.div && this.edit_enabled ) + this.create_iframe(); - if ( OPERA ) - this.iframe.focus(); - else - this.iframe.contentWindow.focus(); + addElementClass( this.div || this.iframe, "focused_note_frame" ); + + if ( this.iframe ) { + if ( OPERA ) + this.iframe.focus(); + else + this.iframe.contentWindow.focus(); + } + + signal( this, "focused", this ); +} + +Editor.prototype.blur = function () { + this.scrape_title(); + this.create_div(); + + removeElementClass( this.div || this.iframe, "focused_note_frame" ); } Editor.prototype.contents = function () { @@ -1097,3 +1095,20 @@ function link_title( link, query ) { function normalize_title( title ) { return title.replace( Editor.title_placeholder_pattern, "" ) || "untitled note"; } + +function editor_by_id( note_id, revision ) { + if ( revision ) + var iframe = getElement( "note_" + note_id + " " + revision ); + else + var iframe = getElement( "note_" + note_id ); + + if ( iframe ) + return iframe.editor; + + var div = getElement( "static_note_" + note_id ); + + if ( div ) + return div.editor; + + return null; +} diff --git a/static/js/Wiki.js b/static/js/Wiki.js index e2475ff..d3c1d38 100644 --- a/static/js/Wiki.js +++ b/static/js/Wiki.js @@ -533,13 +533,10 @@ Wiki.prototype.load_editor = function ( note_title, note_id, revision, previous_ // if the note corresponding to the link's id is already open, highlight it and bail, but only if // we didn't pull a title from an open link pulldown if ( !pulldown_title ) { - if ( revision ) - var iframe = getElement( "note_" + note_id + " " + revision ); - else - var iframe = getElement( "note_" + note_id ); + var editor = editor_by_id( note_id, revision ); - if ( iframe ) { - iframe.editor.highlight(); + if ( editor ) { + editor.highlight(); if ( link ) link.href = "/notebooks/" + this.notebook.object_id + "?note_id=" + note_id; return; @@ -1022,13 +1019,9 @@ Wiki.prototype.update_link_with_suggestion = function ( editor, link, note ) { } Wiki.prototype.editor_focused = function ( editor, synchronous ) { - if ( editor ) - addElementClass( editor.iframe || editor.div, "focused_note_frame" ); - if ( this.focused_editor && this.focused_editor != editor && this.focused_editor.iframe ) { this.clear_pulldowns(); - removeElementClass( this.focused_editor.iframe, "focused_note_frame" ); - this.focused_editor.blurred(); + this.focused_editor.blur(); // if the formerly focused editor is completely empty, then remove it as the user leaves it and switches to this editor if ( this.focused_editor.id == this.blank_editor_id && this.focused_editor.empty() ) { @@ -1762,10 +1755,9 @@ Wiki.prototype.submit_form = function ( form ) { else self.display_message( "The note is already at that revision." ); - var frame = getElement( "note_" + form.note_id.value ); + var editor = editor_by_id( form.note_id.value ); - if ( frame ) { - var editor = frame.editor; + if ( editor ) { self.update_editor_revisions( result, editor ); editor.mark_clean(); @@ -3311,10 +3303,10 @@ function Link_pulldown( wiki, notebook_id, invoker, editor, link, ephemeral ) { // if this link has an actual destination note id set, then see if that note is already open. if // so, display its title and a summary of its contents - var iframe = getElement( "note_" + id ); - if ( iframe ) { - this.title_field.value = iframe.editor.title; - this.display_summary( iframe.editor.title, iframe.editor.summarize() ); + var destination_editor = editor_by_id( id ); + if ( destination_editor ) { + this.title_field.value = destination_editor.title; + this.display_summary( destination_editor.title, destination_editor.summarize() ); Pulldown.prototype.finish_init.call( this ); return; } @@ -4686,9 +4678,9 @@ Note_tree.prototype.save_and_display_startup_note = function ( note ) { // mark the note as a startup note on the client this.wiki.startup_notes[ note.object_id ] = true; - var frame = getElement( "note_" + note.object_id ); - if ( frame ) - frame.editor.startup = true; + var editor = editor_by_id( note.object_id ); + if ( editor ) + editor.startup = true; // save the note as a startup note on the server, and then add it to the note tree var self = this;