blank.gif
triangle.gif Du er her: /  Forsiden  /  Javascript  /  Full-source-code    
blank.gif
blank.gif
tl.gif Curriculum Vitae (da) tr.gif tl.gif Certificeringer tr.gif tl.gif Erhvervskvalifikationer tr.gif tl.gif Java-udvikler tr.gif tl.gif Kurser tr.gif tl.gif Personlighed tr.gif tl.gif Profil tr.gif tl.gif Projekter tr.gif tl.gif Resultater tr.gif tl.gif Sprogkundskaber tr.gif tl.gif Uddannelse tr.gif
blank.gif
blank.gif
arrow-headline.gif Navigation
arrow-headline.gif Artikler
 

Demo af følgende javascript funktionalitet

De følgende demo samples viser dig hvordan man opretter en singleton i javascript og hvordan man kan kode en dato formatter og formatere et beløb med det locale der er gældende for dit område

Kildekoden

Copyrigt notice: Du må gerne kopiere denne kildekode, så længe du beholder copyright notice intakt

Copyright Notice
@package   javascript
@author    http://Finn-Rasmussen.com
@copyright http://Finn-Rasmussen.com
@version   1.10
@since     22-feb-2007
Den fulde kildekode for: AmountFormat

The full source code for:

AmountFormat.js.php.html

AmountFormat.js.php
   1:<?
   2:/**
   3: * This class simulates a AmountFormat object, in a very primitive matter
   4: *
   5: * class AmountFormat {
   6: *    public static AmountFormat getInstance(); // Use the current Locale
   7: *    public static AmountFormat getInstance(Locale locale);
   8: *    public static String format(String number);
   9: * }
  10: *
  11: * @package javascript
  12: * @author    http://Finn-Rasmussen.com
  13: * @copyright http://Finn-Rasmussen.com
  14: * @version 1.10
  15: * @since 22-feb-2007
  16: */
  17:
  18:/**
  19: * The constructor function simulates a crude AmountFormat object
  20: * The AmountFormat object is instanciated with the singleton pattern
  21: * The Singleton pattern is simulated in JavaScript by:
  22: * - Using an anonymous constructor to create a function object
  23: * - And saving the returned object in an external variable
  24: * <code>
  25: * usage:
  26: *   // Include and instanciate the AmountFormat class as a singleton
  27: *   &lt;script type="text/javascript" src="../page/AmountFormat.js.php"&gt;&lt;/script&gt;
  28: *
  29: *   // Use the AmountFormat singleton class with the default Locale
  30: *   var value = AmountFormat.format('1234567.89'); // value=1,234,567.89
  31: * Or
  32: *   var locale = Locale.getInstance(LanguageCode.DANISH, CountryCode.DK);
  33: *   var amountformat = AmountFormat.getInstance(locale);
  34: *   var formatted    = amountformat.format('1234567,89');
  35: * </code>
  36: */
  37:?>
  38:var AmountFormat = new function() {
  39:    // Consts, do NOT change
  40:    this.PERIOD = '.';
  41:    this.COMMA  = ',';
  42:    
  43:    // local vars to customize to your locale: I.e. 1,234,567.89
  44:    this.locale = Locale.getInstance();
  45:<?
  46:    /**
  47:     * Get the instance of the current AmountFormat object 
  48:     * @param  Locale locale The Locale object
  49:     * @return AmountFormat  The current default AmountFormat object
  50:     */
  51:?>
  52:    this.getInstance = function(locale) {
  53:        if (typeof locale !== 'undefined') {
  54:            this.locale = locale;
  55:        } else {
  56:            alert(typeof locale);
  57:        }
  58:        return this;
  59:    }
  60:<?    
  61:    /**
  62:     * Format the amount with decimal point and thousand seperator
  63:     * This is the entry methode you must use
  64:     * usage:
  65:     *   var value = AmountFormat.format('1234567.89'); // value=1,234,567.89
  66:     * </code>
  67:     * @public
  68:     * @param  String amount The number to format
  69:     * @return String The number formatted like 1.234.567,89
  70:     */
  71:?>
  72:    this.format = function(number) {
  73:       var n = this.parse(number);
  74:       var i = parseFloat(n);
  75:       var s = this.parseString(i); 
  76:       var sign = i<0?'-':'';
  77:       return sign + this.addThousandSeperators(s);
  78:    }
  79:<?
  80:   // THE following methods are private helpers
  81:   
  82:    /**
  83:     * Parse the amount into a string of digits and decimals seperated by a period
  84:     * All the thousand seperators and the comma decimal point seperators are removed
  85:     * @private
  86:     * @param  String number The number to parse
  87:     * @return String The number parsed into: digits+'.'+decimals
  88:     */
  89:?>
  90:    this.parse = function(number) {
  91:        var decimals = '';
  92:        var prefix   = '';
  93:        var d = number.split(this.locale.getDecimalPoint());
  94:        switch (d.length) {
  95:            case 2:
  96:                decimals = d[1];
  97:            case 1:
  98:                prefix = d[0];
  99:                break;
 100:            default:
 101:                alert('Wrong DECIMAL_POINT, Expected=digits'+this.locale.getDecimalPoint()+'decimals found='+number);
 102:                break;
 103:        }
 104:        // Strip off any thousand seperators and return a real javascript number like: 1234567.89
 105:        var digits = this.stripOfThousandSeperators(prefix);
 106:        s = digits + this.PERIOD + decimals;
 107:        return s;
 108:    }
 109:<?
 110:    /**
 111:     * Parse the amount into a string of digits and decimals seperated by a period
 112:     * All the thousand seperators and the comma decimal point seperators are removed
 113:     * @private
 114:     * @param  String number The number to parse
 115:     * @return String The number parsed into: digits+'.'+decimals
 116:     */
 117:?>
 118:    this.parseString = function(number) {
 119:       var n = number;
 120:       if (isNaN(n) ) {
 121:           alert('Wrong format, Expected=digits'+this.locale.getDecimalPoint()+'decimals, found='+number);
 122:           n = 0.00;
 123:       }
 124:       i = Math.abs(n);
 125:       i = parseInt((i + .005) * 100, 10);
 126:       i = i / 100;
 127:       s = new String(i);
 128:       if (s.indexOf(this.PERIOD) < 0) {
 129:           s += this.PERIOD+'00';
 130:       }
 131:       if (s.indexOf(this.PERIOD) == (s.length - 2)) {
 132:           s += '0';
 133:       }
 134:       return s;
 135:    }
 136:<?
 137:    /**
 138:     * Strip off any thousand seperators
 139:     * @private
 140:     * @param  String number The number to strip of the thousand seperators
 141:     * @return String The number with out any thousand seperators
 142:     */
 143:?>
 144:    this.stripOfThousandSeperators = function(number) {
 145:        var digits = '';
 146:        var t = number.split(this.locale.getThousandSeperator());
 147:        for (var i=0; i < t.length; i++) {
 148:            digits += t[i];
 149:        }
 150:        return digits;
 151:    }
 152:<?
 153:    /**
 154:     * Update the decimal point, Add the thousand seperators, if defined
 155:     * @private
 156:     * @param  String number The number to update
 157:     * @return String The updated number
 158:     */
 159:?>
 160:    this.addThousandSeperators = function(number) {
 161:       var s = number.replace(RegExp('[\\'+this.PERIOD+']',"g"),this.locale.getDecimalPoint());
 162:       var decimals = s.split(this.locale.getDecimalPoint());
 163:       var t = '';
 164:       if (this.locale.getThousandSeperator() !== '') {
 165:           // Add thousand seperators 1.234.567,89
 166:           var size = decimals[0].length;
 167:           for (var i=0; i < size; i++) {
 168:                t = decimals[0].substr((size-1)-i, 1) + t;
 169:                if ((i+1) % 3) {
 170:                    // Ignore
 171:                } else {
 172:                    if (i < size - 1) {
 173:                        t = this.locale.getThousandSeperator() + t;
 174:                    }
 175:                }
 176:           }
 177:       } else {
 178:            t = s;
 179:       }
 180:       if (decimals.length === 2) {
 181:            t += this.locale.getDecimalPoint() + decimals[1]
 182:       }
 183:       return t;
 184:    }
 185:    
 186:}

