*** empty log message ***
[m17n/m17n-lib-js.git] / mim.js
diff --git a/mim.js b/mim.js
index 2461427..aee6293 100644 (file)
--- a/mim.js
+++ b/mim.js
@@ -31,7 +31,7 @@ var MIM = {
   // Boolean flag to tell if MIM is running in debug mode or not.
   debug: false,
   // List of main input methods.
-  imlist: null,
+  imlist: {},
   // List of extra input methods;
   imextra: {},
   // Global input method data
@@ -76,8 +76,8 @@ var MIM = {
   }
 };
   
-if (window.location.hostname == 'localhost')
-  MIM.server = 'http://localhost/mim';
+//if (window.location.hostname == 'localhost')
+//  MIM.server = 'http://localhost/mim';
 
 (function () {
   var keysyms = new Array ();
@@ -1010,7 +1010,7 @@ MIM.im_domain.DefType (MIM.State.prototype);
     if (node.nodeName != 'tags')
       return null;
     
-    var lang = null, name = null, extra = null;
+    var lang = null, name = null, extra = null, im;
     for (node = node.firstElement (); node; node = node.nextElement ())
       {
        if (node.nodeName == 'language')
@@ -1032,9 +1032,13 @@ MIM.im_domain.DefType (MIM.State.prototype);
        if (! (im = MIM.imextra[lang][extra]))
          return null;
       }
-    if (im.load_status != MIM.LoadStatus.Loaded
-       && (im.load_status != MIM.LoadStatus.NotLoaded || ! im.Load ()))
-      return null;
+    if (im.load_status != MIM.LoadStatus.Loaded)
+      {
+       if (im.load_status == MIM.LoadStatus.NotLoaded)
+         im.Load ();
+       if (im.load_status != MIM.LoadStatus.Loading)
+         return null;
+      }
     return im;
   }
 
@@ -1043,6 +1047,7 @@ MIM.im_domain.DefType (MIM.State.prototype);
   parsers['description'] = function (node)
   {
     this.description = node.firstChild.nodeValue;
+    return true;
   }
   parsers['variable-list'] = function (node)
   {
@@ -1057,9 +1062,11 @@ MIM.im_domain.DefType (MIM.State.prototype);
          }
        vname = Xex.Term.Parse (this.domain, node)
       }
+    return true;
   }
   parsers['command-list'] = function (node)
   {
+    return true;
   }
   parsers['macro-list'] = function (node)
   {
@@ -1068,22 +1075,28 @@ MIM.im_domain.DefType (MIM.State.prototype);
        {
          var im = include (n);
          if (! im)
-           alert ('inclusion fail');
-         else
-           for (var macro in im.domain.functions)
-             {
-               var func = im.domain.functions[macro];
-               if (func instanceof Xex.Macro)
-                 im.domain.CopyFunc (this.domain, macro);
-             }
+           {
+             alert ('inclusion fail');
+             throw new Xex.ErrTerm (MIM.Error.ParseError, "inclusion fail: ");
+           }
+         if (im.load_status == MIM.LoadStatus.Loading)
+           return false;       // force reloading
+         for (var macro in im.domain.functions)
+           {
+             var func = im.domain.functions[macro];
+             if (func instanceof Xex.Macro)
+               im.domain.CopyFunc (this.domain, macro);
+           }
          n = n.previousSibling;
          node.removeChild (n.nextSibling);
        }
     Xex.Term.Parse (this.domain, node.firstElement (), null);
+    return true;
   }
   parsers['title'] = function (node)
   {
     this.title = node.firstChild.nodeValue;
+    return true;
   }
   parsers['map-list'] = function (node)
   {
@@ -1095,8 +1108,10 @@ MIM.im_domain.DefType (MIM.State.prototype);
            if (! im)
              {
                alert ('inclusion fail');
-               continue;
+               throw new Xex.ErrTerm (MIM.Error.ParseError, "inclusion fail: ");
              }
+           else if (im.load_status == MIM.LoadStatus.Loading)
+             return false;
            for (var mname in im.map_list)
              this.map_list[mname] = im.map_list[mname];
          }
@@ -1106,6 +1121,7 @@ MIM.im_domain.DefType (MIM.State.prototype);
            this.map_list[map.name] = map;
          }
       }
+    return true;
   }
   parsers['state-list'] = function (node)
   {
@@ -1116,7 +1132,12 @@ MIM.im_domain.DefType (MIM.State.prototype);
          {
            var im = include (node);
            if (! im)
-             alert ('inclusion fail');
+             {
+               alert ('inclusion fail');
+               throw new Xex.ErrTerm (MIM.Error.ParseError, "inclusion fail: ");
+             }
+           else if (im.load_status == MIM.LoadStatus.Loading)
+             return false;
            for (var sname in im.state_list)
              {
                state = im.state_list[sname];
@@ -1136,6 +1157,7 @@ MIM.im_domain.DefType (MIM.State.prototype);
          }
       }
     delete this.domain.map_list;
+    return true;
   }
 
   MIM.IM = function (lang, name, extra_id, file)
