home *** CD-ROM | disk | FTP | other *** search
/ Online Praxis 1998 March / Image.iso / CD-ROM / NETSCAPE / CCK / CONF_ED.Z / cejs.jar / initpref.js < prev    next >
Encoding:
JavaScript  |  1998-02-12  |  10.2 KB  |  385 lines

  1. // Copyright (C) 1996, 1997 Netscape Communications Corporation, All rights reserved.
  2.  
  3. // --- Preference objects ---
  4. function preference(default_value) {
  5.         this.default_value = default_value;
  6.         this.locked = false;
  7.         this.changed = false;                // Admin Kit extra
  8.         this.is_leaf = true;
  9. };
  10.  
  11. function pref_node () {
  12.         this.is_leaf = false;
  13. };
  14.  
  15. // --- Preference initialization functions ---
  16. //  -- Pass a string and value to a pref function, e.g.
  17. //      user_pref("browser.startup.homepage", "abc");
  18. //  -- It parses the string and defines branches of the prefs
  19. //      object as necessary.
  20. //  -- Note assumptions:
  21. //      Root of the tree is prefs.
  22. //      Every branch is a pref_node and leaves are preference objects.
  23. //
  24. function next_field(str, index)
  25. {
  26.         var i = str.indexOf('.', index);
  27.         if (i == -1)
  28.                 i = str.length;
  29.         return str.substring(index, i)
  30. }
  31.  
  32. function init_prefnode(prefix, name)
  33. {
  34.         if (typeof(prefix[name]) == "undefined") {
  35.                         prefix[name] = new pref_node();
  36.         }
  37. }
  38.  
  39. function init_prefleaf(prefix, name)
  40. {
  41.         if (typeof(prefix[name]) == "undefined")
  42.                 prefix[name] = new preference(null);
  43. }
  44.  
  45. // creates the full path to prefname under prefs and 
  46. // returns the the preference object named by prefname
  47. function init_preftree(prefname)
  48. {
  49.   var index = 0;
  50.   var prefix = prefs;
  51.   while (index < prefname.length)
  52.   {
  53.     field = next_field(prefname, index);
  54.     index = index + field.length + 1;
  55.                
  56.     if (index >= prefname.length) {
  57.       init_prefleaf(prefix, field);
  58.     }
  59.     else {
  60.       init_prefnode(prefix, field);
  61.     }
  62.     prefix = prefix[field];
  63.   }
  64.   return prefix;
  65. }
  66.  
  67. // all.js will be calling this
  68. function pref(prefname, value)
  69. {
  70.         var pref = init_preftree(prefname);
  71.         pref.default_value = value;
  72.         pref.changed = false;
  73. }
  74.  
  75. // configURL files generated by the config editor (or otherwise)
  76. // will be calling the following two functions
  77. function default_pref(prefname, value)
  78. {
  79.         var pref = init_preftree(prefname);
  80.         
  81.         pref.default_value = value;
  82.         pref.changed = true;
  83.         return pref;
  84. }
  85.  
  86. function defaultPref(prefname, value)
  87. {
  88.   return (default_pref(prefname, value));
  89. }
  90.  
  91. function lock_pref(prefname, value)
  92. {
  93.         // Set default pref and lock it
  94.         var pref = init_preftree(prefname);
  95.         pref.default_value = value;
  96.         pref.locked = true;
  97.         pref.changed = true;
  98. }
  99.  
  100. function lockPref(prefname, value)
  101. {
  102.   lock_pref(prefname, value);
  103. }
  104.  
  105. // the config editor itself will be calling the following two functions
  106. // to maintain it's own run-time preference data structure which the
  107. // user will be interacting with.
  108. function set_pref(prefname, value, locked)
  109. {
  110.   var pref = init_preftree(prefname);
  111.  
  112.   if ((pref.default_value != value) || (pref.locked != locked))
  113.     pref.changed = true;
  114.   pref.default_value = value;
  115.   pref.locked = locked;
  116. }
  117.  
  118. // the config editor itself will be calling the following function
  119. // to maintain wipe out pref setting and turn of the "changed" flag.
  120. function clear_pref(prefname)
  121. {
  122.   var pref = init_preftree(prefname);
  123.  
  124.   pref.changed = false;
  125.   pref.default_value = null;
  126.   pref.locked = null;
  127. }
  128.  
  129. // returns null if the pref doesn't exist
  130. function get_pref(prefname)
  131. {
  132.   var index = 0;
  133.   var prefix = prefs;
  134.   while (index < prefname.length)
  135.   {
  136.     field = next_field(prefname, index);
  137.     index = index + field.length + 1;
  138.     if (typeof(prefix[field]) == "undefined")
  139.       return null;          
  140.     prefix = prefix[field];
  141.   }
  142.   return prefix;
  143. }
  144.  
  145. // This is like the func in prefapi, to escape \ and "
  146. function str_escape(str)
  147. {
  148.         var outstr = "";
  149.         var next = 0;
  150.         while (next < str.length) {
  151.                 var c1 = str.indexOf('\\', next); 
  152.                 var c2 = str.indexOf('\"', next);
  153.                 if (c1 == -1 && c2 == -1) {
  154.                         outstr += str.substring(next, str.length);
  155.                         next = str.length;
  156.                 }
  157.                 else {
  158.                         var start = next;
  159.                         if (c1 == -1)
  160.                                 next = c2;
  161.                         else if (c2 == -1)
  162.                                 next = c1;
  163.                         else
  164.                                 next = Math.min(c1, c2);
  165.                         outstr += str.substring(start, next) + "\\" + str.charAt(next);
  166.                         next++;
  167.                 }
  168.         }
  169.         return outstr;
  170. }
  171.  
  172. // does a depth-first traversal of a preference structure and
  173. // returns the JS for a configURL file that will set all the same 
  174. // settings that have been changed by the user in this preference structure.
  175. function save_uservals(obj, obj_name)
  176. {
  177.   var result = "";
  178.  
  179.   if (obj==null) return result;
  180.  
  181.   // if it's a leaf,a pref, print it out
  182.   if (obj.is_leaf == true)  
  183.   {
  184.     if ((obj.default_value != null) && (obj.changed == true))
  185.      {
  186.        if (obj.locked)
  187.           result += "lockPref(\"" + obj_name + "\", ";
  188.        else
  189.          result += "defaultPref(\"" + obj_name + "\", ";
  190.                               
  191.        if (typeof(obj.default_value) == "string")
  192.           result += "\"" + str_escape(obj.default_value) + "\");" + top.EOL;
  193.        else
  194.           // number or boolean
  195.           result += obj.default_value + ");" + top.EOL;
  196.      }
  197.   }
  198.   else   
  199.   // if it's a node traverse all it's children 
  200.   {
  201.     for (var key in obj)
  202.     {
  203.       if (key != "is_leaf")
  204.       {
  205.         if (obj_name.length == 0)
  206.           result += save_uservals(obj[key], key);
  207.         else
  208.           result += save_uservals(obj[key], obj_name + "." + key);
  209.       }
  210.     }
  211.   }
  212.   return result;
  213. }
  214.  
  215. // -- These functions are called from within prefapi.c
  216. // -- Returns true iff the pref's value has changed.
  217. // (Set creates the pref if necessary; Get assumes it already exists)
  218. function SetUserPref(prefname, value)
  219. {
  220.         var pref = init_preftree(prefname);
  221.         
  222.         if (pref.user_value != value && !pref.locked) {
  223.                 if (pref.default_value == value)
  224.                         pref.user_value = null;
  225.                 else
  226.                         pref.user_value = value;
  227.                 return true;
  228.         }
  229.         return false;
  230. }
  231.  
  232. function GetUserPref(obj)
  233. {
  234.         if (obj.locked || obj.user_value == null) {
  235.                 return obj.default_value;
  236.         }
  237.         else {
  238.                 return obj.user_value;
  239.         }
  240. }
  241.  
  242. function SetDefaultUserPref(prefname, value)
  243. {
  244.         var pref = init_preftree(prefname);
  245.         
  246.         if (pref.default_value != value) {
  247.                 pref.default_value = value;
  248.                 return true;
  249.         }
  250.         return false;
  251. }
  252.  
  253.  
  254. function mime_type()
  255. {}
  256.  
  257. // ----------------------------------------------------------------------
  258.  
  259. // --- Config objects ---
  260. function configsetting (value) {
  261.         this.value = value;
  262.         this.is_leaf = true;
  263.         this.changed = false;
  264. };
  265.  
  266. function config_array() {
  267.         this.is_leaf = false;
  268. };
  269.  
  270. // --- Config initialization functions ---
  271. //  -- Pass a string and value to a config function, e.g.
  272. //      config("directory.button1.label", "abc");
  273. //  -- It parses the string and defines branches of the configs
  274. //      object as necessary.
  275. //  -- Note assumptions:
  276. //      Root of the tree is configs.
  277. //      Every branch is a config_array and leaves are configsetting objects.
  278.  
  279. function init_configarray(prefix, name)
  280. {
  281.         if (typeof(prefix[name]) == "undefined") {
  282.                         prefix[name] = new config_array(prefix);
  283.         }
  284. }
  285.  
  286. function init_config(prefix, name)
  287. {
  288.         if (typeof(prefix[name]) == "undefined")
  289.                 prefix[name] = new configsetting(null);
  290. }
  291.  
  292. // returns the config object named by configname
  293. function init_configtree(configname)
  294. {
  295.         var index = 0;
  296.         var prefix = configs;
  297.         while (index < configname.length)
  298.         {
  299.                 field = next_field(configname, index);
  300.                 index = index + field.length + 1;
  301.                 
  302.                 if (index >= configname.length) {
  303.                         init_config(prefix, field);
  304.                 }
  305.                 else {
  306.                         init_configarray(prefix, field);
  307.                 }
  308.                 prefix = prefix[field];
  309.         }
  310.         return prefix;
  311. }
  312.  
  313. // This function will be called by defaults.js to load the factory defaults
  314. function default_config(configname, value)
  315. {
  316.         var config = init_configtree(configname);
  317.         
  318.         config.value = value;
  319.         config.changed = false;
  320.         return config;
  321. }
  322.  
  323. // This function will be called by configuration files that we open
  324. function config(configname, value)
  325. {
  326.         var config = init_configtree(configname);
  327.         
  328.         config.value = value;
  329.         config.changed = true;
  330.         return config;
  331. }
  332.  
  333.  
  334. function set_config(configname, value)
  335. {
  336.         var config = init_configtree(configname);
  337.         
  338.         if (config.value != value)
  339.         {
  340.           config.value = value;
  341.           config.changed = true;
  342.         }
  343.         return config;
  344. }
  345.  
  346.  
  347. // Admin Kit: writes default values instead.
  348.  
  349. function save_configs(obj, obj_name) { 
  350.   var result = "";
  351.  
  352.   if (obj==null) return result;
  353.  
  354.   // if it's a leaf, print it out
  355.   if (obj.is_leaf == true)  
  356.   {
  357.     if ((obj.value != null) && (obj.changed == true))
  358.      {
  359.        result += "config(\"" + obj_name + "\", ";
  360.                               
  361.        if (typeof(obj.value) == "string")
  362.           result += "\"" + str_escape(obj.value) + "\");"+top.EOL;
  363.        else
  364.           // number or boolean
  365.           result += obj.value + ");"+top.EOL;
  366.      }
  367.   }
  368.   else   
  369.   // if it's a node traverse all it's children 
  370.   {
  371.     for (var key in obj)
  372.     {
  373.       if (key != "is_leaf")
  374.       {
  375.         if (obj_name.length == 0)
  376.           result += save_configs(obj[key], key);
  377.         else
  378.           result += save_configs(obj[key], obj_name + "." + key);
  379.       }
  380.     }
  381.   }
  382.   return result;
  383. }
  384.  
  385.