CountryCode

Den fulde kildekode for:

CountryCode.js.php.html

CountryCode.js.php
   1:<?
   2:/**
   3: * This class simulates a CountryCode object, in a very primitive matter
   4: *
   5: * class CountryCode {
   6: *    public static final String DK = "DK";
   7: *    public static final String NO = "NO"; // Etc.
   8: * }
   9: *
  10: * @package javascript
  11: * @author    http://Finn-Rasmussen.com
  12: * @copyright http://Finn-Rasmussen.com
  13: * @version 1.10
  14: * @since 22-feb-2007
  15: */
  16:
  17:/**
  18: * The supported Country Codes
  19: * @see http://www.iso.org/iso/en/prods-services/iso3166ma/02iso-3166-code-lists/list-en1.html
  20: * @see http://www.chemie.fu-berlin.de/diverse/doc/ISO_3166.html
  21: * The constructor function simulates a crude Country Code object
  22: * The Country Code object is instanciated with the singleton pattern
  23: * The Singleton pattern is simulated in JavaScript by:
  24: * - Using an anonymous constructor to create a function object
  25: * - And saving the returned object in an external variable
  26: * <code>
  27: * usage:
  28: *   // Include and instanciate the Country Code class as a singleton
  29: *   &lt;script type="text/javascript" src="../page/CountryCode.js.php"&gt;&lt;/script&gt;
  30: *
  31: *   // Use the Country Codes singleton class
  32: *   var countryCode = CountryCode.DK; // Returns a Country Code string specific for DENMARK
  33: * </code>
  34: */
  35:?>
  36:var CountryCode = new function() {
  37:    this.DE = 'DE';
  38:    this.DK = 'DK';
  39:    this.NO = 'NO';
  40:    this.SE = 'SE';
  41:    this.UK = 'UK';
  42:    this.US = 'US';
  43:    // TODO, add more ...
  44:}

