| 1 | /* |
|---|
| 2 | $Id$ |
|---|
| 3 | |
|---|
| 4 | Copyright Six Apart, Ltd. All rights reserved. |
|---|
| 5 | Redistribution and use in source and binary forms is |
|---|
| 6 | subject to the Six Apart JavaScript license: |
|---|
| 7 | |
|---|
| 8 | http://code.sixapart.com/svn/js/trunk/LICENSE.txt |
|---|
| 9 | */ |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | Editor.Toolbar = new Class( Component, { |
|---|
| 13 | CLASSNAME_ROOT: "editor-state-", |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | initObject: function( element, editor ) { |
|---|
| 17 | arguments.callee.applySuper( this, arguments ); |
|---|
| 18 | this.editor = editor; |
|---|
| 19 | this.element.unselectable = "on"; |
|---|
| 20 | }, |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | destroyObject: function() { |
|---|
| 24 | this.editor = null; |
|---|
| 25 | arguments.callee.applySuper( this, arguments ); |
|---|
| 26 | }, |
|---|
| 27 | |
|---|
| 28 | |
|---|
| 29 | /* events */ |
|---|
| 30 | |
|---|
| 31 | eventMouseDown: function( event ) { |
|---|
| 32 | event.stop(); |
|---|
| 33 | }, |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | eventClick: function( event ) { |
|---|
| 37 | var command = this.getMouseEventCommand( event ); |
|---|
| 38 | if( command ) { |
|---|
| 39 | switch( command ) { |
|---|
| 40 | case "toggleMode": |
|---|
| 41 | this.editor.toggleMode(); |
|---|
| 42 | this.editor.focus(); |
|---|
| 43 | break; |
|---|
| 44 | |
|---|
| 45 | case "setModeIframe": |
|---|
| 46 | this.editor.setMode( "iframe" ); |
|---|
| 47 | this.editor.focus(); |
|---|
| 48 | break; |
|---|
| 49 | |
|---|
| 50 | case "setModeTextarea": |
|---|
| 51 | this.editor.setMode( "textarea" ); |
|---|
| 52 | this.editor.focus(); |
|---|
| 53 | break; |
|---|
| 54 | |
|---|
| 55 | case "insertLink": |
|---|
| 56 | var link = this.editor.getSelectedLink(); |
|---|
| 57 | if( link ) |
|---|
| 58 | this.editLink( link ); |
|---|
| 59 | else |
|---|
| 60 | this.createLink(); |
|---|
| 61 | break; |
|---|
| 62 | |
|---|
| 63 | default: |
|---|
| 64 | this.extendedCommand( command, event ); |
|---|
| 65 | } |
|---|
| 66 | } |
|---|
| 67 | return event.stop(); |
|---|
| 68 | }, |
|---|
| 69 | |
|---|
| 70 | |
|---|
| 71 | /** |
|---|
| 72 | * class: <code>Editor.Toolbar</code><br/> |
|---|
| 73 | * Extend this method to add click-handler functionality. |
|---|
| 74 | */ |
|---|
| 75 | extendedCommand: function( command ) { |
|---|
| 76 | this.editor.execCommand( command ); |
|---|
| 77 | }, |
|---|
| 78 | |
|---|
| 79 | |
|---|
| 80 | /* Utilities */ |
|---|
| 81 | /* Toolbar user feature execution */ |
|---|
| 82 | |
|---|
| 83 | initLinkDataBagUrl: function( dataBag ) { |
|---|
| 84 | if( !dataBag || !dataBag.url || !dataBag.url.trim() ) |
|---|
| 85 | return null; |
|---|
| 86 | dataBag.url = dataBag.url.trim(); |
|---|
| 87 | return dataBag; |
|---|
| 88 | }, |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | /** |
|---|
| 92 | * class: <code>Editor.Toolbar</code><br/> |
|---|
| 93 | * Open the <code>createLink</code> dialog, for creating (or editing) a link. |
|---|
| 94 | */ |
|---|
| 95 | createLink: function( url, textSelected, anchor ) { |
|---|
| 96 | var linkedText = ""; |
|---|
| 97 | if( !textSelected ) |
|---|
| 98 | textSelected = this.editor.isTextSelected(); |
|---|
| 99 | if( !url ) |
|---|
| 100 | url = "http://"; |
|---|
| 101 | |
|---|
| 102 | url = prompt( Editor.strings.enterLinkAddress, url ); |
|---|
| 103 | if( !url ) |
|---|
| 104 | return false; |
|---|
| 105 | if( !textSelected ) |
|---|
| 106 | linkedText = prompt( Editor.strings.enterTextToLinkTo, "" ); |
|---|
| 107 | |
|---|
| 108 | this.insertLink( { url: url, linkedText: linkedText, anchor: anchor } ); |
|---|
| 109 | }, |
|---|
| 110 | |
|---|
| 111 | |
|---|
| 112 | /** |
|---|
| 113 | * class: <code>Editor.Toolbar</code><br/> |
|---|
| 114 | * Edit a pre-existing link in the post. (This method leverages <code>createLink</code>). |
|---|
| 115 | * @param linkElement <code>Node</code> A DOM element representing an anchor element. |
|---|
| 116 | */ |
|---|
| 117 | editLink: function( linkElement ) { |
|---|
| 118 | this.createLink( linkElement.href, true, linkElement ); |
|---|
| 119 | }, |
|---|
| 120 | |
|---|
| 121 | |
|---|
| 122 | /** |
|---|
| 123 | * class: <code>Editor.Toolbar</code><br/> |
|---|
| 124 | * Perform final adjustment of data and run the |
|---|
| 125 | * low-level command to put the link in the editor body. |
|---|
| 126 | * Passively returns if no <code>data</code> or no <code>data.url</code>. |
|---|
| 127 | * @param dataBag <code>Object</code> Must contain the properties: |
|---|
| 128 | * <code>linkedText<code>, <code>url</code>, and a callback to the <code>insertLink</code> method. |
|---|
| 129 | */ |
|---|
| 130 | insertLink: function( dataBag ) { |
|---|
| 131 | dataBag = this.initLinkDataBagUrl( dataBag ); |
|---|
| 132 | if( !dataBag ) |
|---|
| 133 | return; |
|---|
| 134 | if( !dataBag.anchor ) { |
|---|
| 135 | if( dataBag.linkedText ) { |
|---|
| 136 | var id = "temp_id_for_retrieving_inserted_element_" + Unique.id(); |
|---|
| 137 | var html = "<a id='" + id + "' href='" + dataBag.url + "'>" + dataBag.linkedText + "</a>"; |
|---|
| 138 | return this.editor.insertHTML( html, true, id, true ); |
|---|
| 139 | } else { |
|---|
| 140 | return this.editor.execCommand( "createLink", false, dataBag.url ); |
|---|
| 141 | } |
|---|
| 142 | } else { |
|---|
| 143 | dataBag.anchor.href = dataBag.url; |
|---|
| 144 | return dataBag.anchor; |
|---|
| 145 | } |
|---|
| 146 | } |
|---|
| 147 | } ); |
|---|