home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2005 March / PCpro_2005_03.ISO / files / firefox / editcss0.3.1.xpi / chrome / editcss.jar / content / editcss / editcss.js < prev    next >
Encoding:
JavaScript  |  2004-12-17  |  8.3 KB  |  268 lines

  1. /******************************************************************************
  2. * Version: MPL 1.1
  3. *
  4. * The contents of this file are subject to the Mozilla Public License Version
  5. * 1.1 (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. * http://www.mozilla.org/MPL/
  8. *
  9. * Software distributed under the License is distributed on an "AS IS" basis,
  10. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11. * for the specific language governing rights and limitations under the
  12. * License.
  13. *
  14. * The Original Code is EditCSS.
  15. * Portions of the Original Code are  taken from
  16. * - quicknote.mozdev.org
  17. * - tagzilla.mozdev.org
  18. * -'Edit Styles' bookmarklet from Jesse Ruderman http://www.squarefree.com/bookmarklets/webdevel.html#edit_styles
  19. *
  20. * Code uses jslib (jslib.mozdev.org)
  21. *
  22. *
  23. * The Initial Developer of the Original Code is Pascal Guimier.
  24. * Portions created by the Initial Developer are Copyright (C) 2003
  25. * the Initial Developer. All Rights Reserved.
  26. *
  27. * Contributor(s): Gerard, Jonathan Gough
  28.  
  29. ******************************************************************************/
  30.  
  31. var countLoads = 0;
  32. var dataHistory = new Array();
  33. var mediaList = new Array();
  34. var dataOrig;
  35. function init() {
  36.  
  37.   var newline = unescape("%0A"), importCount = 0, L = [];
  38.   dead = false;
  39.   oldCSS = null;
  40.   x = window._content;
  41.   ta = window.document.getElementById('ecText');
  42.   ta.select();
  43.    
  44.   if (x.editStyles) {
  45.     ta.value = x.editStyles.innerHTML; 
  46.     update(); 
  47.     return; 
  48.     }
  49.     
  50.     ta.value = "/* Edited with EditCSS */" + newline; 
  51.     /* Add a stylesheet to the panel */
  52.     function add(s){
  53.       /* test the media type */
  54.       var isscreen=s.media.length==0?true:false;
  55.       for(var j=0;j<s.media.length;j++)
  56.         if(s.media.item(j)=="screen")
  57.       isscreen=true;
  58.       var mediaName=s.media.item(j);
  59.       if(!s.disabled&&isscreen){
  60.         var y = {sheet:s, readable:true, label:"Imported", inline:false, shorturl:"", fulltext:""};
  61.         try { for (var k = 0, m; m = s.cssRules[k]; ++k) { if (m.type == 3) { add(m.styleSheet); } } }
  62.         catch (er) { y.readable = false; }
  63.         L.push(y);
  64.  
  65.         if (s.ownerNode) {
  66.           y.label = s.ownerNode.tagName.toUpperCase() + "-tag";
  67.  
  68.           if (!s.ownerNode.getAttribute("src") && !s.ownerNode.href) {y.inline = true; }
  69.         }
  70.  
  71.         if (y.inline) {
  72.           y.label = "Inline " + y.label; y.fulltext = fix(s.ownerNode.innerHTML);
  73.         }
  74.         else {
  75.  
  76.           if (s.href.substr(0, 13) == "data:text/css") {
  77.             y.shorturl = " contained in a data: URL"; y.fulltext = fix(unescape(s.href.slice(14)));
  78.           }
  79.           else {
  80.             ++importCount;
  81.             y.importtext = "@import \"" + s.href + "\";";
  82.             y.shorturl = " " + s.href.split("/").reverse()[0];
  83.  
  84.             if (!y.readable) {
  85.               y.fulltext = "/* Out-of-domain; imported above. */";
  86.             }
  87.             else {
  88.               
  89.               if (s.href.substr(0, 4) != "http") {
  90.                 y.fulltext = "/* Non-http; imported above. */";
  91.               }
  92.               else {
  93.                 
  94.                 var loadingText = "/* Loading (" + (L.length - 1) + ") */";
  95.                 y.fulltext = loadingText;
  96.                 var p = new XMLHttpRequest();
  97.                 p.onload = ( function (e) {
  98.                     ta.value = ta.value.replace(y.importtext + newline, "");
  99.                     y.fulltext = p.responseText;
  100.                     ta.value = ta.value.replace(loadingText, fix(y.fulltext));
  101.                     ta.value = ta.value.replace(firstNote + newline, "");
  102.                     }
  103.                 );
  104.                 p.open("GET", s.href); 
  105.                 p.send(null); 
  106.               }
  107.             }
  108.           }
  109.         }
  110.       } 
  111.     }
  112.     
  113.     function fix(s) {
  114.       
  115.       while ((s[0] == newline) && s.length > 1) {
  116.         s = s.slice(1);
  117.       }
  118.       
  119.       while ((s[s.length - 1] == newline) && s.length > 1) {
  120.         s = s.substr(0, s.length - 1);
  121.       }
  122.  
  123.       s = s.replace(/@import.*;/gi, (function () { return "/* " + RegExp.lastMatch + " */"; }));
  124.       return s;
  125.     }
  126.  
  127.     for (var i = 0, ss; ss = x.document.styleSheets[i]; ++i) { add(ss); }
  128.  
  129.     var imports = "", main = "";
  130.     var firstNote = "/**** Style sheets whose contents could be loaded were ****/" + newline +
  131.                     "/**** imported instead. Rule order may be incorrect ****/" + newline +
  132.                     "/**** as a result. ****/" + newline;
  133.  
  134.     if (importCount) {
  135.         ta.value += firstNote;
  136.     }
  137.  
  138.     for (var i = 0; ss = L[i]; ++i) {
  139.       
  140.       if (ss.importtext) {
  141.         imports += ss.importtext + newline;
  142.       }
  143.       
  144.       main += "/**** " + ss.label + " style sheet" + ss.shorturl + " ****/" + newline;
  145.       main += newline;
  146.       main += ss.fulltext;
  147.       main += newline;
  148.       main += newline;
  149.       main += newline;
  150.     }
  151.     
  152.     ta.value += imports + newline + main;
  153.     /*dataHistory[countLoads] = ta.value;*/
  154.     dataOrig = getTextareaText('ecText');
  155.     countLoads++;
  156.     update();
  157. }
  158. function done(){
  159.     notSavedDlg();
  160. }
  161.  
  162. function update() {
  163.   /* first, test current selected tab corresponds withe the original loaded page */
  164.   try {
  165.     if (!x || x.closed) {
  166.       ta.style.backgroundColor = "#ddd";
  167.       return;
  168.     }
  169.     
  170.     x.editStyles;
  171.   }
  172.   catch (er) {
  173.     ta.style.backgroundColor = "#fdc";
  174.     setTimeout(update, 150);
  175.     dead = true;
  176.     return;
  177.   }
  178.  
  179.   if (dead) {
  180.     dead = false;
  181.     ta.style.backgroundColor = "";
  182.     oldCSS = null;
  183.   }
  184.   /*  */ 
  185.   if (!x.editStyles) {
  186.     var newSS;
  187.     newSS = x.document.createElement("style");
  188.     newSS.type = "text/css";
  189.     x.document.getElementsByTagName("head")[0].appendChild(newSS);
  190.     x.editStyles = newSS;
  191.     oldCSS = null;
  192.     
  193.     for (var i = 0, ss; ss = x.document.styleSheets[i]; ++i) {
  194.       ss.disabled = true;
  195.     }
  196.  
  197.   }
  198.  
  199.   if (oldCSS != ta.value) {
  200.     oldCSS = ta.value;
  201.     x.editStyles.innerHTML = ta.value;
  202.   }
  203.  
  204.   setTimeout(update, 150); 
  205.  
  206. }
  207. function resetContent(idx, tNodeID) {
  208.   /*var fContents = dataHistory[idx];*/
  209.   /*setTextareaText(fContents,tNodeID);*/
  210.   setTextareaText(dataOrig,tNodeID);
  211. }
  212.  
  213. function closeWindow() {
  214.   window.close();
  215. }
  216. function getTextareaText(tNodeID) {
  217.   var tat = document.getElementById(tNodeID).value
  218.   return tat
  219. }
  220. function setTextareaText(thetext,tNodeID) {
  221.   document.getElementById(tNodeID).value= thetext
  222. }
  223.  
  224. /* Easyer FilePicker */
  225. function txtFilePicker(aTitle, aSave) {
  226.   var retVal = null;
  227.   try {
  228.     const nsIFilePicker = Components.interfaces.nsIFilePicker;
  229.     var fp = Components.classes["@mozilla.org/filepicker;1"].createInstance(nsIFilePicker);
  230.     fp.init(window, aTitle, (aSave ? nsIFilePicker.modeSave : nsIFilePicker.modeOpen));
  231.     fp.appendFilter("Stylesheet","*.css");
  232.     fp.appendFilters(nsIFilePicker.filterAll | nsIFilePicker.filterText);
  233.     var result=fp.show();
  234.     if (result == nsIFilePicker.returnOK || result == nsIFilePicker.returnReplace) {
  235.       retVal=fp.file;
  236.     }
  237.   }
  238.   catch (ex) {
  239.   }
  240.   return retVal;
  241. }
  242.  
  243. /* Saves the CSS file */
  244. function saveTextAs(tNodeID) {
  245.   const outputStream = Components.classes["@mozilla.org/network/file-output-stream;1"].createInstance(Components.interfaces.nsIFileOutputStream);
  246.   var data = getTextareaText(tNodeID);
  247.   var theFile = txtFilePicker("Save the File as",1);
  248.   if(!theFile.exists()){
  249.     theFile.create(Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 444);
  250.   } 
  251.   outputStream.init(theFile, 0x04, 0444, null);
  252.   outputStream.write(data, data.length);
  253.   outputStream.close();
  254. }
  255. /* Loads a CSS file */
  256. function loadFile(tNodeID) {
  257.   const inputStream = Components.classes["@mozilla.org/network/file-input-stream;1"].createInstance(Components.interfaces.nsIFileInputStream);
  258.   const scriptableStream = Components.classes["@mozilla.org/scriptableinputstream;1"].createInstance(Components.interfaces.nsIScriptableInputStream);
  259.   var theFile=txtFilePicker("Open a CSS File",0);
  260.   inputStream.init(theFile, 0x01, 0444, null);
  261.   scriptableStream.init(inputStream);
  262.   var fContents = scriptableStream.read(scriptableStream.available());
  263.   scriptableStream.close();
  264.   inputStream.close();
  265.   /*var dataHistory[countLoads] = fContents;
  266.   countLoads++;*/
  267.   setTextareaText(fContents,tNodeID);
  268. }