home *** CD-ROM | disk | FTP | other *** search
/ Practical Internet Web Designer 86 / PIWD86.iso / pc / contents / dreamweaver / software / dwmx2004.exe / Disk1 / data1.cab / Configuration_En / Commands / PMContext.js < prev    next >
Encoding:
JavaScript  |  2003-09-05  |  12.2 KB  |  419 lines

  1. //=========================================================================================================
  2. //
  3. // Copyright 2002 Macromedia, Inc. All rights reserved.
  4. //
  5. // Feature: Paste Fix
  6. // Author:  JDH
  7. // Module:  PMContext.js
  8. // Purpose:    The context object for the Paste Fix pipeline, as well as wrappers for CSS class access.
  9. // Updates:
  10. //    5/17/02 - Started file control
  11. //
  12. //=========================================================================================================
  13.  
  14. function CSSClassCollection( targetCSS )
  15. {
  16.     // Allocate an associate array to hold the classes
  17.     this.targetCSS = targetCSS;
  18.     this.classes = new Array();
  19.     this.referenced = new Array();
  20.     this.newToOld = new Array();
  21.     this.oldToNew = new Array();
  22.     this.tdClassName = "";
  23. }
  24.  
  25. CSSClassCollection.prototype.add = CSSClassCollection_add;
  26. CSSClassCollection.prototype.get = CSSClassCollection_get;
  27. CSSClassCollection.prototype.has = CSSClassCollection_has;
  28. CSSClassCollection.prototype.ref = CSSClassCollection_ref;
  29. CSSClassCollection.prototype.create_used = CSSClassCollection_create_used;
  30. CSSClassCollection.prototype.remapClasses = CSSClassCollection_remapClasses;
  31. CSSClassCollection.prototype.newClassName = CSSClassCollection_newClassName;
  32. CSSClassCollection.prototype.getTDClassName = CSSClassCollection_getTDClassName;
  33.  
  34. function CSSClassCollection_getTDClassName()
  35. {
  36.     return this.tdClassName;
  37. }
  38.  
  39. function CSSClassCollection_newClassName()
  40. {
  41.     // Iterate through the possible class names to find a new excel
  42.     // name
  43.     for( var index = 1; ; index++ )
  44.     {
  45.         var name = "excel" + index;
  46.         var dotName = ".excel" + index;
  47.         if ( this.targetCSS.has( name ) == false &&
  48.              this.targetCSS.has( dotName ) == false && 
  49.              this.newToOld[ dotName ] == null )
  50.         {
  51.             return dotName;
  52.         }
  53.     }
  54. }
  55.  
  56. function CSSClassCollection_remapClasses()
  57. {
  58.     // Create maps for the old excel name to the new excel name
  59.     // and vice versa
  60.     for( var key in this.classes )
  61.     {
  62.         if ( key.match( /^[.]xl/ ) || key == "td" )
  63.         {
  64.             var newName = this.newClassName();
  65.  
  66.             if ( key == "td" )
  67.             {
  68.                 this.tdClassName = newName.replace( /^./, "" );
  69.                 this.referenced[ newName ] = 1;
  70.             }
  71.                     
  72.             this.newToOld[ newName ] = key;
  73.             this.oldToNew[ key ] = newName;
  74.         }
  75.     }
  76.  
  77.     // Copy the TD styles into each .xl class
  78.     var tdStyle = this.classes[ 'td' ];
  79.     for( var key in this.classes )
  80.     {
  81.         if ( key.match( /^[.]xl/ ) )
  82.         {
  83.             for( var tdKey in tdStyle )
  84.             {
  85.                 if ( this.classes[ key ][ tdKey ] == null )
  86.                     this.classes[ key ][ tdKey ] = tdStyle[ tdKey ];
  87.             }
  88.         }
  89.     }
  90. }
  91.  
  92. function CSSClassCollection_add( name, data )
  93. {
  94.     // Coerce the name to lower case and add the data
  95.  
  96.     this.classes[ name ] = data;
  97. }
  98.  
  99. function CSSClassCollection_get( name )
  100. {
  101.     // Coerce the name to lower caes and return the class definition
  102.  
  103.     return this.classes[ name ];
  104. }
  105.  
  106. function CSSClassCollection_has( name )
  107. {
  108.     // Coerce the name to lower case and return true if there is a 
  109.     // class definition
  110.  
  111.     if ( this.classes[ name ] != null )
  112.         return true;
  113.     return false;
  114. }
  115.  
  116. function CSSClassCollection_ref( tag, name )
  117. {
  118.     // Check to see if this is a remapped name
  119.     if ( this.oldToNew[ name ] )
  120.     {
  121.         this.referenced[ this.oldToNew[ name ] ] = 1;
  122.         return this.oldToNew[ name ];
  123.     }
  124.  
  125.     // Check to see if this is a remapped to a dot-name
  126.     if ( this.oldToNew[ "." + name ] )
  127.     {
  128.         this.referenced[ this.oldToNew[ "." + name ] ] = 1;
  129.         var styleName = this.oldToNew[ "." + name ];
  130.         styleName = styleName.replace( /^./, "" );
  131.         return styleName;
  132.     }
  133.  
  134.     // Check to see if this is an unstyle td that needs a class
  135.     if ( tag == "td" && name == "" )
  136.     {
  137.         this.referenced[ this.oldToNew[ "td" ] ] = 1;
  138.         styleName = this.oldToNew[ "td" ];
  139.         styleName = styleName.replace( /^./, "" );
  140.         return styleName;
  141.     }
  142.  
  143.     // Ignore if this is already referenced
  144.     if ( this.referenced[ name ] )
  145.         return name;
  146.  
  147.     /// Check the basic name
  148.     if ( this.classes[ name ] )
  149.     {
  150.         this.referenced[ name ] = 1;
  151.         return name;
  152.     }
  153.  
  154.     /// Check the dot-name
  155.     var dirName = tag+"."+name;
  156.     if ( this.classes[ dirName ] )
  157.     {
  158.         this.referenced[ dirName ] = 1;
  159.         return name;
  160.     }
  161.  
  162.     return name;
  163. }
  164.  
  165. function CSSClassCollection_create_used( )
  166. {
  167.     // Create text for all of the new classes
  168.  
  169.     var output = "";
  170.     for ( var name in this.referenced )
  171.     {
  172.         var style = this.classes[ name ];
  173.         if( this.newToOld[ name ] )
  174.             style = this.classes[ this.newToOld[ name ] ];
  175.  
  176.         output += name + " {\n" + Utils_BuildClass( style ) + "}\n";
  177.     }
  178.     return output;
  179. }
  180.  
  181.  
  182. function CSSReferenceClassCollection()
  183. {
  184.     // Initialize associate array for the class names
  185.  
  186.     this.classes = new Array();
  187. }
  188.  
  189. CSSReferenceClassCollection.prototype.add = CSSReferenceClassCollection_add;
  190. CSSReferenceClassCollection.prototype.get = CSSReferenceClassCollection_get;
  191. CSSReferenceClassCollection.prototype.has = CSSReferenceClassCollection_has;
  192.  
  193. function CSSReferenceClassCollection_add( name )
  194. {
  195.     // Coerce the name to lower case and add a reference to it to the associative
  196.     // array
  197.  
  198.     name = name.toLowerCase();
  199.     this.classes[ name ] = 1;
  200. }
  201.  
  202. function CSSReferenceClassCollection_get( name )
  203. {
  204.     // We shouldn't be trying to get the class definition, since we don't have
  205.     // them for the target document
  206.  
  207.     alert( "CSSReferenceClassCollection.get( " + name + " ) called" );
  208. }
  209.  
  210. function CSSReferenceClassCollection_has( name )
  211. {
  212.     // Coerce the name to lower case and check for it's existince in the array
  213.  
  214.     name = name.toLowerCase();
  215.     if ( this.classes[ name ] != null )
  216.         return true;
  217.     return false;
  218. }
  219.  
  220.  
  221. function JSStringBuffer() { this._text = ""; }
  222.  
  223. JSStringBuffer.prototype.append = JSStringBuffer_append;
  224. JSStringBuffer.prototype.get = JSStringBuffer_get;
  225.  
  226. function JSStringBuffer_get( ) { return this._text; }
  227. function JSStringBuffer_append( str ) { this._text += str; }
  228.  
  229.  
  230.  
  231. function JSDWBuffer() { this._id = dw.createStringBuffer(); }
  232.  
  233. JSDWBuffer.prototype.append = JSDWBuffer_append;
  234. JSDWBuffer.prototype.get = JSDWBuffer_get;
  235.  
  236. function JSDWBuffer_get( ) { return dw.getStringBuffer( this._id ); }
  237. function JSDWBuffer_append( str ) { dw.appendToStringBuffer( this._id, str ); }
  238.  
  239.  
  240.  
  241. function PasteContext( clipDOM, clipCSS, targetDOM, targetCSS, settings )
  242. {
  243.     // Initialize string buffers
  244.     dw.resetStringBuffers();
  245.  
  246.     // Initialize the member variables
  247.  
  248.     // The clipboard and it's current type (assumend to be HTML)
  249.     this.clipDOM                  = clipDOM;
  250.     this.clipText                 = this.clipDOM.documentElement.outerHTML;
  251.     this.contentType              = CONTENT_TYPE_HTML;
  252.     this.originClasses            = clipCSS;
  253.  
  254.     // The target
  255.     this.targetDOM                = targetDOM;
  256.     this.targetClasses            = targetCSS;
  257.     this.remapClasses             = false;
  258.  
  259.     // The information about the current application
  260.     this.originApplicationFull    = "";
  261.     this.originApplication        = null;
  262.     this.originApplicationVersion = null;
  263.  
  264.     // The global settings for the handlers
  265.     this.settings                 = settings;
  266.  
  267.     // The debug data
  268.     this.debugData                = new Array();
  269.  
  270.     // The meta tags
  271.     this.metaTags                 = {};
  272. }
  273.  
  274. PasteContext.prototype.updateClipDOM = PasteContext_updateClipDOM;
  275. PasteContext.prototype.getClipDOM = PasteContext_getClipDOM;
  276. PasteContext.prototype.setClipText = PasteContext_setClipText;
  277. PasteContext.prototype.getClipText = PasteContext_getClipText;
  278. PasteContext.prototype.getDebugText = PasteContext_getDebugText;
  279. PasteContext.prototype.getDebugHTML = PasteContext_getDebugHTML;
  280. PasteContext.prototype.getContentType = PasteContext_getContentType;
  281. PasteContext.prototype.getOriginApplicationFull = PasteContext_getOriginApplicationFull;
  282. PasteContext.prototype.getOriginApplication = PasteContext_getOriginApplication;
  283. PasteContext.prototype.getOriginApplicationVersion = PasteContext_getOriginApplicationVersion;
  284. PasteContext.prototype.setOriginApplicationFull = PasteContext_setOriginApplicationFull;
  285. PasteContext.prototype.setOriginApplication = PasteContext_setOriginApplication;
  286. PasteContext.prototype.setOriginApplicationVersion = PasteContext_setOriginApplicationVersion;
  287. PasteContext.prototype.addToDebug = PasteContext_addToDebug;
  288. PasteContext.prototype.debugWarning = PasteContext_debugWarning;
  289. PasteContext.prototype.debugInformation = PasteContext_debugInformation;
  290. PasteContext.prototype.debugCritical = PasteContext_debugCritical;
  291. PasteContext.prototype.getSetting = PasteContext_getSetting;
  292. PasteContext.prototype.setSetting = PasteContext_setSetting;
  293. PasteContext.prototype.settingDefined = PasteContext_settingDefined;
  294. PasteContext.prototype.getOriginClasses = PasteContext_getOriginClasses;
  295. PasteContext.prototype.getTargetClasses = PasteContext_getTargetClasses;
  296. PasteContext.prototype.setMeta = PasteContext_setMeta;
  297. PasteContext.prototype.getMeta = PasteContext_getMeta;
  298. PasteContext.prototype.createStringBuffer = PasteContext_createStringBuffer;
  299. PasteContext.prototype.checkClasses = PasteContext_checkClasses;
  300.  
  301. function PasteContext_setMeta( key, value ) { this.metaTags[ key ] = value; }
  302.  
  303. function PasteContext_getMeta( key ) { return this.metaTags[ key ]; }
  304.  
  305. function PasteContext_updateClipDOM( )
  306. {
  307.     this.clipDOM.documentElement.outerHTML = this.clipText;
  308. }
  309.  
  310. function PasteContext_getClipDOM( )
  311. {
  312.     this.updateClipDOM();
  313.     return this.clipDOM;
  314. }
  315.  
  316. function PasteContext_setClipText( html )
  317. {
  318.     this.clipText = html;
  319. }
  320.  
  321. function PasteContext_getClipText( )
  322. {
  323.     return this.clipText;
  324. }
  325.  
  326. function PasteContext_getTargetDOM( ) { return this.targetDOM; }
  327.  
  328. function PasteContext_getContentType( ) { return this.contentType; }
  329.  
  330. function PasteContext_getDebugText( )
  331. {
  332.     // Build a text string of the debug data generated during the run
  333.  
  334.     var debugText = "";
  335.  
  336.     for ( var index in this.debugData )
  337.     {
  338.         var item = this.debugData[ index ];
  339.         debugText += item.module + " : " + item.text + "\n";
  340.     }
  341.  
  342.     return debugText;
  343. }
  344.  
  345. function PasteContext_getDebugHTML( )
  346. {
  347.     // Build an HTML table that has the debug data from the current run
  348.  
  349.     var debugText = "<table>";
  350.  
  351.     for ( var index in this.debugData )
  352.     {
  353.         var item = this.debugData[ index ];
  354.         debugText += "<tr><td>" + item.module + "</td><td>" + item.level + "</td><td>" + item.text + "</td></tr>\n";
  355.     }
  356.  
  357.     debugText += "</table>";
  358.  
  359.     return debugText;
  360. }
  361.  
  362. function PasteContext_addToDebug ( level, module, text )
  363. {
  364.     this.debugData.push( { level: level, module: module, text: text } );
  365. }
  366.  
  367. function PasteContext_debugWarning ( module, text ) { this.addToDebug( DEBUG_WARNING, module, text ); }
  368.  
  369. function PasteContext_debugInformation ( module, text ) { this.addToDebug( DEBUG_INFORMATION, module, text ); }
  370.  
  371. function PasteContext_debugCritical ( module, text ) { this.addToDebug( DEBUG_CRITICAL, module, text ); }
  372.  
  373. function PasteContext_getOriginApplicationFull() { return this.originApplicationFull; }
  374.  
  375. function PasteContext_getOriginApplication() { return this.originApplication; }
  376.  
  377. function PasteContext_getOriginApplicationVersion() { return this.originApplicationVersion; }
  378.  
  379. function PasteContext_setOriginApplication( name )
  380. {
  381.     if ( name == "excel" && this.settingDefined( SETTINGS_CREATE_CLASSES ) )
  382.     {
  383.         this.originClasses.remapClasses();
  384.         this.remapClasses = true;
  385.     }
  386.     this.originApplication = name;
  387. }
  388.  
  389. function PasteContext_setOriginApplicationVersion( version ) { this.originApplicationVersion = version; }
  390.  
  391. function PasteContext_setOriginApplicationFull( name ) { this.originApplicationFull = name; }
  392.  
  393. function PasteContext_getSetting( name ) { return this.settings[ name ]; }
  394.  
  395. function PasteContext_settingDefined( name ) { return ( this.settings[ name ] != null ) ? true : false; }
  396.  
  397. function PasteContext_setSetting( name, value ) { this.settings[ name ] = value; }
  398.  
  399. function PasteContext_getOriginClasses( ) { return this.originClasses; }
  400.  
  401. function PasteContext_getTargetClasses( ) { return this.targetClasses; }
  402.  
  403. function PasteContext_createStringBuffer()
  404. {
  405. //    return new JSStringBuffer();
  406.     return new JSDWBuffer();
  407. }
  408.  
  409. function PasteContext_checkClasses( tag, className )
  410. {
  411.     if ( this.targetClasses.has( className ) )
  412.         return className;
  413.  
  414.     if ( !this.settingDefined( SETTINGS_CREATE_CLASSES ) )
  415.         return null;
  416.  
  417.     return this.originClasses.ref( tag, className );
  418. }
  419.