www

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | Submodules | README | LICENSE

timeline-api.js (9506B)


      1 /*	==================================================
      2 	Timeline API
      3 	
      4 	http://simile.mit.edu/timeline/api/timeline-api.js
      5 	
      6 	This file will load all the Javascript files
      7 	necessary to make the standard timeline work.
      8 	It also detects the default locale.
      9 	==================================================
     10 	
     11 	© Copyright The SIMILE Project 2003-2005.
     12 	http://simile.mit.edu/
     13 	
     14 	Redistribution and use in source and binary forms, with or without
     15 	modification, are permitted provided that the following conditions
     16 	are met:
     17 	
     18 	1. Redistributions of source code must retain the above copyright
     19 	   notice, this list of conditions and the following disclaimer.
     20 	
     21 	2. Redistributions in binary form must reproduce the above copyright
     22 	   notice, this list of conditions and the following disclaimer in the
     23 	   documentation and/or other materials provided with the distribution.
     24 	
     25 	3. The name of the author may not be used to endorse or promote products
     26 	   derived from this software without specific prior written permission.
     27 	
     28 	THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     29 	IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     30 	OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     31 	IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     32 	INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     33 	NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     34 	DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     35 	THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     36 	(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     37 	THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     38 */
     39  
     40 var Timeline = new Object();
     41 Timeline.Platform = new Object();
     42 
     43     /*
     44         HACK: We need these 2 things here because we cannot simply append
     45         a <script> element containing code that accesses Timeline.Platform
     46         to initialize it because IE executes that <script> code first
     47         before it loads timeline.js and util/platform.js.
     48     */
     49 
     50 (function() {
     51     var bundle = true;
     52     var javascriptFiles = [
     53         "timeline.js",
     54         
     55         "util/platform.js",
     56         "util/debug.js",
     57         "util/xmlhttp.js",
     58         "util/dom.js",
     59         "util/graphics.js",
     60         "util/date-time.js",
     61         "util/data-structure.js",
     62         "util/html.js",
     63         
     64         "units.js",
     65         "themes.js",
     66         "ethers.js",
     67         "ether-painters.js",
     68         "labellers.js",
     69         "sources.js",
     70         "layouts.js",
     71         "painters.js",
     72         "decorators.js"
     73     ];
     74     var cssFiles = [
     75         "timeline.css",
     76         "ethers.css",
     77         "events.css"
     78     ];
     79     
     80     /*
     81 		Modified by Ben for Zotero
     82 	*/
     83     
     84 
     85 
     86     // ISO-639 language codes, ISO-3166 country codes (2 characters)
     87     var supportedLocales = [
     88         "cs",       // Czech
     89         "de",       // German
     90         "en",       // English
     91         "es",       // Spanish
     92         "fr",       // French
     93         "it",       // Italian
     94         "ru",       // Russian
     95         "se",       // Swedish
     96         "vi",       // Vietnamese
     97         "zh"        // Chinese
     98     ];
     99     
    100     try {
    101         var desiredLocales = [ "en" ];
    102         var defaultServerLocale = "en";
    103         
    104         var parseURLParameters = function(parameters) {
    105             var params = parameters.split("&");
    106             for (var p = 0; p < params.length; p++) {
    107                 var pair = params[p].split("=");
    108                 if (pair[0] == "locales") {
    109                     desiredLocales = desiredLocales.concat(pair[1].split(","));
    110                 } else if (pair[0] == "defaultLocale") {
    111                     defaultServerLocale = pair[1];
    112                 } else if (pair[0] == "bundle") {
    113                     bundle = pair[1] != "false";
    114                 }
    115             }
    116         };
    117         
    118         (function() {
    119             if (typeof Timeline_urlPrefix == "string") {
    120                 Timeline.urlPrefix = Timeline_urlPrefix;
    121                 if (typeof Timeline_parameters == "string") {
    122                     parseURLParameters(Timeline_parameters);
    123                 }
    124             } else {
    125                 var heads = document.documentElement.getElementsByTagName("head");
    126                 for (var h = 0; h < heads.length; h++) {
    127                     var scripts = heads[h].getElementsByTagName("script");
    128                     for (var s = 0; s < scripts.length; s++) {
    129                         var url = scripts[s].src;
    130                         var i = url.indexOf("timeline-api.js");
    131                         if (i >= 0) {
    132                             Timeline.urlPrefix = url.substr(0, i);
    133                             var q = url.indexOf("?");
    134                             if (q > 0) {
    135                                 parseURLParameters(url.substr(q + 1));
    136                             }
    137                             return;
    138                         }
    139                     }
    140                 }
    141                 throw new Error("Failed to derive URL prefix for Timeline API code files");
    142             }
    143         })();
    144         
    145         var includeJavascriptFiles;
    146         var includeCssFiles;
    147         if ("SimileAjax" in window) {
    148             includeJavascriptFiles = function(urlPrefix, filenames) {
    149                 SimileAjax.includeJavascriptFiles(document, urlPrefix, filenames);
    150             }
    151             includeCssFiles = function(urlPrefix, filenames) {
    152                 SimileAjax.includeCssFiles(document, urlPrefix, filenames);
    153             }
    154         } else {
    155             var getHead = function() {
    156                 return document.getElementsByTagName("head")[0];
    157             };
    158             var includeJavascriptFile = function(url) {
    159                 if (document.body == null) {
    160                     try {
    161                         document.write("<script src='" + url + "' type='text/javascript'></script>");
    162                         return;
    163                     } catch (e) {
    164                         // fall through
    165                     }
    166                 }
    167                 
    168                 var script = document.createElement("script");
    169                 script.type = "text/javascript";
    170                 script.language = "JavaScript";
    171                 script.src = url;
    172                 getHead().appendChild(script);
    173             };
    174             var includeCssFile = function(url) {
    175                 if (document.body == null) {
    176                     try {
    177                         document.write("<link rel='stylesheet' href='" + url + "' type='text/css'/>");
    178                         return;
    179                     } catch (e) {
    180                         // fall through
    181                     }
    182                 }
    183                 
    184                 var link = document.createElement("link");
    185                 link.setAttribute("rel", "stylesheet");
    186                 link.setAttribute("type", "text/css");
    187                 link.setAttribute("href", url);
    188                 getHead().appendChild(link);
    189             }
    190             
    191             includeJavascriptFiles = function(urlPrefix, filenames) {
    192                 for (var i = 0; i < filenames.length; i++) {
    193                     includeJavascriptFile(urlPrefix + filenames[i]);
    194                 }
    195             };
    196             includeCssFiles = function(urlPrefix, filenames) {
    197                 for (var i = 0; i < filenames.length; i++) {
    198                     includeCssFile(urlPrefix + filenames[i]);
    199                 }
    200             };
    201         }
    202         
    203         /*
    204          *  Include non-localized files
    205          */
    206         if (bundle) {
    207             includeJavascriptFiles(Timeline.urlPrefix, [ "bundle.js" ]);
    208             includeCssFiles(Timeline.urlPrefix, [ "bundle.css" ]);
    209         } else {
    210             includeJavascriptFiles(Timeline.urlPrefix + "scripts/", javascriptFiles);
    211             includeCssFiles(Timeline.urlPrefix + "styles/", cssFiles);
    212         }
    213         
    214         /*
    215          *  Include localized files
    216          */
    217         var loadLocale = [];
    218         loadLocale[defaultServerLocale] = true;
    219         
    220         var tryExactLocale = function(locale) {
    221             for (var l = 0; l < supportedLocales.length; l++) {
    222                 if (locale == supportedLocales[l]) {
    223                     loadLocale[locale] = true;
    224                     return true;
    225                 }
    226             }
    227             return false;
    228         }
    229         var tryLocale = function(locale) {
    230             if (tryExactLocale(locale)) {
    231                 return locale;
    232             }
    233             
    234             var dash = locale.indexOf("-");
    235             if (dash > 0 && tryExactLocale(locale.substr(0, dash))) {
    236                 return locale.substr(0, dash);
    237             }
    238             
    239             return null;
    240         }
    241         
    242         for (var l = 0; l < desiredLocales.length; l++) {
    243             tryLocale(desiredLocales[l]);
    244         }
    245         
    246         var defaultClientLocale = defaultServerLocale;
    247         var defaultClientLocales = ("language" in navigator ? navigator.language : navigator.browserLanguage).split(";");
    248         for (var l = 0; l < defaultClientLocales.length; l++) {
    249             var locale = tryLocale(defaultClientLocales[l]);
    250             if (locale != null) {
    251                 defaultClientLocale = locale;
    252                 break;
    253             }
    254         }
    255         
    256         /*
    257 			Modified by Ben for Zotero
    258 		*/
    259         
    260 
    261 
    262         Timeline.Platform.serverLocale = defaultServerLocale;
    263         Timeline.Platform.clientLocale = defaultClientLocale;
    264     } catch (e) {
    265         alert(e);
    266     }
    267 })();