DateFormat

Den fulde kildekode for:

DateFormat.js.php.html

DateFormat.js.php
   1:<?
   2:/**
   3: * This class simulates a DateFormat object, in a very primitive matter
   4: *
   5: * class DateFormat {
   6: *    public static DateFormat getInstance(); // Use the current Locale
   7: *    public static DateFormat getInstance(Locale locale);
   8: *    public static DateFormat getDateInstance(String styleDate, Locale locale);
   9: *    public String format(String pattern);
  10: * }
  11: *
  12: * @package javascript
  13: * @author    http://Finn-Rasmussen.com
  14: * @copyright http://Finn-Rasmussen.com
  15: * @version 1.10
  16: * @since 22-feb-2007
  17: */
  18:
  19:/**
  20: * The constructor function simulates a crude DateFormat object
  21: * The DateFormat object is instanciated with the singleton pattern
  22: * The Singleton pattern is simulated in JavaScript by:
  23: * - Using an anonymous constructor to create a function object
  24: * - And saving the returned object in an external variable
  25: * <code>
  26: * usage:
  27: *   // NOTE: YOU MUST INCLUDE the Locale class before this class
  28: *   // Include and instanciate the DateFormat class as a singleton
  29: *   &lt;script type="text/javascript" src="../page/DateFormat.js.php"&gt;&lt;/script&gt;
  30: *
  31: *   var locale = Locale.getInstance(LanguageCode.DANISH, CountryCode.DK);
  32: *   var dateformat = DateFormat.getDateInstance(DateFormat.LONG, locale);
  33: *   var formatted  = dateformat.format("06122007");
  34: * </code>
  35: */
  36:
  37:/**
  38: * Instanciate the singleton object
  39: */
  40:?>
  41:var DateFormat = new function() {
  42:<?
  43:    /**
  44:     * Consts, do NOT change these helpers
  45:     */
  46:?>
  47:    this.DASH  = '-';
  48:    this.SLASH = '/';
  49:    this.LONG  = 'dd-mm-yyyy'; // Long  style pattern
  50:    this.SHORT = 'ddmmyy'; // Short style pattern
  51:<?    
  52:    /**
  53:     * Default DateFormat to use.
  54:     * PROBLEM med static vars
  55:     */
  56:?>
  57:    this.styleDate  = "date"; // TODO
  58:    this.locale     = Locale.getInstance();
  59:    
  60:<?
  61:    /**
  62:     * Get the instance of the current DateFormat object 
  63:     * @param  Locale locale The Locale object
  64:     * @return DateFormat The current default DateFormat object
  65:     */
  66:?>
  67:    this.getInstance = function(locale) {
  68:        if (typeof locale !== 'undefined') {
  69:            this.locale = locale;
  70:        } else {
  71:            alert(typeof locale);
  72:        }
  73:        return this;
  74:    }
  75:    
  76:<?
  77:    /**
  78:     * Get a default date formatter that uses the style for the date
  79:     * @param  String styleDate The pattern style for the Date
  80:     * @param  Locale locale    The Locale object
  81:     * @return object The current default DateFormat object adjusted with Date style and locale
  82:     */
  83:?>
  84:    this.getDateInstance = function(styleDate, locale) {
  85:        this.styleDate = styleDate;
  86:        return this.getInstance(locale);
  87:    }
  88:<?
  89:    /**
  90:     * Get the formatted date from a date string.
  91:     * The following formats(number of chars) are supported with or without '-' or '/'
  92:     * 
  93:     * Syntax: date[seperator][month][seperator][year]
  94:     * Where : date is 1-2 digits, seperator is none, '-' or '/', month is 1-2 digits, year is 0, 2 or 4 digits
  95:     * 
  96:     * d(1), dd(2), dmm(3), ddmm(4),
  97:     * dmmyy(5), ddmmyy(6), dmmyyyy(7), ddmmyyyy(8),
  98:     * d-mm(4), dd-mm(5), dd-m-yy(7), dd-m-yyyy(9),
  99:     * d-mm-yy(7), dd-mm-yy(8), d-m-yyyy(8), d-mm-yyyy(9), dd-mm-yyyy(10),
 100:     * d/mm(4), dd/mm(5), dd/m/yy(7), dd/m/yyyy(9),
 101:     * d/mm/yy(7), dd/mm/yy(8), d/m/yyyy(8), d/mm/yyyy(9), dd/mm/yyyy(10),
 102:     * 
 103:     * Samples:
 104:     * 03-09-08 => 03-09-2008
 105:     * 3        => 03-[thisMonth]-[thisYear]
 106:     * 3-9      => 03-09-[thisYear]
 107:     * 309      => 03-09-[thisYear]
 108:     * 3-9-8    => 03-09-2008
 109:     * 
 110:     * NOT SUPPORTED: 
 111:     * m(1), mm(2), y(1), yy(2), yyyy(4), myy(3), mmyy(4), 
 112:     * m/d/yy, mm/dd/yyyy etc.
 113:     * 
 114:     * @param  String strDate The pattern to format to a date string
 115:     * @return String The formatted date adjusted for date style and locale
 116:     */
 117:?>
 118:    this.format = function(strDate) {
 119:        var pattern = this.adjust(strDate);
 120:        var date    = this.newDate(pattern);
 121:        var format  = this.parse(date);
 122:        return format;
 123:    }
 124:<?
 125:    /**
 126:     * Adjust the pattern string to match a valid date pattern.
 127:     * This function will try to gess what the user means, with insufficient data
 128:     * @private
 129:     * @param  String strDate The strDate to adjust
 130:     * @return String The adjusted date pattern string
 131:     */
 132:?>
 133:    this.adjust = function(strDate) {
 134:        var pattern = strDate;
 135:        var today = new Date();
 136:        if (pattern.length === 1 || pattern.length === 2) {
 137:            // Expected strDate d(1) or dd(2)
 138:            var date  = this.getDate(pattern);
 139:            var now = new Date(today.getFullYear(), today.getMonth(), date);
 140:            pattern = '' + now.getDate() + this.SLASH + (now.getMonth() + 1) + this.SLASH + now.getFullYear();
 141:        }
 142:        if (pattern.length === 3 || pattern.length === 4) {
 143:            if (pattern.indexOf(this.DASH) === -1 &&  pattern.indexOf(this.SLASH) === -1) {
 144:                // Expected strDate dmm(3) or ddmm(4)
 145:                var pos = pattern.length===3?1:2; // 0123
 146:                var date  = parseInt(pattern.substring(0,pos),10);
 147:                var month = parseInt(pattern.substring(pos),10);
 148:                var now = new Date(today.getFullYear(), month-1, date);
 149:                pattern = '' + now.getDate() + this.SLASH + (now.getMonth() + 1) + this.SLASH + now.getFullYear();
 150:            } else {
 151:                // Expected strDate d-m(3), d-mm(4)
 152:                var date  = this.getDate(pattern);
 153:                var month = this.getMonth(pattern);
 154:                var now = new Date(today.getFullYear(), month-1, date);
 155:                pattern = '' + now.getDate() + this.SLASH + (now.getMonth() + 1) + this.SLASH + now.getFullYear();
 156:            }
 157:        }
 158:        if (pattern.length === 5 || pattern.length === 7) {
 159:            if (pattern.indexOf(this.DASH) === -1 &&  pattern.indexOf(this.SLASH) === -1) {
 160:                // Expected strdate dmmyy(5), dmmyyyy(7)
 161:                pattern = '0' + pattern;
 162:            } else {
 163:                // Expected strdate dd-mm(5), d-mm-yy(7), dd-m-yy(7)
 164:                var date  = this.getDate(pattern);
 165:                var month = this.getMonth(pattern);
 166:                var year  = this.getYear(pattern);
 167:                var now = new Date(year, month-1, date);
 168:                pattern = '' + now.getDate() + this.SLASH + (now.getMonth() + 1) + this.SLASH + now.getFullYear();
 169:            }
 170:        }
 171:        return pattern;
 172:    }
 173:<?
 174:    /**
 175:     * Get a new date object from a pattern string.
 176:     * The following patterns are supported xxxxxx(length). Not supported mm-dd-yyyy or yyyy-mm-dd etc.
 177:     * ddmmyy(6), ddmmyyyy(8), dd-mm-yy(8), dd-mm-yyyy(10), dd/mm/yy(8), or dd/mm/yyyy(10)
 178:     * @private
 179:     * @param  String pattern The pattern to get the day, month and year info from
 180:     * @return Date The date object
 181:     */
 182:?>
 183:    this.newDate = function(pattern) {
 184:        var date  = this.getDate(pattern);
 185:        var month = this.getMonth(pattern);
 186:        var year  = this.getYear(pattern);
 187:        return new Date(year, month-1, date);
 188:    }
 189:<?
 190:    /**
 191:     * Get the date of the month from a pattern string.
 192:     * If not a valid date is found, the current date of the month is used
 193:     * @private
 194:     * @param  String pattern The pattern to get the day, month and year info from
 195:     * @return String The day as 'dd' or 'd'
 196:     */
 197:?>
 198:    this.getDate = function(pattern) {
 199:        var start = this.getStart(pattern,0);
 200:        var end   = this.getEnd(pattern,1);
 201:        var date  = parseInt(pattern.substring(start, end),10);
 202:        if (isNaN(date)) {
 203:            var today = new Date();
 204:            date = today.getDate();
 205:        }
 206:        return date;
 207:    }
 208:<?
 209:    /**
 210:     * Get the month from a pattern string.
 211:     * If not a valid month is found, the current month is used
 212:     * @private
 213:     * @param  String pattern The pattern to get the day, month and year info from
 214:     * @return String The month as 'mm' or 'm'
 215:     */
 216:?>
 217:    this.getMonth = function(pattern) {
 218:        var start = this.getStart(pattern,2);
 219:        var end   = this.getEnd(pattern,3);
 220:        var month = parseInt(pattern.substring(start, end),10);
 221:        if (isNaN(month)) {
 222:            var today = new Date();
 223:            month = today.getMonth() + 1;
 224:        }
 225:        return month;
 226:    }
 227:<?
 228:    /**
 229:     * Get the year from a pattern string.
 230:     * If not a valid year is found, the current year is used
 231:     * @private
 232:     * @param  String pattern The pattern to get the day, month and year info from
 233:     * @return String The year as 'yyyy' or 'yy'
 234:     */
 235:?>
 236:    this.getYear = function(pattern) {
 237:        var start = this.getStart(pattern,4);
 238:        var end   = this.getEnd(pattern,5);
 239:        var year  = parseInt(pattern.substring(start, end),10);
 240:        if (isNaN(year)) {
 241:            var today = new Date();
 242:            year = today.getFullYear();
 243:        }
 244:        if (year < 100) {
 245:            year = 2000 + year;
 246:        }
 247:        return year;
 248:    }
 249:<?
 250:    /**
 251:     * Get the Start position from a pattern string.
 252:     * Several pattern searches are used
 253:     * ddmmyy or dd-mm-yyyy or d-m-yy or d-mm-yyyy
 254:     * @private
 255:     * @param  String pattern The pattern to get the day, month and year info from
 256:     * @param  int    level   The array level to search for
 257:     * @return int The start position in the pattern
 258:     */
 259:?>
 260:    this.getStart = function(pattern,level) {
 261:        var pos = this.getLevel(pattern, level);
 262:        pos = this.split(pattern, level, this.DASH , pos);
 263:        pos = this.split(pattern, level, this.SLASH, pos);
 264:        return pos;
 265:    }
 266:<?
 267:    /**
 268:     * Get the End position from a pattern string.
 269:     * ddmmyy or dd-mm-yyyy or d-m-yy or d-mm-yyyy
 270:     * @private
 271:     * @param  String pattern The pattern to get the day, month and year info from
 272:     * @param  int    level   The array level to search for
 273:     * @return int The end position in the pattern
 274:     */
 275:?>
 276:    this.getEnd = function(pattern,level) {
 277:        return this.getStart(pattern,level);
 278:    }    
 279:<?
 280:    /**
 281:     * Split the pattern into an array, defined by the char
 282:     * If nothing is found the current position is used
 283:     * @private
 284:     * @param  String pattern The pattern to get the day, month and year info from
 285:     * @param  int    level   The array level to search for
 286:     * @param  String char    The char to use as a split char
 287:     * @param  int    pos     The current position in the pattern
 288:     * @return int The new position in the pattern string
 289:     */
 290:?>
 291:    this.split = function(pattern, level, char, pos) {
 292:        var newPos = pos; 
 293:        var split = pattern.split(char);
 294:        if (split.length > 1) {
 295:            var ps = this.getPos(split, level);
 296:            if (ps != -1) {
 297:                newPos = ps; // Found a match, use it
 298:            }
 299:        } else {
 300:            // Ignore
 301:        }
 302:        return newPos;
 303:    }
 304:<?
 305:    /**
 306:     * Get the Start position from a pattern string, where no seperators exists.
 307:     * It is assumed that the 'x' or 'xx' are used for the levels: 0-4
 308:     * 0 2 4        0 2 4
 309:     * ddmmyy(6) or ddmmyyyy(8)
 310:     * Or the End position from a pattern string.
 311:     *   1 3 5        1 3   5
 312:     * ddmmyy(6) or ddmmyyyy(8)
 313:     * @private
 314:     * @param  String pattern The pattern to search
 315:     * @param  int level   The array level to search for
 316:     * @return int The starting level to use, return -1 if not a pattern with '-' or '/'
 317:     */
 318:?>
 319:    this.getLevel = function(pattern, level) {
 320:        var pos = -1;
 321:        if (pattern.indexOf(this.DASH) === -1 &&  pattern.indexOf(this.SLASH) === -1) {
 322:            pos = 0;
 323:            switch (level) {
 324:                case 1:
 325:                    pos++;
 326:                case 2: 
 327:                    pos += level; 
 328:                    break;
 329:                case 3: 
 330:                    pos++;
 331:                case 4: 
 332:                    pos += level; 
 333:                    break;
 334:                case 5: 
 335:                    pos = pattern.length;
 336:                    break;
 337:            }
 338:        }
 339:        return pos;
 340:    }
 341:<?
 342:    /**
 343:     * Get the Start or End position from a pattern string.
 344:     * 0 2 4 5   0 12 34   5   01234 5   012 34   5
 345:     * ddmmyy or dd-mm-yyyy or d-m-yy or d-mm-yyyy
 346:     * @private
 347:     * @param  String split The pattern to get the day, month and year info from
 348:     * @param  int    level The array level to search for
 349:     * @return Date The date object
 350:     */
 351:?>
 352:    this.getPos = function(split, level) {
 353:        var pos = -1;
 354:        if (level >= 0) {
 355:            pos = 0;
 356:        }
 357:        if (level > 0 && split.length > 0) {
 358:            pos += split[0].length;
 359:        }
 360:        if (level > 1 && split.length > 0) {
 361:            pos += 1; // Dash or Slash
 362:        }
 363:        if (level > 2 && split.length > 1) {
 364:            pos += split[1].length;
 365:        }
 366:        if (level > 3 && split.length > 1) {
 367:            pos += 1; // Dash or Slash
 368:        }
 369:        if (level > 4 && split.length > 2) {
 370:            pos += split[2].length;
 371:        }
 372:        if (level > 5 && split.length > 2) {
 373:            // Ignore
 374:        }
 375:        return pos;
 376:    }
 377:<?
 378:    /**
 379:     * Parse the final date into a string from a date object.
 380:     * @private
 381:     * @param  Date date The date object to parse
 382:     * @return String The formatted date string
 383:     */
 384:?>
 385:    this.parse = function(date) {
 386:        var day   = date.getDate();
 387:        if (day < 10) {
 388:            day = '0' + day;
 389:        }
 390:        var month = date.getMonth() + 1; // Zero based
 391:        if (month < 10) {
 392:            month = '0' + month;
 393:        }
 394:        var year  = date.getFullYear();
 395:        var seperator = this.locale.getSeperator();
 396:        // TODO this.SHORT or this.LONG
 397:        return day + seperator + month + seperator + year;
 398:    }
 399:}

