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

confighelper.js 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. *
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. *
  20. */
  21. var config;
  22. function Config(xhr) {
  23. function loadPreferences(xhr) {
  24. var parser = new DOMParser();
  25. var doc = parser.parseFromString(xhr.responseText, "application/xml");
  26. var preferences = doc.getElementsByTagName("preference");
  27. return Array.prototype.slice.call(preferences);
  28. }
  29. this.xhr = xhr;
  30. this.preferences = loadPreferences(this.xhr);
  31. }
  32. function readConfig(success, error) {
  33. var xhr;
  34. if(typeof config != 'undefined') {
  35. success(config);
  36. }
  37. function fail(msg) {
  38. console.error(msg);
  39. if(error) {
  40. error(msg);
  41. }
  42. }
  43. var xhrStatusChangeHandler = function() {
  44. if (xhr.readyState == 4) {
  45. if (xhr.status == 200 || xhr.status == 304 || xhr.status === 0 /* file:// */) {
  46. config = new Config(xhr);
  47. success(config);
  48. }
  49. else {
  50. fail('[Browser][cordova.js][xhrStatusChangeHandler] Could not XHR config.xml: ' + xhr.statusText);
  51. }
  52. }
  53. };
  54. xhr = new XMLHttpRequest();
  55. xhr.addEventListener("load", xhrStatusChangeHandler);
  56. try {
  57. xhr.open("get", "config.xml", true);
  58. xhr.send();
  59. } catch(e) {
  60. fail('[Browser][cordova.js][readConfig] Could not XHR config.xml: ' + JSON.stringify(e));
  61. }
  62. }
  63. /**
  64. * Reads a preference value from config.xml.
  65. * Returns preference value or undefined if it does not exist.
  66. * @param {String} preferenceName Preference name to read */
  67. Config.prototype.getPreferenceValue = function getPreferenceValue(preferenceName) {
  68. var preferenceItem = this.preferences && this.preferences.filter(function(item) {
  69. return item.attributes.name && item.attributes.name.value === preferenceName;
  70. });
  71. if(preferenceItem && preferenceItem[0] && preferenceItem[0].attributes && preferenceItem[0].attributes.value) {
  72. return preferenceItem[0].attributes.value.value;
  73. }
  74. };
  75. exports.readConfig = readConfig;