home *** CD-ROM | disk | FTP | other *** search
Wrap
// // Copyright 2000, 2001, 2002, 2003 Macromedia, Inc. All rights reserved. // // ---------------------------------------------------- // // Date.js // //This object inserts the current date and optionally time //and day at the current insertion point. An Update Automatically //On Save Option will update the date when the document is saved. // // //Localization Note: //Read the comments in the initGlobals function for instructions //on controlling the order of the date formats in the Date Format list, //as well as specifying that certain date formats should not appear //in a localized language. //*********************GLOBAL VARIABLES*********************** //see initGlobals for info on global vars var helpDoc = MM.HELP_objDate; //global arrays var GarrDateFormats; var GarrDayFormats; var GarrTimeFormats; //form element objects var GlistDateFormats; var GselDayFormats; var GselTimeFormats; var GcbUpdate; //global strings needed for accessor functions var GstrFullDate; var GstrHTMLFullDate; var GstrDateID; function initGlobals(){ //The GarrDateFormats array controls the order the date formats are shown in the UI //The list of formats is in Date.htm GarrDateFormats = LIST_DateFormats; //The GarrDateFormats array controls the order the day formats are shown in the UI //The list of formats is in Date.htm GarrDayFormats = LIST_DayFormats; //The GarrTimeFormats array controls the order of the time formats. //The list of formats is in Date.htm GarrTimeFormats = LIST_TimeFormats; //----------------------------------------------------------------------------- //initialize global form elements theForm = document.forms[0]; GlistDateFormats = theForm.DateFormats; GselDayFormats = theForm.DayFormats; GselTimeFormats = theForm.TimeFormats; GcbUpdate = theForm.Update; } function charToEntity(thechar,charCode) { switch(thechar) { case '\x80' : return "€"; case '\xA1' : return "¡"; case '\xA2' : return "¢"; case '\xA3' : return "£"; case '\xA4' : return "¤"; case '\xA5' : return "¥"; case '\xA6' : return "¦"; case '\xA7' : return "§"; case '\xA8' : return "¨"; case '\xA9' : return "©"; case '\xAA' : return "ª"; case '\xAB' : return "«"; case '\xAC' : return "¬"; case '\xAD' : return ""; case '\xAE' : return "®"; case '\xAF' : return "¯"; case '\xB0' : return "°"; case '\xB1' : return "±"; case '\xB2' : return "²"; case '\xB3' : return "³"; case '\xB4' : return "´"; case '\xB5' : return "µ"; case '\xB6' : return "¶"; case '\xB7' : return "·"; case '\xB8' : return "¸"; case '\xB9' : return "¹"; case '\xBA' : return "º"; case '\xBB' : return "»"; case '\xBC' : return "¼"; case '\xBD' : return "½"; case '\xBE' : return "¾"; case '\xBF' : return "¿"; case '\xD7' : return "×"; case '\xF7' : return "÷"; case '\xC6' : return "Æ"; case '\xC1' : return "Á"; case '\xC2' : return "Â"; case '\xC0' : return "À"; case '\xC5' : return "Å"; case '\xC3' : return "Ã"; case '\xC4' : return "Ä"; case '\xC7' : return "Ç"; case '\xD0' : return "Ð"; case '\xC9' : return "É"; case '\xCA' : return "Ê"; case '\xC8' : return "È"; case '\xCB' : return "Ë"; case '\xCD' : return "Í"; case '\xCE' : return "Î"; case '\xCC' : return "Ì"; case '\xCF' : return "Ï"; case '\xD1' : return "Ñ"; case '\xD3' : return "Ó"; case '\xD4' : return "Ô"; case '\xD2' : return "Ò"; case '\xD8' : return "Ø"; case '\xD5' : return "Õ"; case '\xD6' : return "Ö"; case '\xDE' : return "Þ"; case '\xDA' : return "Ú"; case '\xDB' : return "Û"; case '\xD9' : return "Ù"; case '\xDC' : return "Ü"; case '\xDD' : return "Ý"; case '\xE1' : return "á"; case '\xE2' : return "â"; case '\xE6' : return "æ"; case '\xE0' : return "à"; case '\xE5' : return "å"; case '\xE3' : return "ã"; case '\xE4' : return "ä"; case '\xE7' : return "ç"; case '\xE9' : return "é"; case '\xEA' : return "ê"; case '\xE8' : return "è"; case '\xF0' : return "ð"; case '\xEB' : return "ë"; case '\xED' : return "í"; case '\xEE' : return "î"; case '\xEC' : return "ì"; case '\xEF' : return "ï"; case '\xF1' : return "ñ"; case '\xF3' : return "ó"; case '\xF4' : return "ô"; case '\xF2' : return "ò"; case '\xF8' : return "ø"; case '\xF5' : return "õ"; case '\xF6' : return "ö"; case '\xDF' : return "ß"; case '\xFE' : return "þ"; case '\xFA' : return "ú"; case '\xFB' : return "û"; case '\xF9' : return "ù"; case '\xFC' : return "ü"; case '\xFD' : return "ý"; case '\xFF' : return "ÿ"; } if (charCode > 128) return('' + parseInt(charCode) + ';'); else return(thechar); } function entityNameEncode(string) { latin1Str = dreamweaver.nativeToLatin1(string); returnString = new String(); for (i=0;i<latin1Str.length;i++) { returnString += charToEntity(latin1Str.charAt(i),latin1Str.charCodeAt(i)); } return(returnString); } //*********************** API *********************** function isDOMRequired() { // Return false, indicating that this command is available in code view. return false; } //function: commandButtons //description: generic API function, returns string to be inserted at IP function commandButtons(){ return new Array(MM.BTN_OK, "setDateStr();window.close()", MM.BTN_Cancel, "window.close()", MM.BTN_Help, "displayHelp()" ); } //***********************ACCESSOR FUNCTIONS*********************** function getDateStr(){ return GstrFullDate; } function getDateID(){ return GstrDateID; } function getHTMLDateStr(){ return GstrHTMLFullDate; } //***********************LOCAL FUNCTIONS*********************** //function: setDateStr //description: called from OK button of dialog, //sets the global GstrFullDate variable function setDateStr(){ var dateStr = ""; var dateStrHTML = ""; var dateID = ""; var now = new Date(); var dayFormat = getSelectedOptionAttr(GselDayFormats,"value"); var dateFormat = getSelectedOptionAttr(GlistDateFormats,"value"); var timeFormat = getSelectedOptionAttr(GselTimeFormats,"value"); //create the date that is inserted //if selected a Japanese date format in J version of Dreamweaver, concatenate in order of //date, day, time if ((dateFormat.indexOf("Japan") == 0 || dateFormat.indexOf("Korean") == 0 || dateFormat.indexOf("Chinese") == 0) && (dreamweaver.appVersion.indexOf('ja') != -1 || dreamweaver.appVersion.indexOf('ko') != -1 || dreamweaver.appVersion.indexOf('zh') != -1)) { dateStr += createDateStr(now,dateFormat,true); dateStr += " " + createDayStr(now,dayFormat,dateFormat,false,true); dateStr += createTimeStr(now,timeFormat); dateStrHTML += createDateStr(now,dateFormat,true); dateStrHTML += " " + createDayStr(now,dayFormat,dateFormat,false,true); dateStrHTML += createTimeStr(now,timeFormat); } else { dateStr += createDayStr(now,dayFormat,dateFormat,false,true); dateStr += createDateStr(now,dateFormat,true); dateStr += createTimeStr(now,timeFormat); // we don't want to encode for J or K or C day formats if (dreamweaver.appVersion && (dreamweaver.appVersion.indexOf('ja') != -1 || dreamweaver.appVersion.indexOf('ko') != -1 || dreamweaver.appVersion.indexOf('zh') != -1) ) dateStrHTML += createDayStr(now,dayFormat,dateFormat,false,true); else dateStrHTML += createDayStr(now,dayFormat,dateFormat,false,false); dateStrHTML += createDateStr(now,dateFormat,false); dateStrHTML += createTimeStr(now,timeFormat); } //dateID is inserted into the format attribute of the lock //and also placed in the opening comment dateID = createDateID(dayFormat,dateFormat,timeFormat); //if Update Automatically On Save is visible and checked, //add locks around the date if (GcbUpdate!=null && GcbUpdate.checked){ dateStr = addLockMarkup(dateStr,dateID); dateStrHTML = addLockMarkup(dateStrHTML,dateID); } //assign to global variables accessed by accessor functions GstrFullDate = dateStr; GstrHTMLFullDate = dateStrHTML; GstrDateID = dateID; } //function: addLockMarkup //description: adds correct lock markup to the date that is //inserted function addLockMarkup(dateStr,dateID){ var openBracket = "%3C"; var closeBracket = "%3E"; var quote = "%22"; var openComment = openBracket + '!-- #BeginDate ' + 'format:' + dateID + ' --' + closeBracket; var closeComment = openBracket + '!-- #EndDate --' + closeBracket; var origAttr = openComment + dateStr + closeComment; var openLock = '<MM:BeginLock type="mmdate" format="' + dateID + '" orig="' + origAttr + '">'; var closeLock = '<MM:EndLock>'; return openLock + dateStr + closeLock; } //function: initializeUI //description: initializes the global variables, and populates //the UI with date format examples. function initializeUI(){ //return if already initialized //(this happens if command is called from PI) if (GarrDateFormats){ GlistDateFormats.focus(); return; } initGlobals(); //initialize global variables populateUI(); //populate UI } //function: populateUI //description: populate the UI with date format examples function populateUI(){ var dateFormatsArr = GarrDateFormats; //shorter names easier to work with var dayFormatsArr = GarrDayFormats; var timeFormatsArr = GarrTimeFormats; var nDateFormats = dateFormatsArr.length; var nDayFormats = dayFormatsArr.length; var nTimeFormats = timeFormatsArr.length; var dateObj = new Date("74","2","7","22","18"); //examples are for March 7, 1974 var dateStr = "",timeStr="",dayStr=""; //populate day format list in UI //the first line creates an array of formatted dayes //(the createDayStr function is overloaded to return one item //or an array, therefore, unfortunately the function name isn't //always entirely accurate) dayFormatsArr = createDayStr(dateObj,dayFormatsArr,dateFormatsArr,true,true); var counter = 0; for (i in dayFormatsArr){ if (!dayFormatsArr[i].prototype){ // Val 13-aug-99 use temporary for speedier UI loading var curr = new Option(dayFormatsArr[i]); curr.value = i; GselDayFormats.options[ counter++ ] = curr; } } //populate date format list in UI //the first line creates an array of formatted dates //(the createDateStr function is overloaded to return one item //or an array, therefore, unfortunately, the function name isn't ///always entirely accurate) dateFormatsArr = createDateStr(dateObj,dateFormatsArr,true); counter = 0; for (i in dateFormatsArr){ if (!dateFormatsArr[i].prototype){ // Val 13-aug-99: use a temporary to make initialization faster, // and only assign to the formats array once. var currObj = new Option(dateFormatsArr[i]); currObj.value = i; GlistDateFormats.options[ counter++ ] = currObj; } } //populate time format list for (i=0;i<nTimeFormats;i++){ timeStr = createTimeStr(dateObj, timeFormatsArr[i], true); GselTimeFormats.options[ i ] = new Option(timeStr); GselTimeFormats.options[ i ].value = timeFormatsArr[i]; } //select first option of each menu GselDayFormats.selectedIndex = 0; GlistDateFormats.selectedIndex = 0; GselTimeFormats.selectedIndex = 0; //put focus in date formats field GlistDateFormats.focus(); } //function: lead //description: given a one or two digit number, //adds a leading 0 if a 1 digit number function lead(num){ if (num.toString().length == "1") return "0" + num; return ( num ); } //function: createDateStr //description: given a date obj and a date format or formats //returns an array with the correctly formatted date strings //overloaded: dateFormat can be one item or an array //if it is one item, returns one item //if it is an array, returns an array function createDateStr(dateObj,dateFormat,highAscii){ var date = dateObj.getDate(); var day = dateObj.getDay(); var month = dateObj.getMonth(); var year = dateObj.getYear(); var abbrYear = (year<100)? year : year.toString().substring(1); var fullYear = (year<100)? "19" + year : year + 1900; var retVal; //return value; var abbrMonth; var fullMonth; var westfullMonth; if (highAscii || isDoubleByteVersion()) { //highAscii is true when date format is one of double-byte ones. but we also want to use this for western date format on double-byte versions. abbrMonth = ARR_AbbrMonths[ month ]; westfullMonth = ARR_WestFullMonths[ month ]; fullMonth = ARR_FullMonths[ month++ ]; } else { abbrMonth = entityNameEncode(ARR_AbbrMonths[ month ]); fullMonth = entityNameEncode(ARR_FullMonths[ month++ ]); westfullMonth = fullMonth; } //the dateFormat argument is overloaded so that it //can be either a string or an array. Handle accordingly. if (typeof dateFormat == "string"){ if (useEnglishDate(dateFormat)) fullMonth = westfullMonth; retVal = createCorrectDateFormat(dateFormat,date,day,month,abbrMonth,fullMonth,westfullMonth, year,abbrYear,fullYear); } else { //dateFormat is an array retVal = new Array(); dateFormats = dateFormat; //rename var nFormats = dateFormats.length; for (var i=0;i<nFormats;i++){ retVal[dateFormats[i]] = createCorrectDateFormat(dateFormat[i],date,day,month,abbrMonth, fullMonth,westfullMonth,year,abbrYear,fullYear); } } return retVal; } //function: createCorrectDateFormat //description: returns a correclty formatted date string function createCorrectDateFormat(dateFormat,date,day,month,abbrMonth,fullMonth,westfullMonth, year,abbrYear,fullYear,time){ var dateStr = ""; switch (dateFormat){ case "American1": // Thursday, March 7, 1974 dateStr += fullMonth + " " + date + ", " + fullYear; break; case "American2": // 3/7/74 dateStr += month + "/" + date + "/" + abbrYear; break; case "American3": // 03/07/1974 dateStr += lead(month) + "/" + lead(date) + "/" + fullYear; break; case "ISO8601": // 1974-03-07 dateStr += fullYear + "-" + lead(month) + "-" + lead(date); break; case "English1": // 7-mar-74 dateStr += date + "-" + abbrMonth + "-" + abbrYear; break; case "English2": // 07-Mar-1974 abbrMonth = abbrMonth.charAt(0).toUpperCase()+abbrMonth.substring(1); dateStr += lead(date) + "-" + abbrMonth + "-" + fullYear; break; case "Spanish1": // 7/3/74 dateStr += date + "/" + month + "/" + abbrYear; break; case "French1": // 7/03/74 dateStr += date + "/" + lead(month) + "/" + abbrYear; break; case "Italian1": // 7-03-1974 dateStr += date + "-" + lead(month) + "-" + fullYear; break; case "Brazilian1": // 07.03.74 dateStr += lead(date) + "." + lead(month) + "." + abbrYear; break; case "German1": // 07.03.1974 dateStr += lead(date) + "." + lead(month) + "." + fullYear; break; case "Japanese1": // 74/03/07 dateStr += abbrYear + "/" + lead(month) + "/" + lead(date) ; break; case "Japanese2": // 1974 (yearJapanese) 3(month) 7(dayJapanese) dateStr += fullYear + yearJapanese + fullMonth + date + dayJapanese; break; case "Japanese3": // 1974(yearJapanese)03(month) 07(dayJapanese) dateStr += fullYear + yearJapanese + lead(month) + monthJapanese + lead(date) + dayJapanese; break; case "Swedish1": // 7 March, 1974 dateStr += date + " " + fullMonth + ", " + fullYear; break; case "Korean1": // 1974(korean year) 3(korean month) 7(korean day) dateStr += fullYear + yearKorean + fullMonth + " " + date + dayKorean; break; case "Korean2": // 1974.3.7 dateStr += fullYear + "." + month + "." + date; break; case "Korean3": // 3.7.1974 dateStr += month + "." + date + "." + fullYear; break; case "Korean4": // March 7,1974 dateStr += westfullMonth + " " + date + ", " + fullYear; break; case "Korean5": // 74.03.07 dateStr += abbrYear + "." + lead(month) + "." + lead(date); break; case "Korean6": // 3/7/ 1974 dateStr += abbrMonth + "/" + lead(date) + " " + fullYear; break; case "Korean7": // 74-03-07 dateStr += abbrYear + "-" + lead(month) + "-" + lead(date); break; case "Chinese1": // 74/3/7 dateStr += abbrYear + "/" + month + "/" + date ; break; case "Chinese2": // 1974 (yearChinese) 3(monthChinese) 7(dayChinese) dateStr += fullYear + yearChinese + fullMonth + date + dayChinese; break; default: break; } return dateStr; } //function: createDayStr //description: see createDateStr notes. Except of course this function //returns a correctly formatted day (or days) instead of a date function createDayStr(dateObj,dayFormat,dateFormat,bPreview,highAscii){ var day = dateObj.getDay(); var WestFullDay = ARR_WestFullDays[day]; var WestAbbrDay = ARR_WestAbbrDays[day]; var fullDay; var abbrDay; if (highAscii || isDoubleByteVersion()) { //highAscii is true when data format is one of double-byte ones. but we also want to use this for western data format on double-byte versions. fullDay = ARR_FullDays[day]; abbrDay = ARR_AbbrDays[day]; } else { fullDay = entityNameEncode(ARR_FullDays[day]); abbrDay = entityNameEncode(ARR_AbbrDays[day]); } if (typeof dayFormat == "string"){ dayFormat = useEnglishDayFormat(dayFormat, dateFormat); retVal = createCorrectDayFormat(dayFormat,fullDay,abbrDay,WestFullDay,WestAbbrDay,bPreview); } else { //dayFormat is an array retVal = new Array(); dayFormats = dayFormat; //rename for clarity var nFormats = dayFormats.length; for (var i=0;i<nFormats;i++){ retVal[dayFormats[i]] = createCorrectDayFormat(dayFormat[i],fullDay,abbrDay,WestFullDay,WestAbbrDay,bPreview); } } return retVal; } //function: createCorrectDayFormat //description: returns the correctly formatted day format function createCorrectDayFormat(dayFormat,fullDay,abbrDay,WestFullDay,WestAbbrDay,bPreview){ var dayStr = ""; switch (dayFormat){ case "NoDay": if (bPreview) dayStr = "[" + OPTION_NoDay + "]"; break; case "FullDayComma": dayStr = CHAR_PreDay + fullDay + CHAR_PostDay; break; case "FullDay": dayStr = fullDay + " "; break; case "AbbrDayComma": dayStr = CHAR_PreDay + abbrDay + CHAR_PostDay; break; case "AbbrDay": dayStr = abbrDay + " "; break; case "LowAbbrDayComma": dayStr = abbrDay.toLowerCase() + ", "; break; case "LowAbbrDay": dayStr = abbrDay.toLowerCase() + " "; break; case "WestFullDayComma": dayStr = WestFullDay + westSeparator; break; case "WestAbbrDayComma": dayStr = WestAbbrDay + westSeparator; break; case "Euroasian1": // 07-Mar-1974 abbrMonth = abbrMonth.charAt(0).toUpperCase()+abbrMonth.substring(1); dateStr += lead(date) + " " + fullMonth + " " + fullYear; break; default: break; } return dayStr; } //function: createTimeStr //description: given a dateObj and a time format, //returns the correctly formatted time string //The time format argument is "a" for AM/PM, //"m" for military time, and "" for no time function createTimeStr(dateObj,timeFormat,bPreview){ var hours = dateObj.getHours(); var minutes = lead(dateObj.getMinutes()); var timeStr = ""; //return value switch (timeFormat){ case "NoTime": if (bPreview) timeStr = "[" + OPTION_NoTime + "]"; break; case "AMPMTime": timeStr += (hours>=12) ? ((hours-12==0)?hours:hours-12) + ":" + minutes + " " + PM : hours + ":" + minutes + " " + AM; timeStr = " " + timeStr; break; case "MilitaryTime": timeStr += " " + hours + ":" + minutes; timeStr = " " + timeStr; break; default: break; } return timeStr; } // Check if user is running localized DW function isDoubleByteVersion(){ if (dreamweaver.appVersion && (dreamweaver.appVersion.indexOf('ja') != -1 || dreamweaver.appVersion.indexOf('ko') != -1 || dreamweaver.appVersion.indexOf('zh') != -1) ) return true; else return false; } // Return true if selected date format is not localized specific one (i.e. Japanese1). // For example, // -If user selected "American1" date format on Japanese DW, this function returns true. // -If user selected "Japanese1" date format on Japanese DW, this function returns false. function useEnglishDate(format) { if ((dreamweaver.appVersion && dreamweaver.appVersion.indexOf('ja') != -1) && format.indexOf("Japan") != 0) return true; else if ((dreamweaver.appVersion && dreamweaver.appVersion.indexOf('ko') != -1) && format.indexOf("Korean") != 0) return true; else if ((dreamweaver.appVersion && dreamweaver.appVersion.indexOf('zh') != -1) && format.indexOf("Chinese") != 0) return true; else return false; } // if selected date was not localized specific one (i.e. Japanese1), force to use Western day formats. function useEnglishDayFormat(dayFormat, dateFormat) { var rtnDayFormat = dayFormat; if (useEnglishDate(dateFormat)) { if (dayFormat == "FullDayComma" || dayFormat == "FullDay") rtnDayFormat = "WestFullDayComma"; else if (dayFormat != "NoDay") rtnDayFormat = "WestAbbrDayComma"; } return rtnDayFormat; }