# all files / keystone/fields/types/datetime/ DatetimeType.js

94.74% Statements 54/57
70.21% Branches 33/47
100% Functions 5/5
96.43% Lines 54/56

    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
    1121×
    1×
    1×
    1×
    1×
    1×

    1×

    1×
    14×
    14×
    14×
    14×
    14×
    14×
    14×
    14×
    14×
    1×

    13×
    13×

    1×

    1×
    1×
    1×
    1×

    1×

    1×
    54×
    54×
    54×
    3×

    51×

    1×
    14×

    14×
    14×
    11×

    14×

    1×
    10×
    10×
    10×

    10×

    10×

    1×

    14×
    14×
    3×

    1×

    1×
    1×

    2×

    14×

    1×
     var moment = require('moment');
    var DateType = require('../date/DateType');
    var FieldType = require('../Type');
    var util = require('util');
    var utils = require('keystone-utils');
    var TextType = require('../text/TextType');

    // ISO_8601 is needed for the automatically created createdAt and updatedAt fields
    var parseFormats = ['YYYY-MM-DD', 'YYYY-MM-DD h:m:s a', 'YYYY-MM-DD h:m a', 'YYYY-MM-DD H:m:s', 'YYYY-MM-DD H:m', moment.ISO_8601];
    /**
     * DateTime FieldType Constructor
     * @extends Field
     * @api public
     */
    function datetime (list, path, options) {
    	this._nativeType = Date;
    	this._underscoreMethods = ['format', 'moment', 'parse'];
    	this._fixedSize = 'full';
    	this._properties = ['formatString', 'isUTC'];
    	this.typeDescription = 'date and time';
    	this.parseFormatString = options.parseFormat || parseFormats;
    	this.formatString = (options.format === false) ? false : (options.format || 'YYYY-MM-DD h:m:s a');
    	this.isUTC = options.utc || false;
    	if (this.formatString && typeof this.formatString !== 'string') {
    		throw new Error('FieldType.DateTime: options.format must be a string.');
    	}
    	datetime.super_.call(this, list, path, options);
    	this.paths = {
    		date: this._path.append('_date'),
    		time: this._path.append('_time'),
    	};
    }
    util.inherits(datetime, FieldType);

    /* Inherit generic methods */
    datetime.prototype.format = DateType.prototype.format;
    datetime.prototype.moment = DateType.prototype.moment;
    datetime.prototype.parse = DateType.prototype.parse;
    datetime.prototype.addFilterToQuery = DateType.prototype.addFilterToQuery;

    datetime.prototype.validateRequiredInput = TextType.prototype.validateRequiredInput;

    /**
     * Get the value from a data object; may be simple or a pair of fields
     */
    datetime.prototype.getInputFromData = function (data) {
    	var dateValue = this.getValueFromData(data, '_date');
    	var timeValue = this.getValueFromData(data, '_time');
    	if (dateValue && timeValue) {
    		return dateValue + ' ' + timeValue;
    	}
    	return this.getValueFromData(data);
    };

    /**
     * Validates the input we get to be a valid date,
     * undefined, null or an empty string
     */
    datetime.prototype.validateInput = function (data, callback) {
    	var value = this.getInputFromData(data);
    	// If the value is null, undefined or an empty string
    	// bail early since updateItem sanitizes that just fine
    	var result = true;
    	if (value) {
    		result = this.parse(value, this.parseFormatString, true).isValid();
    	}
    	utils.defer(callback, result);
    };

    /**
     * Checks that a valid date has been provided in a data object
     * An empty value clears the stored value and is considered valid
     *
     * Deprecated
     */
    datetime.prototype.inputIsValid = function (data, required, item) {
    	Iif (!(this.path in data && !(this.paths.date in data && this.paths.time in data)) && item && item.get(this.path)) return true;
    	var newValue = moment(this.getInputFromData(data), parseFormats);
    	Iif (required && (!newValue || !newValue.isValid())) {
    		return false;
    	} else Iif (this.getInputFromData(data) && newValue && !newValue.isValid()) {
    		return false;
    	} else {
    		return true;
    	}
    };

    /**
     * Updates the value for this field in the item from a data object
     */
    datetime.prototype.updateItem = function (item, data, callback) {
    	// Get the values from the data
    	var value = this.getInputFromData(data);
    	if (value !== undefined) {
    		if (value !== null && value !== '') {
    			// If the value is not null, empty string or undefined, parse it
    			var newValue = this.parse(value, this.parseFormatString, true);
    			// If it's valid and not the same as the last value, save it
    			Eif (!item.get(this.path) || !newValue.isSame(item.get(this.path))) {
    				item.set(this.path, newValue.toDate());
    			}
    		// If it's null or empty string, clear it out
    		} else {
    			item.set(this.path, null);
    		}
    	}
    	process.nextTick(callback);
    };

    /* Export Field Type */
    module.exports = datetime;