PageRenderTime 45ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/src/ui-scripts/settings.js

https://github.com/runeh/dragonfly-stp-1
JavaScript | 150 lines | 109 code | 11 blank | 30 comment | 18 complexity | 1f1deaa53919602c7fdaf214119f5c0b MD5 | raw file
  1. /**
  2. * @fileoverview
  3. * Classes related to defining settings for views. Settings are dict-like
  4. * objects, providing get/set/exists methods. Settings are persisted by
  5. * calling out to the global storage ogject
  6. *
  7. * @see storage
  8. */
  9. /**
  10. * @constructor
  11. */
  12. var SettingsBase = function()
  13. {
  14. /**
  15. * Update the view according to the new value of the setting key.
  16. * @private
  17. */
  18. var syncView = function(key, value)
  19. {
  20. var
  21. switches = document.getElementsByTagName('toolbar-switches'),
  22. _switch = null,
  23. butttons = null,
  24. button = null,
  25. i = 0,
  26. j = 0,
  27. key_id = this.view_id + '.' + key,
  28. force_reflow = false;
  29. for( ; _switch = switches[i]; i++)
  30. {
  31. force_reflow = false;
  32. buttons = _switch.getElementsByTagName('button');
  33. for( j = 0; button = buttons[j]; j++)
  34. {
  35. if( button.getAttribute('key') == key_id )
  36. {
  37. button.setAttribute('is-active' , value ? "true" : "false" );
  38. force_reflow = true;
  39. }
  40. }
  41. if( force_reflow )
  42. {
  43. _switch.innerHTML += "";
  44. }
  45. }
  46. }
  47. /**
  48. * Set the value of key.
  49. */
  50. this.set = function(key, value, sync_switches)
  51. {
  52. window.localStorage.setItem(key, JSON.stringify(this.map[key] = value))
  53. if( sync_switches && typeof value == 'boolean' )
  54. {
  55. syncView(key, value);
  56. }
  57. }
  58. /**
  59. * Returns the value assosciated with "key". If the key does not exist,
  60. * returns undefined
  61. * @argument {string} key whos value to get
  62. */
  63. this.get = function(key)
  64. {
  65. var val = "";
  66. return (
  67. this.map[key] ||
  68. (this.map[key] = ((val = window.localStorage.getItem(key)) ? JSON.parse(val) : null))
  69. );
  70. }
  71. /**
  72. * Check if a particular key exist in the settings object
  73. */
  74. this.exists = function(key)
  75. {
  76. return key in this.map;
  77. }
  78. this.init = function(view_id, key_map, label_map, setting_map, templates)
  79. {
  80. this.map = {};
  81. this.view_id = view_id;
  82. this.label_map = label_map;
  83. this.setting_map = setting_map;
  84. this.templates = templates || {};
  85. var stored_map = key_map, key = '', val = '';
  86. for( key in stored_map)
  87. {
  88. val = window.localStorage.getItem(key);
  89. this.map[key] = (val === undefined || val === null) ? key_map[key] : JSON.parse(val);
  90. }
  91. if(!window.settings)
  92. {
  93. window.settings = {};
  94. }
  95. window.settings[arguments[0]] = this;
  96. }
  97. if(!window.localStorage)
  98. {
  99. window.localStorage =
  100. {
  101. setItem: function(name, value)
  102. {
  103. document.cookie = name + "="+
  104. encodeURIComponent(value)+
  105. "; expires="+(new Date(new Date().getTime()+ (360 * 24 * 60 * 60 * 1000 ))).toGMTString()+
  106. "; path=/";
  107. },
  108. getItem: function(name)
  109. {
  110. var match = null;
  111. if (match = new RegExp(name+'\=([^;]*);','').exec(document.cookie+';'))
  112. {
  113. return decodeURIComponent(match[1]);
  114. }
  115. return null;
  116. }
  117. }
  118. }
  119. }
  120. /**
  121. * @constructor
  122. * @extends SettingsBase
  123. */
  124. var Settings = function(view_id, key_map, label_map, setting_map, template)
  125. {
  126. this.init(view_id, key_map, label_map, setting_map, template);
  127. }
  128. Settings.get_setting_with_view_key_token = function(token)
  129. {
  130. var arr = token.split('.'), setting = window.settings[arr[0]], key = arr[1];
  131. return setting && setting.exists(key) && {
  132. setting: setting,
  133. view: arr[0],
  134. key: key,
  135. value: setting.get(key),
  136. label: setting.label_map[key]
  137. } || null;
  138. }
  139. Settings.prototype = new SettingsBase();