LanguageCode

Den fulde kildekode for:

LanguageCode.js.php.html

LanguageCode.js.php.html
   1:<?
   2:/**
   3: * This class simulates a Language Code object, in a very primitive matter
   4: *
   5: * class LanguageCode {
   6: *    public static final String DANISH  = "da";
   7: *    public static final String ENGLISH = "en"; // Etc.
   8: * }
   9: *
  10: * @package javascript
  11: * @author    http://Finn-Rasmussen.com
  12: * @copyright http://Finn-Rasmussen.com
  13: * @version 1.10
  14: * @since 22-feb-2007
  15: */
  16:
  17:/**
  18: * The supported Language Codes
  19: * @see http://www.ics.uci.edu/pub/ietf/http/related/iso639.txt 
  20: * The constructor function simulates a crude Language Code object
  21: * The Language Code object is instanciated with the singleton pattern
  22: * The Singleton pattern is simulated in JavaScript by:
  23: * - Using an anonymous constructor to create a function object
  24: * - And saving the returned object in an external variable
  25: * <code>
  26: * usage:
  27: *   // Include and instanciate the Language Code class as a singleton
  28: *   &lt;script type="text/javascript" src="../page/LanguageCode.js.php"&gt;&lt;/script&gt;
  29: *
  30: *   // Use the Language Code singleton class
  31: *   var language = LanguageCode.DANISH; // Returns a Language string specific for DENMARK
  32: * </code>
  33: */
  34:?>
  35:var LanguageCode = new function() {
  36:    this.GERMAN    = 'de';
  37:    this.DANISH    = 'da';
  38:    this.ENGLISH   = 'en';
  39:    this.NORWEGIAN = 'no';
  40:    this.SWEDISH   = 'sv';
  41:    // TODO, add more ...
  42:}

