home *** CD-ROM | disk | FTP | other *** search
/ PC User 2003 December / Australian PC User - December 2003 (CD2).iso / software / apps / files / dwmx2k4.exe / Disk1 / data1.cab / Configuration_En / Objects / Common / E-Mail Link.js < prev    next >
Encoding:
JavaScript  |  2003-09-05  |  4.2 KB  |  127 lines

  1. // Copyright 2000, 2001, 2002, 2003 Macromedia, Inc. All rights reserved
  2.  
  3. var helpDoc = MM.HELP_objEMailLink;
  4.  
  5. //---------------   GLOBAL VARIABLES   ---------------
  6.  
  7. var gDialogShown = false;
  8. var gDefaultEmail =  LABEL_DefaultEmail;
  9.  
  10. //---------------     API FUNCTIONS    ---------------
  11.  
  12. function isDOMRequired() { 
  13.     // Return false, indicating that this object is available in code view.
  14.     return false;
  15. }
  16.  
  17.  
  18. // we need to extract the entity encoded version of the text
  19. function getInputHTML(inputField)
  20. {
  21.     var inputHTML = inputField.innerHTML;
  22.     var start = inputHTML.indexOf('value="');
  23.     var end = inputHTML.indexOf('"',start + 7);
  24.     var valueHTML = inputHTML.substring(start + 7,end);
  25.     if (start > -1 && end > start)
  26.         return valueHTML;
  27.     else
  28.         return inputField.value;
  29. }
  30.  
  31.  
  32. function objectTag() {
  33.   var EmailFld;
  34.   var InsertText;
  35.   
  36.   if (gDialogShown) {
  37.     EmailFld = document.MainForm.EmailFld.value;
  38.     InsertText = getInputHTML(document.MainForm.TextFld);
  39.  
  40.     // Save preferences.
  41.     savePreferences();
  42.   } else {
  43.     populateFields();
  44.     EmailFld = document.MainForm.EmailFld.value;
  45.     if (!EmailFld) EmailFld = gDefaultEmail;
  46.  //   InsertText = getInputHTML(document.MainForm.TextFld);
  47.   }
  48.  
  49.   if (!InsertText) InsertText = EmailFld;
  50.   var rtnString = '<a href="mailto:' + EmailFld + '">' + InsertText + '<'+'/a>'
  51.  
  52.   var curDOM = dw.getDocumentDOM('document');
  53.   if (dw.getFocus() != 'textView' && dw.getFocus(true) != 'html' && isCurSelectionTextOnly(curDOM)) {
  54.     curDOM.insertHTML(rtnString, true); // Replaces current selection.
  55.     rtnString = ''; // Set return value to empty, tag already inserted.
  56.   }
  57.   gDialogShown = false; // Reset show dialog global.
  58.   return rtnString;
  59. }
  60.  
  61. //---------------    LOCAL FUNCTIONS   ---------------
  62.  
  63. // Description: Determines if the current selection is contained within a text node.
  64. // Parameters:  DOM - checked for valid, returns false if no DOM.
  65. function isCurSelectionTextOnly(curDOM) {
  66.   var rtnBool = false;
  67.   if (curDOM != null) {
  68.     var curNode = curDOM.getSelectedNode();
  69.     if (curNode.nodeType == Node.TEXT_NODE) { // Return true if we are a text node.
  70.       rtnBool = true;
  71.     } else { // Return true if the selection contains a single text node.
  72.       if (curNode.hasChildNodes() && curNode.childNodes[0].nodeType == Node.TEXT_NODE) {
  73.         var curSel = curDOM.getSelection();
  74.         var nodeOffset = dw.nodeToOffsets(curNode.childNodes[0]);
  75.         if  ((nodeOffset[0] <= curSel[0]) && (curSel[1] <= nodeOffset[1])) {
  76.           rtnBool = true;
  77.   } } } }
  78.   return rtnBool;
  79. }
  80.  
  81. function populateFields() {
  82.   var metaFile, curVal;
  83.   if (typeof MMNotes == 'undefined') {return;} // Check for MMNotes extension.
  84.   metaFile = MMNotes.open(document.URL, false);
  85.   if (metaFile) {
  86.     // If the user entered an e-mail address before, it's been saved.
  87.     // Use that value to populate the E-mail field.
  88.     curVal = MMNotes.get(metaFile, 'MM_pref_E-Mail');
  89.     if (curVal) document.MainForm.EmailFld.value = curVal;
  90.     MMNotes.close(metaFile);
  91.   }
  92.   
  93.   // Populate the Text field with the currently-selected text, if
  94.   // applicable.
  95.   var curDOM = dw.getDocumentDOM('document');
  96.   if (isCurSelectionTextOnly(curDOM)) {
  97.     var curSel = curDOM.getSelection();
  98.     var selText = curDOM.documentElement.outerHTML.slice(curSel[0],curSel[1]);
  99.     // replace carriage returns (and the space on either side of any carriage returns, 
  100.     // if one exists) with a single space.
  101.     selText = selText.replace(/\s*[\n\r]+\s*/g, ' ');
  102.     document.MainForm.TextFld.value = entityNameDecode(selText);
  103.     if (selText.match(/\w+(\.\w+)?@\w+\.\w+/)){
  104.       document.MainForm.EmailFld.value = selText;
  105.     }
  106.   }
  107. }
  108.  
  109. function savePreferences() {
  110.   if (typeof MMNotes == 'undefined') {return;} // Check for MMNotes extension.
  111.   var metaFile, curVal;
  112.   metaFile = MMNotes.open(document.URL, true);
  113.   if (metaFile) {
  114.     curVal = MMNotes.set(metaFile, 'MM_pref_E-Mail', document.MainForm.EmailFld.value);
  115.     MMNotes.close(metaFile);
  116.   }
  117. }
  118.  
  119. function initUI() {
  120.   // Initialize the form dialog.
  121.   populateFields();
  122.  
  123.   document.MainForm.TextFld.focus(); //set focus on textbox
  124.   document.MainForm.TextFld.select(); //set insertion point into textbox
  125.   gDialogShown = true;
  126. }
  127.