Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

page-attribute-expression.js 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. Copyright 2013-2015 ASIAL CORPORATION
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. import platform from './platform';
  14. import util from './util';
  15. var error = function error(message) {
  16. return util.throw('In PageAttributeExpression: ' + message);
  17. };
  18. var pageAttributeExpression = {
  19. _variables: {},
  20. /**
  21. * Define a variable.
  22. *
  23. * @param {String} name Name of the variable
  24. * @param {String|Function} value Value of the variable. Can be a string or a function. The function must return a string.
  25. * @param {Boolean} overwrite If this value is false, an error will be thrown when trying to define a variable that has already been defined.
  26. */
  27. defineVariable: function defineVariable(name, value) {
  28. var overwrite = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
  29. if (typeof name !== 'string') {
  30. error('Variable name must be a string');
  31. } else if (typeof value !== 'string' && typeof value !== 'function') {
  32. error('Variable value must be a string or a function');
  33. } else if (this._variables.hasOwnProperty(name) && !overwrite) {
  34. error('"' + name + '" is already defined');
  35. }
  36. this._variables[name] = value;
  37. },
  38. /**
  39. * Get a variable.
  40. *
  41. * @param {String} name Name of the variable.
  42. * @return {String|Function|null}
  43. */
  44. getVariable: function getVariable(name) {
  45. if (!this._variables.hasOwnProperty(name)) {
  46. return null;
  47. }
  48. return this._variables[name];
  49. },
  50. /**
  51. * Remove a variable.
  52. *
  53. * @param {String} name Name of the varaible.
  54. */
  55. removeVariable: function removeVariable(name) {
  56. delete this._variables[name];
  57. },
  58. /**
  59. * Get all variables.
  60. *
  61. * @return {Object}
  62. */
  63. getAllVariables: function getAllVariables() {
  64. return this._variables;
  65. },
  66. _parsePart: function _parsePart(part) {
  67. var c = void 0,
  68. inInterpolation = false,
  69. currentIndex = 0;
  70. var tokens = [];
  71. if (part.length === 0) {
  72. error('Unable to parse empty string');
  73. }
  74. for (var i = 0; i < part.length; i++) {
  75. c = part.charAt(i);
  76. if (c === '$' && part.charAt(i + 1) === '{') {
  77. if (inInterpolation) {
  78. error('Nested interpolation not supported');
  79. }
  80. var token = part.substring(currentIndex, i);
  81. if (token.length > 0) {
  82. tokens.push(part.substring(currentIndex, i));
  83. }
  84. currentIndex = i;
  85. inInterpolation = true;
  86. } else if (c === '}') {
  87. if (!inInterpolation) {
  88. error('} must be preceeded by ${');
  89. }
  90. var _token = part.substring(currentIndex, i + 1);
  91. if (_token.length > 0) {
  92. tokens.push(part.substring(currentIndex, i + 1));
  93. }
  94. currentIndex = i + 1;
  95. inInterpolation = false;
  96. }
  97. }
  98. if (inInterpolation) {
  99. error('Unterminated interpolation');
  100. }
  101. tokens.push(part.substring(currentIndex, part.length));
  102. return tokens;
  103. },
  104. _replaceToken: function _replaceToken(token) {
  105. var re = /^\${(.*?)}$/,
  106. match = token.match(re);
  107. if (match) {
  108. var name = match[1].trim();
  109. var variable = this.getVariable(name);
  110. if (variable === null) {
  111. error('Variable "' + name + '" does not exist');
  112. } else if (typeof variable === 'string') {
  113. return variable;
  114. } else {
  115. var rv = variable();
  116. if (typeof rv !== 'string') {
  117. error('Must return a string');
  118. }
  119. return rv;
  120. }
  121. } else {
  122. return token;
  123. }
  124. },
  125. _replaceTokens: function _replaceTokens(tokens) {
  126. return tokens.map(this._replaceToken.bind(this));
  127. },
  128. _parseExpression: function _parseExpression(expression) {
  129. return expression.split(',').map(function (part) {
  130. return part.trim();
  131. }).map(this._parsePart.bind(this)).map(this._replaceTokens.bind(this)).map(function (part) {
  132. return part.join('');
  133. });
  134. },
  135. /**
  136. * Evaluate an expression.
  137. *
  138. * @param {String} expression An page attribute expression.
  139. * @return {Array}
  140. */
  141. evaluate: function evaluate(expression) {
  142. if (!expression) {
  143. return [];
  144. }
  145. return this._parseExpression(expression);
  146. }
  147. };
  148. // Define default variables.
  149. pageAttributeExpression.defineVariable('mobileOS', platform.getMobileOS());
  150. pageAttributeExpression.defineVariable('iOSDevice', platform.getIOSDevice());
  151. pageAttributeExpression.defineVariable('runtime', function () {
  152. return platform.isWebView() ? 'cordova' : 'browser';
  153. });
  154. export default pageAttributeExpression;