Locale

Den fulde kildekode for:

Locale.js.php.html

Locale.js.php
   1:<?
   2:/**
   3: * This class simulates a Locale object, in a very primitive matter
   4: *
   5: * class Locale {
   6: *    // NOTE: staic vars and a singleton, means changes are shared by every body
   7: *    public static LanguageCode language = LanguageCode.DANISH;
   8: *    public static CountryCode  country  = CountryCode.DK;
   9: *    public static Locale getInstance();
  10: *    public static String getDecimalPoint();
  11: *    public static String getThousandSeperator();
  12: *    public static String getSeperator();
  13: * }
  14: *
  15: * @package javascript
  16: * @author    http://Finn-Rasmussen.com
  17: * @copyright http://Finn-Rasmussen.com
  18: * @version 1.10
  19: * @since 22-feb-2007
  20: */
  21:
  22:// NOTE: The LanguageCode.js and CountryCode.js src files must be included before this file
  23:
  24:/**
  25: * The constructor function simulates a crude Locale object
  26: * The Locale object is instanciated with the singleton pattern
  27: * The Singleton pattern is simulated in JavaScript by:
  28: * - Using an anonymous constructor to create a function object
  29: * - And saving the returned object in an external variable
  30: * <code>
  31: * usage:
  32: *   // Include and instanciate the Locale class as a singleton
  33: *   // NOTE: CountryCode.js and LanguageCode.js MUST be included before this file
  34: *   &lt;script type="text/javascript" src="../page/Locale.js.php"&gt;&lt;/script&gt;
  35: *
  36: *   // Use the Locale singleton class, and set the Locale to use
  37: *   var custom            = Locale.getInstance(LanguageCode.DANISH, CountryCode.DK);
  38: * Or
  39: *   var mylocale          = Locale.getInstance();
  40: *   var decimalPoint      = mylocale.getDecimalPoint();
  41: *   var thousandSeperator = mylocale.getThousandSeperator();
  42: *   var seperator         = mylocale.getSeperator();
  43: * Or
  44: *   var decimalPoint      = Locale.getDecimalPoint();
  45: *   var thousandSeperator = Locale.getThousandSeperator();
  46: *   var seperator         = Locale.getSeperator();
  47: * Or
  48: *   var decimalPoint      = Locale.getInstance().getDecimalPoint();
  49: *   var thousandSeperator = Locale.getInstance().getThousandSeperator();
  50: *   var seperator         = Locale.getInstance().getSeperator();
  51: * Or
  52: *   var decimalPoint      = Locale.getInstance(language, country).getDecimalPoint();
  53: * </code>
  54: */
  55:
  56:/**
  57: * Instanciate the singleton object
  58: */
  59:?>
  60:var Locale = new function() {
  61:    // Consts, do NOT change
  62:    this.PERIOD = '.';
  63:    this.COMMA  = ',';
  64:    this.DASH   = '-';
  65:    this.SLASH  = '/';
  66:<?
  67:    /**
  68:     * static vars which in fact are global, so if you change them, they are changed every where 
  69:     * You may Localize here
  70:     */
  71:?>
  72:    this.language = LanguageCode.DANISH;
  73:    this.country  = CountryCode.DK;
  74:<?
  75:    /**
  76:     * Get an Instance for the default locale object
  77:     * @param  String language The language code for this locale
  78:     * @param  String country  The country  code for this locale
  79:     * @return object The current default Locale object
  80:     */
  81:?>
  82:    this.getInstance = function(language, country) {
  83:        if (typeof language !== 'undefined') {
  84:            this.language = language;
  85:        }
  86:        if (typeof country !== 'undefined') {
  87:            this.country  = country;
  88:        }
  89:        return this;
  90:    }
  91:<?
  92:    /**
  93:     * Get the Decimal Point for this locale
  94:     * @return String The decimal point ',' or '.'
  95:     */
  96:?>
  97:    this.getDecimalPoint = function() {
  98:        var decimalPoint = this.PERIOD;
  99:        switch (this.language) {
 100:            case LanguageCode.DANISH:
 101:            case LanguageCode.NORWEGIAN:
 102:            case LanguageCode.SWEDISH:
 103:                decimalPoint = this.COMMA;
 104:                break;
 105:            default:
 106:                break;
 107:        }
 108:        return decimalPoint;
 109:    }
 110:<?    
 111:    
 112:    /**
 113:     * Get the Thousand Seperator for this locale
 114:     * @return String The thousand seperator ',' or '.'
 115:     */
 116:?>
 117:    this.getThousandSeperator = function() {
 118:        var thousandSeperator = this.COMMA;
 119:        switch (this.language) {
 120:            case LanguageCode.DANISH:
 121:            case LanguageCode.NORWEGIAN:
 122:            case LanguageCode.SWEDISH:
 123:                thousandSeperator = this.PERIOD;
 124:                break;
 125:            default:
 126:                break;
 127:        }
 128:        return thousandSeperator;
 129:    }
 130:<?    
 131:    
 132:    /**
 133:     * Get the date seperator for this locale
 134:     * @return String The date seperator '-' or '/'
 135:     */
 136:?>
 137:    this.getSeperator = function() {
 138:        var seperator = this.SLASH;
 139:        switch (this.language) {
 140:            case LanguageCode.DANISH:
 141:            case LanguageCode.NORWEGIAN:
 142:            case LanguageCode.SWEDISH:
 143:                seperator = this.DASH;
 144:                break;
 145:            default:
 146:                break;
 147:        }
 148:        return seperator;
 149:    }
 150:}

