DatePicker.js 3.44 KB
Newer Older
李晓兵's avatar
李晓兵 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
/**
  Phonegap DatePicker Plugin
  https://github.com/sectore/phonegap3-ios-datepicker-plugin

  Copyright (c) Greg Allen 2011
  Additional refactoring by Sam de Freyssinet

  Rewrite by Jens Krause (www.websector.de)

  MIT Licensed
*/

var exec = require('cordova/exec');
/**
 * Constructor
 */
function DatePicker() {
    this._callback;
}

/**
 * Android themes
 * @todo Avoid error when an Android theme is define...
 */
DatePicker.prototype.ANDROID_THEMES = {
  THEME_TRADITIONAL          : 1, // default
  THEME_HOLO_DARK            : 2,
  THEME_HOLO_LIGHT           : 3,
  THEME_DEVICE_DEFAULT_DARK  : 4,
  THEME_DEVICE_DEFAULT_LIGHT : 5
};

/**
 * show - true to show the ad, false to hide the ad
 */
DatePicker.prototype.show = function(options, cb) {
    var padDate = function(date) {
      if (date.length == 1) {
        return ("0" + date);
      }
      return date;
    };

    var formatDate = function(date){
      // date/minDate/maxDate will be string at second time
      if (!(date instanceof Date)) {
        date = new Date(date)
      }
      date = date.getFullYear()
            + "-"
            + padDate(date.getMonth()+1)
            + "-"
            + padDate(date.getDate())
            + "T"
            + padDate(date.getHours())
            + ":"
            + padDate(date.getMinutes())
            + ":00Z";

      return date
    }

    if (options.date) {
        options.date = formatDate(options.date);
    }

    if (options.minDate) {
        options.minDate = formatDate(options.minDate);
    }

    if (options.maxDate) {
        options.maxDate = formatDate(options.maxDate);
    }

    if (options.popoverArrowDirection) {
        options.popoverArrowDirection = this._popoverArrowDirectionIntegerFromString(options.popoverArrowDirection);
        console.log('ha options', this, options.popoverArrowDirection);
    }

    var defaults = {
        mode: 'date',
        date: new Date(),
        allowOldDates: true,
        allowFutureDates: true,
        minDate: '',
        maxDate: '',
        doneButtonLabel: 'Done',
        doneButtonColor: '#007AFF',
        cancelButtonLabel: 'Cancel',
        cancelButtonColor: '#007AFF',
        locale: "NL",
        x: '0',
        y: '0',
        minuteInterval: 1,
        popoverArrowDirection: this._popoverArrowDirectionIntegerFromString("any"),
        locale: "en_US"
    };

    for (var key in defaults) {
        if (typeof options[key] !== "undefined")
            defaults[key] = options[key];
    }
    this._callback = cb;

    exec(null,
      null,
      "DatePicker",
      "show",
      [defaults]
    );
};

DatePicker.prototype._dateSelected = function(date) {
    var d = new Date(parseFloat(date) * 1000);
    if (this._callback)
        this._callback(d);
};

DatePicker.prototype._dateSelectionCanceled = function() {
    if (this._callback)
        this._callback();
};

DatePicker.prototype._UIPopoverArrowDirection = {
    "up": 1,
    "down": 2,
    "left": 4,
    "right": 8,
    "any": 15
};

DatePicker.prototype._popoverArrowDirectionIntegerFromString = function (string) {
    if (typeof this._UIPopoverArrowDirection[string] !== "undefined") {
        return this._UIPopoverArrowDirection[string];
    }
    return this._UIPopoverArrowDirection.any;
};



var datePicker = new DatePicker();
module.exports = datePicker;

// Make plugin work under window.plugins
if (!window.plugins) {
    window.plugins = {};
}
if (!window.plugins.datePicker) {
    window.plugins.datePicker = datePicker;
}