[795] | 1 | // Plugin for htmlArea to run code through the server's HTML Tidy |
---|
| 2 | // By Adam Wright, for The University of Western Australia |
---|
| 3 | // |
---|
| 4 | // Distributed under the same terms as HTMLArea itself. |
---|
| 5 | // This notice MUST stay intact for use (see license.txt). |
---|
| 6 | |
---|
| 7 | function HtmlTidy(editor) { |
---|
| 8 | this.editor = editor; |
---|
| 9 | |
---|
| 10 | var cfg = editor.config; |
---|
| 11 | var tt = HtmlTidy.I18N; |
---|
| 12 | var bl = HtmlTidy.btnList; |
---|
| 13 | var self = this; |
---|
| 14 | |
---|
| 15 | this.onMode = this.__onMode; |
---|
| 16 | |
---|
| 17 | // register the toolbar buttons provided by this plugin |
---|
| 18 | var toolbar = []; |
---|
| 19 | for (var i in bl) { |
---|
| 20 | var btn = bl[i]; |
---|
| 21 | if (btn == "html-tidy") { |
---|
| 22 | var id = "HT-html-tidy"; |
---|
| 23 | cfg.registerButton(id, tt[id], editor.imgURL(btn[0] + ".gif", "HtmlTidy"), true, |
---|
| 24 | function(editor, id) { |
---|
| 25 | // dispatch button press event |
---|
| 26 | self.buttonPress(editor, id); |
---|
| 27 | }, btn[1]); |
---|
| 28 | toolbar.push(id); |
---|
| 29 | } else if (btn == "html-auto-tidy") { |
---|
| 30 | var ht_class = { |
---|
| 31 | id : "HT-auto-tidy", |
---|
| 32 | options : { "Auto-Tidy" : "auto", "Don't Tidy" : "noauto" }, |
---|
| 33 | action : function (editor) { self.__onSelect(editor, this); }, |
---|
| 34 | refresh : function (editor) { }, |
---|
| 35 | context : "body" |
---|
| 36 | }; |
---|
| 37 | cfg.registerDropdown(ht_class); |
---|
| 38 | } |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | for (var i in toolbar) { |
---|
| 42 | cfg.toolbar[0].push(toolbar[i]); |
---|
| 43 | } |
---|
| 44 | }; |
---|
| 45 | |
---|
| 46 | HtmlTidy._pluginInfo = { |
---|
| 47 | name : "HtmlTidy", |
---|
| 48 | version : "1.0", |
---|
| 49 | developer : "Adam Wright", |
---|
| 50 | developer_url : "http://blog.hipikat.org/", |
---|
| 51 | sponsor : "The University of Western Australia", |
---|
| 52 | sponsor_url : "http://www.uwa.edu.au/", |
---|
| 53 | license : "htmlArea" |
---|
| 54 | }; |
---|
| 55 | |
---|
| 56 | HtmlTidy.prototype.__onSelect = function(editor, obj) { |
---|
| 57 | // Get the toolbar element object |
---|
| 58 | var elem = editor._toolbarObjects[obj.id].element; |
---|
| 59 | |
---|
| 60 | // Set our onMode event appropriately |
---|
| 61 | if (elem.value == "auto") |
---|
| 62 | this.onMode = this.__onMode; |
---|
| 63 | else |
---|
| 64 | this.onMode = null; |
---|
| 65 | }; |
---|
| 66 | |
---|
| 67 | HtmlTidy.prototype.__onMode = function(mode) { |
---|
| 68 | if ( mode == "textmode" ) { |
---|
| 69 | this.buttonPress(this.editor, "HT-html-tidy"); |
---|
| 70 | } |
---|
| 71 | }; |
---|
| 72 | |
---|
| 73 | HtmlTidy.btnList = [ |
---|
| 74 | null, // separator |
---|
| 75 | ["html-tidy"], |
---|
| 76 | ["html-auto-tidy"] |
---|
| 77 | ]; |
---|
| 78 | |
---|
| 79 | HtmlTidy.prototype.onGenerateOnce = function() { |
---|
| 80 | var editor = this.editor; |
---|
| 81 | |
---|
| 82 | var ifr = document.createElement("iframe"); |
---|
| 83 | ifr.name = "htiframe_name"; |
---|
| 84 | var s = ifr.style; |
---|
| 85 | s.position = "absolute"; |
---|
| 86 | s.width = s.height = s.border = s.left = s.top = s.padding = s.margin = "0px"; |
---|
| 87 | document.body.appendChild(ifr); |
---|
| 88 | |
---|
| 89 | var frm = '<form id="htiform_id" name="htiform_name" method="post" target="htiframe_name" action="'; |
---|
| 90 | frm += _editor_url + 'plugins/HtmlTidy/html-tidy-logic.php'; |
---|
| 91 | frm += '"><textarea name="htisource_name" id="htisource_id">'; |
---|
| 92 | frm += '</textarea></form>'; |
---|
| 93 | |
---|
| 94 | var newdiv = document.createElement('div'); |
---|
| 95 | newdiv.style.display = "none"; |
---|
| 96 | newdiv.innerHTML = frm; |
---|
| 97 | document.body.appendChild(newdiv); |
---|
| 98 | }; |
---|
| 99 | |
---|
| 100 | HtmlTidy.prototype.buttonPress = function(editor, id) { |
---|
| 101 | var i18n = HtmlTidy.I18N; |
---|
| 102 | |
---|
| 103 | switch (id) { |
---|
| 104 | case "HT-html-tidy": |
---|
| 105 | |
---|
| 106 | var oldhtml = editor.getHTML(); |
---|
| 107 | |
---|
| 108 | // Ask the server for some nice new html, based on the old... |
---|
| 109 | var myform = document.getElementById('htiform_id'); |
---|
| 110 | var txtarea = document.getElementById('htisource_id'); |
---|
| 111 | txtarea.value = editor.getHTML(); |
---|
| 112 | |
---|
| 113 | // Apply the 'meanwhile' text, e.g. "Tidying HTML, please wait..." |
---|
| 114 | editor.setHTML(i18n['tidying']); |
---|
| 115 | |
---|
| 116 | // The returning tidying processing script needs to find the editor |
---|
| 117 | window._editorRef = editor; |
---|
| 118 | |
---|
| 119 | // ...And send our old source off for processing! |
---|
| 120 | myform.submit(); |
---|
| 121 | break; |
---|
| 122 | } |
---|
| 123 | }; |
---|
| 124 | |
---|
| 125 | HtmlTidy.prototype.processTidied = function(newSrc) { |
---|
| 126 | editor = this.editor; |
---|
| 127 | editor.setHTML(newSrc); |
---|
| 128 | }; |
---|