Request

Den fulde kildekode for:

Request.js.php.html

Request.js.php
   1:<?
   2:/**
   3: * This class simulates the servlet Request object, in a very primitive matter
   4: * class Request {
   5: *    private void init();
   6: *    int getKey();
   7: *    void setKey(int i);
   8: *    int getValue();
   9: *    void setValue(int i);
  10: *    int getSize();
  11: *    String getParameter(string key);
  12: * }
  13: * @package javascript
  14: * @author    http://Finn-Rasmussen.com
  15: * @copyright http://Finn-Rasmussen.com
  16: * @version 1.10
  17: * @since 22-feb-2007
  18: */
  19:
  20:/**
  21: * The constructor function simulates the java request object
  22: * The Request object is instanciated with the singleton pattern
  23: * The Singleton pattern is simulated in JavaScript by:
  24: * - Using an anonymous constructor to create a function object
  25: * - And saving the returned object in an external variable
  26: * <code>
  27: * Assume the url is : test.html?Say=Hello World
  28: * usage:
  29: *   alert( Request.getParameter('Say'));
  30: * Or
  31: *   Request.setKey  (i,key); // Use custom i, key and value
  32: *   Request.setValue(i,value);
  33: *   for (var i=0;i<Request.getSize();i++) {
  34: *      alert(Request.getKey(i)+'='+Request.getValue(i));
  35: *   }
  36: *   alert(Request.getParameter(key));
  37: * </code>
  38: */
  39:?>
  40:var Request = new function() {
  41:        // proporties
  42:        this.key      = new Array();
  43:        this.value    = new Array();
  44:<?
  45:        // methods
  46:        /**
  47:         * Get the key from the request object
  48:         * @param int index The index of the array
  49:         * @return String   The key at position index
  50:         */
  51:?>
  52:        this.getKey = function(index) {
  53:                return this.key[index];
  54:        }
  55:<?
  56:        /**
  57:         * Set the key in the request object
  58:         * @param int    index The index of the array
  59:         * @param String key   The key to set
  60:         */
  61:?>
  62:        this.setKey = function(index,key) {
  63:                this.key[index] = key;
  64:        }
  65:<?
  66:        /**
  67:         * Get the value from the request object
  68:         * @param int index The index of the array
  69:         * @return String   The value at position index
  70:         */
  71:?>
  72:        this.getValue = function(index) {
  73:                return this.value[index];
  74:        }
  75:<?
  76:        /**
  77:         * Set the value in the request object
  78:         * @param int    index The index of the array
  79:         * @param String value The value to set
  80:         */
  81:?>
  82:        this.setValue = function(index,value) {
  83:                this.value[index] = value;
  84:        }
  85:<?
  86:        /**
  87:         * Get the size of the key/value pair for the request object
  88:         * @return int The size of the array
  89:         */
  90:?>
  91:        this.getSize = function() {
  92:                if (this.key.length!==this.value.length) {
  93:                        alert("Error in Request.js, function getSize(), differences found in the length
\r\n\tlength of this.key.length="+this.key.length+"
\r\n\tlength of this.value.length="+this.value.length); 94: } 95: return this.key.length; 96: } 97:<? 98: /** 99: * Simulates the java request object used in jsp/servlet 100: * Get the value from the key in the request object 101: * @param String key The key to use as lookup 102: * @return String The value associated with the key 103: */ 104:?> 105: this.getParameter = function(key) { 106: var value = ''; 107: for (var i=0;i<this.getSize();i++) { 108: if (key===this.getKey(i)) { 109: // Found a match 110: value = this.getValue(i); 111: } 112: } 113: return value; 114: } 115:<? 116: /** 117: * The init methode parses the query string and inserts the key/value pairs into the array 118: */ 119:?> 120: this.init = function() { 121: var url = '' + document.location.href; 122: var query = url.split('?'); 123: if (query[1]) { // Any query string? 124: var pairs = query[1].split('&'); // Get all key/value pairs x=a&y=b&z=c ... 125: for(var i=0;i<pairs.length;i++) { 126: var keyValue = pairs[i].split('='); // Find key/value pair x=a 127: if (keyValue[1]) { 128: this.setKey (i,keyValue[0]); // Create request elements 129: this.setValue(i,keyValue[1]); 130: } else { 131: // value = '', So skip this 132: } 133: } 134: } else { 135: // No request parameters found 136: } 137: } 138:<? 139: /** 140: * Initialize the request object with any request parameters, if any. 141: */ 142:?> 143: this.init(); 144:}

triangle.gif

Dansk

Deutch

English (UK)

France

Italy

Norsk

Svensk

English (USA)


 
blank.gif
triangle.gif Copyright @ 1999-2010 www.Finn-Rasmussen.com Powered by myPHP Version (5.2.6-1+lenny8) 1.11
blank.gif
Valid XHTML 1.0 Strict Valid CSS!