globalization.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. *
  3. * Copyright 2013 Canonical Ltd.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing,
  12. * software distributed under the License is distributed on an
  13. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. * KIND, either express or implied. See the License for the
  15. * specific language governing permissions and limitations
  16. * under the License.
  17. *
  18. */
  19. /* global Cordova */
  20. var exec = require('cordova/exec');
  21. var argscheck = require('cordova/argscheck');
  22. var GlobalizationError = require('./GlobalizationError');
  23. function convertStringToDateOptions(override) {
  24. var options = { formatLength: 'short', selector: 'date and time' };
  25. if (override) {
  26. for (var key in options) {
  27. if (!options.hasOwnProperty(key))
  28. continue;
  29. if (typeof(options[key]) !== typeof(override[key]))
  30. continue;
  31. options[key] = override[key];
  32. }
  33. }
  34. var formats = ["short", "medium", "long", "full"];
  35. var selectors = ["date", "time", "date and time"];
  36. options.formatLength = formats.indexOf(options.formatLength);
  37. options.selector = selectors.indexOf(options.selector);
  38. //TODO: throw error
  39. if (options.formatLength === -1)
  40. options.formatLength = 0;
  41. if (options.selector === -1)
  42. options.selector = 0;
  43. return options;
  44. }
  45. function convertStringToNumberOptions(override) {
  46. var options = { type: 'decimal' };
  47. //TODO: make function
  48. if (override) {
  49. for (var key in options) {
  50. if (!options.hasOwnProperty(key))
  51. continue;
  52. if (typeof(options[key]) !== typeof(override[key]))
  53. continue;
  54. options[key] = override[key];
  55. }
  56. }
  57. var types = [ 'decimal', 'percent', 'currency' ];
  58. options.type = types.indexOf(options.type);
  59. if (options.type === -1)
  60. options.type = 0;
  61. return options;
  62. }
  63. function isInt(n) {
  64. return n % 1 === 0;
  65. }
  66. module.exports = {
  67. isDayLightSavingsTime: function(date, successCB, failureCB) {
  68. argscheck.checkArgs('dfF', 'Globalization.isDayLightSavingsTime', arguments);
  69. exec(successCB, failureCB, "Globalization", "isDayLightSavingsTime", [ { time_t: date.getTime() } ]);
  70. },
  71. dateToString: function(date, successCB, failureCB, override) {
  72. argscheck.checkArgs('dfFO', 'Globalization.dateToString', arguments);
  73. var options = convertStringToDateOptions(override);
  74. exec(successCB, failureCB, "Globalization", "dateToString",
  75. [ { time_t: date.getTime(), formatLength: options.formatLength, selector: options.selector } ]);
  76. },
  77. stringToDate: function(dateString, successCB, failureCB, override) {
  78. argscheck.checkArgs('sfFO', 'Globalization.stringToDate', arguments);
  79. var options = convertStringToDateOptions(override);
  80. exec(successCB, failureCB, "Globalization", "stringToDate",
  81. [ { dateString: dateString, formatLength: options.formatLength, selector: options.selector } ]);
  82. },
  83. getDateNames: function(successCB, failureCB, override) {
  84. argscheck.checkArgs('fFO', 'Globalization.getDateNames', arguments);
  85. var options = { type: 'wide', item: 'months' };
  86. if (override) {
  87. for (var key in options) {
  88. if (!options.hasOwnProperty(key))
  89. continue;
  90. if (typeof(options[key]) !== typeof(override[key]))
  91. continue;
  92. options[key] = override[key];
  93. }
  94. }
  95. var requests = ["days", "months"];
  96. var formats = ["narrow", "wide"];
  97. options.item = requests.indexOf(options.item);
  98. options.type = formats.indexOf(options.type);
  99. //TODO: throw error
  100. if (options.item === -1)
  101. options.item = 0;
  102. if (options.type === -1)
  103. options.type = 0;
  104. exec(successCB, failureCB, "Globalization", "getDateNames", [ options ]);
  105. },
  106. numberToString: function(number, successCB, failureCB, override) {
  107. argscheck.checkArgs('nfFO', 'Globalization.numberToString', arguments);
  108. var options = convertStringToNumberOptions(override);
  109. exec(successCB, failureCB, "Globalization", "numberToString",
  110. [ { type: options.type, isInt: isInt(number), number: number } ]);
  111. },
  112. stringToNumber: function(numberString, successCB, failureCB, override) {
  113. argscheck.checkArgs('sfFO', 'Globalization.stringToNumber', arguments);
  114. var options = convertStringToNumberOptions(override);
  115. exec(successCB, failureCB, "Globalization", "stringToNumber", [ options.type, numberString ]);
  116. },
  117. getDatePattern: function(successCB, failureCB, override) {
  118. argscheck.checkArgs('fFO', 'Globalization.getDatePattern', arguments);
  119. var options = convertStringToDateOptions(override);
  120. exec(successCB, failureCB, "Globalization", "getDatePattern", [ options.formatLength, options.selector ]);
  121. },
  122. getNumberPattern: function (successCB, failureCB, override) {
  123. argscheck.checkArgs('fFO', 'getNumberPattern', arguments);
  124. var options = convertStringToNumberOptions(override);
  125. Cordova.exec(successCB, failureCB, "Globalization", "getNumberPattern", [ options.type ]);
  126. },
  127. getCurrencyPattern: function(currencyCode, successCB, failureCB) {
  128. argscheck.checkArgs('sfF', 'Globalization.getCurrencyPattern', arguments);
  129. failureCB(new GlobalizationError(GlobalizationError.PATTERN_ERROR, "unimplemented"));
  130. //exec(successCB, failureCB, "Globalization", "getCurrencyPattern", [{"currencyCode": currencyCode}]);
  131. }
  132. };
  133. require("cordova/exec/proxy").add("navigator.globalization", module.exports);