From cf10cf8c8279ed70737d391f34132b55f271e4c0 Mon Sep 17 00:00:00 2001 From: Dan Helfman Date: Mon, 2 Jun 2008 14:16:35 -0700 Subject: [PATCH] Refactored some of the client-side form-handling code to cut down on special-case hacks. --- NEWS | 2 ++ static/js/Editor.js | 58 ++++++------------------------------- static/js/Wiki.js | 70 +++++++++++++++++++++++++++++---------------- 3 files changed, 56 insertions(+), 74 deletions(-) diff --git a/NEWS b/NEWS index 02b4f4f..0302a63 100644 --- a/NEWS +++ b/NEWS @@ -2,6 +2,8 @@ * Implemented some basic user administration features. * Added new rate plans with support for user administration. * Wrote a tool for manually updating a user's rate plan: tools/set_plan.py + * Refactored some of the client-side form-handling code to cut down on + special-case hacks. 1.3.40: May 27, 2008 * Added some minor product page tweaks like meta description tags. diff --git a/static/js/Editor.js b/static/js/Editor.js index a3a3862..ba36ff0 100644 --- a/static/js/Editor.js +++ b/static/js/Editor.js @@ -160,67 +160,27 @@ Editor.prototype.finish_init = function () { for ( var i in forms ) { var form = forms[ i ]; connect( form, "onsubmit", function ( event ) { - signal( self, "submit_form", form.getAttribute( "target" ), form ); + signal( self, "submit_form", form ); event.stop(); } ); } // connect each (non-submit) button to issue an event - var buttons = getElementsByTagAndClassName( "input", "button", this.document ); - for ( var i in buttons ) { - var button = buttons[ i ]; - if ( button.getAttribute( "type" ) == "submit") - continue; - + function connect_button( button ) { connect( button, "onclick", function ( event ) { signal( self, "button_clicked", this, button ); event.stop(); } ); } - // special-case: connect any submit buttons within the contents of this note - // TODO: phase out entirely - withDocument( this.document, function () { - var invite_button = getElement( "invite_button" ); - if ( invite_button ) { - var invite_form = getElement( "invite_form" ); - connect( invite_button, "onclick", function ( event ) { - signal( self, "submit_form", "/users/send_invites", invite_form, function ( result ) { - if ( !result.invites ) return; - signal( self, "invites_updated", result.invites ); - } ); - event.stop(); - } ); + var buttons = getElementsByTagAndClassName( "input", "button", this.document ); + for ( var i in buttons ) { + var button = buttons[ i ]; + if ( button.getAttribute( "type" ) == "submit") + continue; - function connect_button( revoke_button, invite_id ) { - connect( revoke_button, "onclick", function ( event ) { - signal( self, "revoke_invite", invite_id, function ( result ) { - if ( !result.invites ) return; - signal( self, "invites_updated", result.invites ); - } ); - event.stop(); - } ); - } - - var revoke_buttons = getElementsByTagAndClassName( "input", "revoke_button" ); - for ( var i in revoke_buttons ) { - var revoke_button = revoke_buttons[ i ]; - var invite_id = revoke_button.id.split( "_" ).pop(); - connect_button( revoke_button, invite_id ); - } - } - - var settings_button = getElement( "settings_button" ); - if ( settings_button ) { - var settings_form = getElement( "settings_form" ); - connect( settings_button, "onclick", function ( event ) { - signal( self, "submit_form", "/users/update_settings", settings_form, function ( result ) { - signal( self, "settings_updated", result ); - } ); - event.stop(); - } ); - } - } ); + connect_button( button ); + } // browsers such as Firefox, but not Opera if ( this.iframe.contentDocument && !/Opera/.test( navigator.userAgent ) && this.edit_enabled ) diff --git a/static/js/Wiki.js b/static/js/Wiki.js index 926e138..6190281 100644 --- a/static/js/Wiki.js +++ b/static/js/Wiki.js @@ -732,30 +732,8 @@ Wiki.prototype.create_editor = function ( id, note_text, deleted_from_id, revisi connect( editor, "load_editor", this, "load_editor" ); connect( editor, "hide_clicked", function ( event ) { self.hide_editor( event, editor ) } ); - connect( editor, "invites_updated", function ( invites ) { self.invites = invites; self.share_notebook(); } ); - connect( editor, "settings_updated", function ( result ) { - self.email_address = result.email_address || ""; - self.display_message( "Your account settings have been updated." ); - } ); - - connect( editor, "submit_form", function ( url, form, callback ) { - var args = {} - if ( url == "/users/signup" ) { - args[ "invite_id" ] = self.invite_id; - args[ "rate_plan" ] = self.signup_plan; - args[ "yearly" ] = self.yearly; - } else if ( url == "/users/login" ) { - args[ "invite_id" ] = self.invite_id; - } - - self.invoker.invoke( url, "POST", args, callback, form ); - } ); - connect( editor, "revoke_invite", function ( invite_id, callback ) { - self.invoker.invoke( "/users/revoke_invite", "POST", { - "notebook_id": self.notebook_id, - "invite_id": invite_id - }, callback ); - } ); + connect( editor, "submit_form", function ( form ) { self.submit_form( form ); } ); + connect( editor, "button_clicked", function ( editor, button ) { self.editor_button_clicked( editor, button ); } ); this.clear_pulldowns(); @@ -1440,6 +1418,48 @@ Wiki.prototype.update_editor_revisions = function ( result, editor ) { editor.user_revisions.push( result.new_revision ); } +Wiki.prototype.submit_form = function ( form ) { + var self = this; + var args = {} + var url = form.getAttribute( "target" ); + var callback = null; + + if ( url == "/users/signup" ) { + args[ "invite_id" ] = this.invite_id; + args[ "rate_plan" ] = this.signup_plan; + args[ "yearly" ] = this.yearly; + } else if ( url == "/users/login" ) { + args[ "invite_id" ] = this.invite_id; + } else if ( url == "/users/send_invites" ) { + callback = function ( result ) { + if ( !result.invites ) return; + self.invites = result.invites; + self.share_notebook(); + } + } else if ( url == "/users/update_settings" ) { + self.email_address = result.email_address || ""; + self.display_message( "Your account settings have been updated." ); + } + + this.invoker.invoke( url, "POST", args, callback, form ); +} + +Wiki.prototype.editor_button_clicked = function ( editor, button ) { + if ( hasElementClass( button, "revoke_button" ) ) { + var invite_id = button.id.split( "_" ).pop(); + var self = this; + + this.invoker.invoke( "/users/revoke_invite", "POST", { + "notebook_id": this.notebook_id, + "invite_id": invite_id + }, function ( result ) { + if ( !result.invites ) return; + self.invites = result.invites; + self.share_notebook(); + } ); + } +} + Wiki.prototype.search = function ( event ) { this.clear_messages(); this.clear_pulldowns(); @@ -1645,7 +1665,7 @@ Wiki.prototype.display_invites = function ( invite_area ) { var revoke_button = createDOM( "input", { "type": "button", "id": "revoke_" + invite.object_id, - "class": "revoke_button", + "class": "revoke_button button", "value": " x ", "title": "revoke this person's notebook access" } );