DatePicker.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /**
  2. Phonegap DatePicker Plugin
  3. https://github.com/sectore/phonegap3-ios-datepicker-plugin
  4. Copyright (c) Greg Allen 2011
  5. Additional refactoring by Sam de Freyssinet
  6. Rewrite by Jens Krause (www.websector.de)
  7. MIT Licensed
  8. */
  9. var exec = require('cordova/exec');
  10. /**
  11. * Constructor
  12. */
  13. function DatePicker() {
  14. this._callback;
  15. }
  16. /**
  17. * Android themes
  18. * @todo Avoid error when an Android theme is define...
  19. */
  20. DatePicker.prototype.ANDROID_THEMES = {
  21. THEME_TRADITIONAL : 1, // default
  22. THEME_HOLO_DARK : 2,
  23. THEME_HOLO_LIGHT : 3,
  24. THEME_DEVICE_DEFAULT_DARK : 4,
  25. THEME_DEVICE_DEFAULT_LIGHT : 5
  26. };
  27. /**
  28. * show - true to show the ad, false to hide the ad
  29. */
  30. DatePicker.prototype.show = function(options, cb) {
  31. var padDate = function(date) {
  32. if (date.length == 1) {
  33. return ("0" + date);
  34. }
  35. return date;
  36. };
  37. var formatDate = function(date){
  38. // date/minDate/maxDate will be string at second time
  39. if (!(date instanceof Date)) {
  40. date = new Date(date)
  41. }
  42. date = date.getFullYear()
  43. + "-"
  44. + padDate(date.getMonth()+1)
  45. + "-"
  46. + padDate(date.getDate())
  47. + "T"
  48. + padDate(date.getHours())
  49. + ":"
  50. + padDate(date.getMinutes())
  51. + ":00Z";
  52. return date
  53. }
  54. if (options.date) {
  55. options.date = formatDate(options.date);
  56. }
  57. if (options.minDate) {
  58. options.minDate = formatDate(options.minDate);
  59. }
  60. if (options.maxDate) {
  61. options.maxDate = formatDate(options.maxDate);
  62. }
  63. if (options.popoverArrowDirection) {
  64. options.popoverArrowDirection = this._popoverArrowDirectionIntegerFromString(options.popoverArrowDirection);
  65. console.log('ha options', this, options.popoverArrowDirection);
  66. }
  67. var defaults = {
  68. mode: 'date',
  69. date: new Date(),
  70. allowOldDates: true,
  71. allowFutureDates: true,
  72. minDate: '',
  73. maxDate: '',
  74. doneButtonLabel: 'Done',
  75. doneButtonColor: '#007AFF',
  76. cancelButtonLabel: 'Cancel',
  77. cancelButtonColor: '#007AFF',
  78. locale: "NL",
  79. x: '0',
  80. y: '0',
  81. minuteInterval: 1,
  82. popoverArrowDirection: this._popoverArrowDirectionIntegerFromString("any"),
  83. locale: "en_US"
  84. };
  85. for (var key in defaults) {
  86. if (typeof options[key] !== "undefined")
  87. defaults[key] = options[key];
  88. }
  89. this._callback = cb;
  90. exec(null,
  91. null,
  92. "DatePicker",
  93. "show",
  94. [defaults]
  95. );
  96. };
  97. DatePicker.prototype._dateSelected = function(date) {
  98. var d = new Date(parseFloat(date) * 1000);
  99. if (this._callback)
  100. this._callback(d);
  101. };
  102. DatePicker.prototype._dateSelectionCanceled = function() {
  103. if (this._callback)
  104. this._callback();
  105. };
  106. DatePicker.prototype._UIPopoverArrowDirection = {
  107. "up": 1,
  108. "down": 2,
  109. "left": 4,
  110. "right": 8,
  111. "any": 15
  112. };
  113. DatePicker.prototype._popoverArrowDirectionIntegerFromString = function (string) {
  114. if (typeof this._UIPopoverArrowDirection[string] !== "undefined") {
  115. return this._UIPopoverArrowDirection[string];
  116. }
  117. return this._UIPopoverArrowDirection.any;
  118. };
  119. var datePicker = new DatePicker();
  120. module.exports = datePicker;
  121. // Make plugin work under window.plugins
  122. if (!window.plugins) {
  123. window.plugins = {};
  124. }
  125. if (!window.plugins.datePicker) {
  126. window.plugins.datePicker = datePicker;
  127. }