]> git.donarmstrong.com Git - x_full.git/blob - .mozilla/firefox/default/extensions/itsalltext@docwhat.gerf.org/chrome/content/API.js
add itsalltext
[x_full.git] / .mozilla / firefox / default / extensions / itsalltext@docwhat.gerf.org / chrome / content / API.js
1 /*
2   This file is used to allow external editors to work inside your chrome XUL.
3
4   ***** How To Use This API *****
5   Add this script line to your .xul file:
6   <script type="application/javascript" src="chrome://itsalltext/content/API.js"/>
7
8   If "It's All Text!" isn't installed in the browser, it will fail safely.
9   It only generates an info message in the error console.
10
11   You then have two choices.  You can call ItsAllTextopenEditor() directly
12   via JavaScript or you can add one or two attributes to a XUL element and
13   it'll automatically be set up right.
14
15   The suggested method is to add the correct attributes to your XUL button 
16   or menuitem and let "It's All Text!" do it for you.
17
18   Attributes:
19     'itsalltext-control' -- This should be set to the id of the textbox
20                             that you want to edit when command is executed
21                             on this XUL element. This is required.
22     'itsalltext-extension' -- This is the file extension.  Include the
23                               leading dot character.  Example: '.css'
24                               It defaults to '.txt' and is optional.
25
26   If you don't want this XUL element to be visible unless "It's All Text!"
27   is installed, then you should set it's CSS style display to 'none'.
28
29   Example using attributes (recommended method):
30       <hbox>
31         <spacer flex="1"/>
32         <button label="It's All Text!"
33                 itsalltext-control="code"
34                 itsalltext-extension=".css"
35                 style="display: none;"
36         />
37       </hbox>
38
39   Example calling openEditor() directly:
40      if(some_condition && ItsAllText) {
41          ItsAllText.openEditor('id-of-textarea', '.extension');
42      }
43
44  */
45
46 (function () {
47     /* Load up the main It's All Text! file */
48     var objScriptLoader = Components.classes["@mozilla.org/moz/jssubscript-loader;1"].getService(Components.interfaces.mozIJSSubScriptLoader);
49     objScriptLoader.loadSubScript('chrome://itsalltext/content/itsalltext.js');
50
51     var onload = function (event) {
52         /* Start watching the document, but force it. */
53         ItsAllText.monitor.watch(document, true);
54
55         /* Turn on all the hidden CSS */
56         var nodes = [];
57         var nodesIter = document.evaluate("//node()[@itsalltext-control]",
58                                           document, null,
59                                           XPathResult.UNORDERED_NODE_ITERATOR_TYPE, null); 
60
61         var node = nodesIter.iterateNext(); 
62         while (node) {
63             nodes.push(node);
64             node = nodesIter.iterateNext();
65         }
66         var command = function(event) {
67             ItsAllText.openEditor( this.getAttribute("itsalltext-control"),
68                                    this.getAttribute("itsalltext-extension") );
69             return false;
70         };
71         for(i in nodes) {
72             node = nodes[i];
73             node.addEventListener('command', command, true);
74             node.style.display = '-moz-box';
75         }
76
77     };
78     window.addEventListener("load", onload, true);
79 })();
80
81 /**
82  * This is part of the public XUL API.
83  * Use this to open an editor for a specific textarea or textbox with
84  * the id 'id'.  The file will have the extension 'extension'.  Include 
85  * the leading dot in the extension.
86  * @param {String} id The id of textarea or textbody that should be opened in the editor.
87  * @param {String} extension The extension of the file used as a temporary file. Example: '.css' (optional) 
88  */
89 ItsAllText.openEditor = function(id, extension) {
90     var node = document.getElementById(id);
91     /* The only way I can adjust the background of the textbox is
92      * to turn off the -moz-appearance attribute.
93      */
94     node.style.MozAppearance = 'none';
95     var cache_object = node && ItsAllText.getCacheObj(node);
96     if(!cache_object) { return; }
97     cache_object.edit(extension);
98 };
99