@@ -1149,33 +1171,35 @@ MIM.im_domain.DefType (MIM.State.prototype);
                                  + (this.name != 'nil'
                                     ? this.name : this.extra_id),
                                  MIM.im_domain, null);
+  };
+
+  function load_im (node, im)
+  {
+    //alert ('Loading IM (' + im + ':' + im.lang + '-' + im.name + ')');
+    im.map_list = {};
+    im.initial_state = null;
+    im.state_list = {};
+    for (node = node.firstElement (); node; node = node.nextElement ())
+      {
+       var name = node.nodeName;
+       var parser = parsers[name];
+       if (parser && ! parser.call (im, node))
+         {
+           im.Load ();
+           return;
+         }
+      }
+    //alert ('initial state = ' + im.initial_state);
+    im.load_status = MIM.LoadStatus.Loaded;
   }
 
-  var proto = {
+  MIM.IM.prototype = {
     Load: function ()
     {
-      var node = Xex.Load (MIM.server, this.file);
-      if (! node)
-       {
-         this.load_status = MIM.LoadStatus.Error;
-         return false;
-       }
-      this.map_list = {};
-      this.initial_state = null;
-      this.state_list = {};
-      for (node = node.firstElement (); node; node = node.nextElement ())
-        {
-         var name = node.nodeName;
-         var parser = parsers[name];
-         if (parser)
-           parser.call (this, node);
-       }
-      this.load_status = MIM.LoadStatus.Loaded;
-      return true;
+      this.load_status = MIM.LoadStatus.Loading;
+      Xex.Load (MIM.server, this.file, load_im, this);
     }
-  }
-
-  MIM.IM.prototype = proto;
+  };
 
   MIM.IC = function (im, target)
   {
@@ -1688,12 +1712,9 @@ MIM.im_domain.DefType (MIM.State.prototype);
     }
   };
 
-  MIM.Load = function () {
-    if (MIM.imlist)
-      return;
-    MIM.imlist = {};
+  MIM.create_list = function (node)
+  {
     // Load the list of input methods.
-    var node = Xex.Load (MIM.server, "imlist.xml");
     for (node = node.firstChild; node; node = node.nextSibling)
       if (node.nodeName == 'input-method')
        {
@@ -1727,14 +1748,17 @@ MIM.im_domain.DefType (MIM.State.prototype);
            }
        }
     if (MIM.imextra.t && MIM.imextra.t.global)
-      MIM.im_global = MIM.imextra.t.global;
+      {
+       MIM.im_global = MIM.imextra.t.global;
+       MIM.im_global.Load ();
+      }
     else
       {
        MIM.im_global = new MIM.IM ('t', 'nil', 'global', null);
        MIM.im_global.load_status = MIM.LoadStatus.Error;
       }
-    node = undefined;
     MIM.current = MIM.imlist['t']['latn-post'];
+    MIM.current.Load ();
   }
 }) ();
 
@@ -2115,12 +2139,11 @@ MIM.keydown = function (event)
   if (! (target.type == "text" || target.type == "textarea"))
     return;
 
-  MIM.Load ();
   var ic = target.mim_ic;
   if (! ic || ic.im != MIM.current)
     {
       target.mim_ic = null;
-      Xex.Log ('creating IC');
+      Xex.Log ('creating IC for ' + MIM.current.lang + '-' + MIM.current.name);
       ic = new MIM.IC (MIM.current, target);
       if (ic.im.load_status != MIM.LoadStatus.Loaded)
        return true;
@@ -2374,6 +2397,7 @@ MIM.keypress = function (event)
     if (target.im)
       {
        MIM.current = target.im;
+       MIM.current.Load ();
        destroy ();
       }
     event.preventDefault ();
@@ -2436,7 +2460,6 @@ MIM.keypress = function (event)
       return;
     if (! menu)
       {
-       MIM.Load ();
        categorize_im ();
        menu = create_ul ('visible');
        menu.style.fontFamily = 'sans-serif';
@@ -2486,5 +2509,6 @@ MIM.keypress = function (event)
     MIM.add_event_listener (window, 'keydown', MIM.keydown);
     MIM.add_event_listener (window, 'keypress', MIM.keypress);
     MIM.add_event_listener (window, 'mousedown', create_menu);
+    Xex.Load (MIM.server, "imlist.xml", MIM.create_list);
   };
 }) ();