app_config.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Distributed under the BSD license:
  3. *
  4. * Copyright (c) 2010, Ajax.org B.V.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * * Neither the name of Ajax.org B.V. nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL AJAX.ORG B.V. BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * ***** END LICENSE BLOCK ***** */
  30. define(function(require, exports, module) {
  31. "no use strict";
  32. var oop = require("./oop");
  33. var EventEmitter = require("./event_emitter").EventEmitter;
  34. var optionsProvider = {
  35. setOptions: function(optList) {
  36. Object.keys(optList).forEach(function(key) {
  37. this.setOption(key, optList[key]);
  38. }, this);
  39. },
  40. getOptions: function(optionNames) {
  41. var result = {};
  42. if (!optionNames) {
  43. optionNames = Object.keys(this.$options);
  44. } else if (!Array.isArray(optionNames)) {
  45. result = optionNames;
  46. optionNames = Object.keys(result);
  47. }
  48. optionNames.forEach(function(key) {
  49. result[key] = this.getOption(key);
  50. }, this);
  51. return result;
  52. },
  53. setOption: function(name, value) {
  54. if (this["$" + name] === value)
  55. return;
  56. var opt = this.$options[name];
  57. if (!opt) {
  58. return warn('misspelled option "' + name + '"');
  59. }
  60. if (opt.forwardTo)
  61. return this[opt.forwardTo] && this[opt.forwardTo].setOption(name, value);
  62. if (!opt.handlesSet)
  63. this["$" + name] = value;
  64. if (opt && opt.set)
  65. opt.set.call(this, value);
  66. },
  67. getOption: function(name) {
  68. var opt = this.$options[name];
  69. if (!opt) {
  70. return warn('misspelled option "' + name + '"');
  71. }
  72. if (opt.forwardTo)
  73. return this[opt.forwardTo] && this[opt.forwardTo].getOption(name);
  74. return opt && opt.get ? opt.get.call(this) : this["$" + name];
  75. }
  76. };
  77. function warn(message) {
  78. if (typeof console != "undefined" && console.warn)
  79. console.warn.apply(console, arguments);
  80. }
  81. function reportError(msg, data) {
  82. var e = new Error(msg);
  83. e.data = data;
  84. if (typeof console == "object" && console.error)
  85. console.error(e);
  86. setTimeout(function() { throw e; });
  87. }
  88. var AppConfig = function() {
  89. this.$defaultOptions = {};
  90. };
  91. (function() {
  92. // module loading
  93. oop.implement(this, EventEmitter);
  94. /*
  95. * option {name, value, initialValue, setterName, set, get }
  96. */
  97. this.defineOptions = function(obj, path, options) {
  98. if (!obj.$options)
  99. this.$defaultOptions[path] = obj.$options = {};
  100. Object.keys(options).forEach(function(key) {
  101. var opt = options[key];
  102. if (typeof opt == "string")
  103. opt = {forwardTo: opt};
  104. opt.name || (opt.name = key);
  105. obj.$options[opt.name] = opt;
  106. if ("initialValue" in opt)
  107. obj["$" + opt.name] = opt.initialValue;
  108. });
  109. // implement option provider interface
  110. oop.implement(obj, optionsProvider);
  111. return this;
  112. };
  113. this.resetOptions = function(obj) {
  114. Object.keys(obj.$options).forEach(function(key) {
  115. var opt = obj.$options[key];
  116. if ("value" in opt)
  117. obj.setOption(key, opt.value);
  118. });
  119. };
  120. this.setDefaultValue = function(path, name, value) {
  121. var opts = this.$defaultOptions[path] || (this.$defaultOptions[path] = {});
  122. if (opts[name]) {
  123. if (opts.forwardTo)
  124. this.setDefaultValue(opts.forwardTo, name, value);
  125. else
  126. opts[name].value = value;
  127. }
  128. };
  129. this.setDefaultValues = function(path, optionHash) {
  130. Object.keys(optionHash).forEach(function(key) {
  131. this.setDefaultValue(path, key, optionHash[key]);
  132. }, this);
  133. };
  134. this.warn = warn;
  135. this.reportError = reportError;
  136. }).call(AppConfig.prototype);
  137. exports.AppConfig = AppConfig;
  138. });