UNPKG

zcy-antd

Version:

An enterprise-class UI design language and React-based implementation

1,791 lines (1,546 loc) 1.88 MB
/*! * zcy-antd v1.0.6 * * Copyright 2015-present, Alipay, Inc. * All rights reserved. */ (function webpackUniversalModuleDefinition(root, factory) { if(typeof exports === 'object' && typeof module === 'object') module.exports = factory(require("react"), require("react-dom")); else if(typeof define === 'function' && define.amd) define(["react", "react-dom"], factory); else if(typeof exports === 'object') exports["zcy-antd"] = factory(require("react"), require("react-dom")); else root["zcy-antd"] = factory(root["React"], root["ReactDOM"]); })(this, function(__WEBPACK_EXTERNAL_MODULE_1__, __WEBPACK_EXTERNAL_MODULE_4__) { return /******/ (function(modules) { // webpackBootstrap /******/ // The module cache /******/ var installedModules = {}; /******/ /******/ // The require function /******/ function __webpack_require__(moduleId) { /******/ /******/ // Check if module is in cache /******/ if(installedModules[moduleId]) /******/ return installedModules[moduleId].exports; /******/ /******/ // Create a new module (and put it into the cache) /******/ var module = installedModules[moduleId] = { /******/ exports: {}, /******/ id: moduleId, /******/ loaded: false /******/ }; /******/ /******/ // Execute the module function /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); /******/ /******/ // Flag the module as loaded /******/ module.loaded = true; /******/ /******/ // Return the exports of the module /******/ return module.exports; /******/ } /******/ /******/ /******/ // expose the modules object (__webpack_modules__) /******/ __webpack_require__.m = modules; /******/ /******/ // expose the module cache /******/ __webpack_require__.c = installedModules; /******/ /******/ // __webpack_public_path__ /******/ __webpack_require__.p = ""; /******/ /******/ // Load entry module and return exports /******/ return __webpack_require__(0); /******/ }) /************************************************************************/ /******/ ((function(modules) { // Check all modules for deduplicated modules for(var i in modules) { if(Object.prototype.hasOwnProperty.call(modules, i)) { switch(typeof modules[i]) { case "function": break; case "object": // Module can be created from a template modules[i] = (function(_m) { var args = _m.slice(1), fn = modules[_m[0]]; return function (a,b,c) { fn.apply(this, [a,b,c].concat(args)); }; }(modules[i])); break; default: // Module is a copy of another module modules[i] = modules[modules[i]]; break; } } } return modules; }([ /* 0 */ /***/ function(module, exports, __webpack_require__) { module.exports = __webpack_require__(316); /***/ }, /* 1 */ /***/ function(module, exports) { module.exports = __WEBPACK_EXTERNAL_MODULE_1__; /***/ }, /* 2 */ /***/ function(module, exports, __webpack_require__) { var __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_DEFINE_RESULT__;/*! Copyright (c) 2016 Jed Watson. Licensed under the MIT License (MIT), see http://jedwatson.github.io/classnames */ /* global define */ (function () { 'use strict'; var hasOwn = {}.hasOwnProperty; function classNames () { var classes = []; for (var i = 0; i < arguments.length; i++) { var arg = arguments[i]; if (!arg) continue; var argType = typeof arg; if (argType === 'string' || argType === 'number') { classes.push(arg); } else if (Array.isArray(arg)) { classes.push(classNames.apply(null, arg)); } else if (argType === 'object') { for (var key in arg) { if (hasOwn.call(arg, key) && arg[key]) { classes.push(key); } } } } return classes.join(' '); } if (typeof module !== 'undefined' && module.exports) { module.exports = classNames; } else if (true) { // register as 'classnames', consistent with npm package name !(__WEBPACK_AMD_DEFINE_ARRAY__ = [], __WEBPACK_AMD_DEFINE_RESULT__ = function () { return classNames; }.apply(exports, __WEBPACK_AMD_DEFINE_ARRAY__), __WEBPACK_AMD_DEFINE_RESULT__ !== undefined && (module.exports = __WEBPACK_AMD_DEFINE_RESULT__)); } else { window.classNames = classNames; } }()); /***/ }, /* 3 */ /***/ function(module, exports) { // removed by extract-text-webpack-plugin /***/ }, /* 4 */ /***/ function(module, exports) { module.exports = __WEBPACK_EXTERNAL_MODULE_4__; /***/ }, /* 5 */ /***/ function(module, exports, __webpack_require__) { 'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; exports.format = format; exports.isEmptyValue = isEmptyValue; exports.isEmptyObject = isEmptyObject; exports.asyncMap = asyncMap; exports.complementError = complementError; exports.deepMerge = deepMerge; var formatRegExp = /%[sdj%]/g; var warning = exports.warning = function warning() {}; // don't print warning message when in production env or node runtime if (("development") !== 'production' && typeof window !== 'undefined' && typeof document !== 'undefined') { exports.warning = warning = function warning(type, errors) { if (typeof console !== 'undefined' && console.warn) { if (errors.every(function (e) { return typeof e === 'string'; })) { console.warn(type, errors); } } }; } function format() { for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } var i = 1; var f = args[0]; var len = args.length; if (typeof f === 'function') { return f.apply(null, args.slice(1)); } if (typeof f === 'string') { var str = String(f).replace(formatRegExp, function (x) { if (x === '%%') { return '%'; } if (i >= len) { return x; } switch (x) { case '%s': return String(args[i++]); case '%d': return Number(args[i++]); case '%j': try { return JSON.stringify(args[i++]); } catch (_) { return '[Circular]'; } break; default: return x; } }); for (var arg = args[i]; i < len; arg = args[++i]) { str += ' ' + arg; } return str; } return f; } function isNativeStringType(type) { return type === 'string' || type === 'url' || type === 'hex' || type === 'email' || type === 'pattern'; } function isEmptyValue(value, type) { if (value === undefined || value === null) { return true; } if (type === 'array' && Array.isArray(value) && !value.length) { return true; } if (isNativeStringType(type) && typeof value === 'string' && !value) { return true; } return false; } function isEmptyObject(obj) { return Object.keys(obj).length === 0; } function asyncParallelArray(arr, func, callback) { var results = []; var total = 0; var arrLength = arr.length; function count(errors) { results.push.apply(results, errors); total++; if (total === arrLength) { callback(results); } } arr.forEach(function (a) { func(a, count); }); } function asyncSerialArray(arr, func, callback) { var index = 0; var arrLength = arr.length; function next(errors) { if (errors && errors.length) { callback(errors); return; } var original = index; index = index + 1; if (original < arrLength) { func(arr[original], next); } else { callback([]); } } next([]); } function flattenObjArr(objArr) { var ret = []; Object.keys(objArr).forEach(function (k) { ret.push.apply(ret, objArr[k]); }); return ret; } function asyncMap(objArr, option, func, callback) { if (option.first) { var flattenArr = flattenObjArr(objArr); return asyncSerialArray(flattenArr, func, callback); } var firstFields = option.firstFields || []; if (firstFields === true) { firstFields = Object.keys(objArr); } var objArrKeys = Object.keys(objArr); var objArrLength = objArrKeys.length; var total = 0; var results = []; var next = function next(errors) { results.push.apply(results, errors); total++; if (total === objArrLength) { callback(results); } }; objArrKeys.forEach(function (key) { var arr = objArr[key]; if (firstFields.indexOf(key) !== -1) { asyncSerialArray(arr, func, next); } else { asyncParallelArray(arr, func, next); } }); } function complementError(rule) { return function (oe) { if (oe && oe.message) { oe.field = oe.field || rule.fullField; return oe; } return { message: oe, field: oe.field || rule.fullField }; }; } function deepMerge(target, source) { if (source) { for (var s in source) { if (source.hasOwnProperty(s)) { var value = source[s]; if ((typeof value === 'undefined' ? 'undefined' : _typeof(value)) === 'object' && _typeof(target[s]) === 'object') { target[s] = _extends({}, target[s], value); } else { target[s] = value; } } } } return target; } /***/ }, /* 6 */ /***/ function(module, exports, __webpack_require__) { 'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; var _react = __webpack_require__(1); var _react2 = _interopRequireDefault(_react); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; } exports["default"] = function (props) { var type = props.type, _props$className = props.className, className = _props$className === undefined ? '' : _props$className, other = _objectWithoutProperties(props, ['type', 'className']); className += ' anticon anticon-' + type; return _react2["default"].createElement('i', _extends({ className: className.trim() }, other)); }; module.exports = exports['default']; /***/ }, /* 7 */ /***/ function(module, exports, __webpack_require__) { "use strict"; exports.__esModule = true; var _assign = __webpack_require__(318); var _assign2 = _interopRequireDefault(_assign); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } exports["default"] = _assign2["default"] || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; /***/ }, /* 8 */ /***/ function(module, exports, __webpack_require__) { 'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = { required: __webpack_require__(92), whitespace: __webpack_require__(192), type: __webpack_require__(191), range: __webpack_require__(190), "enum": __webpack_require__(188), pattern: __webpack_require__(189) }; module.exports = exports['default']; /***/ }, /* 9 */ /***/ function(module, exports, __webpack_require__) { 'use strict'; // export this package's api module.exports = __webpack_require__(405); /***/ }, /* 10 */ /***/ function(module, exports) { /* object-assign (c) Sindre Sorhus @license MIT */ 'use strict'; /* eslint-disable no-unused-vars */ var getOwnPropertySymbols = Object.getOwnPropertySymbols; var hasOwnProperty = Object.prototype.hasOwnProperty; var propIsEnumerable = Object.prototype.propertyIsEnumerable; function toObject(val) { if (val === null || val === undefined) { throw new TypeError('Object.assign cannot be called with null or undefined'); } return Object(val); } function shouldUseNative() { try { if (!Object.assign) { return false; } // Detect buggy property enumeration order in older V8 versions. // https://bugs.chromium.org/p/v8/issues/detail?id=4118 var test1 = new String('abc'); // eslint-disable-line no-new-wrappers test1[5] = 'de'; if (Object.getOwnPropertyNames(test1)[0] === '5') { return false; } // https://bugs.chromium.org/p/v8/issues/detail?id=3056 var test2 = {}; for (var i = 0; i < 10; i++) { test2['_' + String.fromCharCode(i)] = i; } var order2 = Object.getOwnPropertyNames(test2).map(function (n) { return test2[n]; }); if (order2.join('') !== '0123456789') { return false; } // https://bugs.chromium.org/p/v8/issues/detail?id=3056 var test3 = {}; 'abcdefghijklmnopqrst'.split('').forEach(function (letter) { test3[letter] = letter; }); if (Object.keys(Object.assign({}, test3)).join('') !== 'abcdefghijklmnopqrst') { return false; } return true; } catch (err) { // We don't expect any of the above to throw, but better to be safe. return false; } } module.exports = shouldUseNative() ? Object.assign : function (target, source) { var from; var to = toObject(target); var symbols; for (var s = 1; s < arguments.length; s++) { from = Object(arguments[s]); for (var key in from) { if (hasOwnProperty.call(from, key)) { to[key] = from[key]; } } if (getOwnPropertySymbols) { symbols = getOwnPropertySymbols(from); for (var i = 0; i < symbols.length; i++) { if (propIsEnumerable.call(from, symbols[i])) { to[symbols[i]] = from[symbols[i]]; } } } } return to; }; /***/ }, /* 11 */ /***/ function(module, exports, __webpack_require__) { /** * Copyright 2013-present, Facebook, Inc. * All rights reserved. * * This source code is licensed under the BSD-style license found in the * LICENSE file in the root directory of this source tree. An additional grant * of patent rights can be found in the PATENTS file in the same directory. */ if (true) { var REACT_ELEMENT_TYPE = (typeof Symbol === 'function' && Symbol["for"] && Symbol["for"]('react.element')) || 0xeac7; var isValidElement = function(object) { return typeof object === 'object' && object !== null && object.$$typeof === REACT_ELEMENT_TYPE; }; // By explicitly using `prop-types` you are opting into new development behavior. // http://fb.me/prop-types-in-prod var throwOnDirectAccess = true; module.exports = __webpack_require__(400)(isValidElement, throwOnDirectAccess); } else { // By explicitly using `prop-types` you are opting into new production behavior. // http://fb.me/prop-types-in-prod module.exports = require('./factoryWithThrowingShims')(); } /***/ }, /* 12 */ /***/ function(module, exports) { var core = module.exports = {version: '2.4.0'}; if(typeof __e == 'number')__e = core; // eslint-disable-line no-undef /***/ }, /* 13 */ /***/ function(module, exports, __webpack_require__) { "use strict"; exports.__esModule = true; var _defineProperty = __webpack_require__(121); var _defineProperty2 = _interopRequireDefault(_defineProperty); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } exports["default"] = function (obj, key, value) { if (key in obj) { (0, _defineProperty2["default"])(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }; /***/ }, /* 14 */ /***/ function(module, exports, __webpack_require__) { var store = __webpack_require__(76)('wks') , uid = __webpack_require__(53) , Symbol = __webpack_require__(21).Symbol , USE_SYMBOL = typeof Symbol == 'function'; var $exports = module.exports = function(name){ return store[name] || (store[name] = USE_SYMBOL && Symbol[name] || (USE_SYMBOL ? Symbol : uid)('Symbol.' + name)); }; $exports.store = store; /***/ }, /* 15 */ /***/ function(module, exports, __webpack_require__) { /* * GregorianCalendar class * @ignore * @author yiminghe@gmail.com */ 'use strict'; var toInt = parseInt; var Utils = __webpack_require__(390); var defaultLocale = __webpack_require__(141); var Const = __webpack_require__(140); /* * GregorianCalendar class. * * - no arguments: * Constructs a default GregorianCalendar using the current time * in the default time zone with the default locale. * - one argument locale: * Constructs a GregorianCalendar * based on the current time in the default time zone with the given locale. * * @class Date.Gregorian */ function GregorianCalendar(loc) { var locale = loc || defaultLocale; this.locale = locale; this.fields = []; /* * The currently set time for this date. * @protected * @type Number|undefined */ this.time = undefined; /* * The timezoneOffset in minutes used by this date. * @type Number * @protected */ this.timezoneOffset = locale.timezoneOffset; /* * The first day of the week * @type Number * @protected */ this.firstDayOfWeek = locale.firstDayOfWeek; /* * The number of days required for the first week in a month or year, * with possible values from 1 to 7. * @@protected * @type Number */ this.minimalDaysInFirstWeek = locale.minimalDaysInFirstWeek; this.fieldsComputed = false; } Utils.mix(GregorianCalendar, Const); Utils.mix(GregorianCalendar, { Utils: Utils, defaultLocale: defaultLocale, /* * Determines if the given year is a leap year. * Returns true if the given year is a leap year. To specify BC year numbers, * 1 - year number must be given. For example, year BC 4 is specified as -3. * @param {Number} year the given year. * @returns {Boolean} true if the given year is a leap year; false otherwise. * @static * @method */ isLeapYear: Utils.isLeapYear, /* * Enum indicating year field of date * @type Number */ YEAR: 1, /* * Enum indicating month field of date * @type Number */ MONTH: 2, /* * Enum indicating the day of the month * @type Number */ DAY_OF_MONTH: 3, /* * Enum indicating the hour (24). * @type Number */ HOUR_OF_DAY: 4, /* * Enum indicating the minute of the day * @type Number */ MINUTES: 5, /* * Enum indicating the second of the day * @type Number */ SECONDS: 6, /* * Enum indicating the millisecond of the day * @type Number */ MILLISECONDS: 7, /* * Enum indicating the week number within the current year * @type Number */ WEEK_OF_YEAR: 8, /* * Enum indicating the week number within the current month * @type Number */ WEEK_OF_MONTH: 9, /* * Enum indicating the day of the day number within the current year * @type Number */ DAY_OF_YEAR: 10, /* * Enum indicating the day of the week * @type Number */ DAY_OF_WEEK: 11, /* * Enum indicating the day of the ordinal number of the day of the week * @type Number */ DAY_OF_WEEK_IN_MONTH: 12, /* * Enum indicating am * @type Number */ AM: 0, /* * Enum indicating pm * @type Number */ PM: 1 }); var FIELDS = ['', 'Year', 'Month', 'DayOfMonth', 'HourOfDay', 'Minutes', 'Seconds', 'Milliseconds', 'WeekOfYear', 'WeekOfMonth', 'DayOfYear', 'DayOfWeek', 'DayOfWeekInMonth']; var YEAR = GregorianCalendar.YEAR; var MONTH = GregorianCalendar.MONTH; var DAY_OF_MONTH = GregorianCalendar.DAY_OF_MONTH; var HOUR_OF_DAY = GregorianCalendar.HOUR_OF_DAY; var MINUTE = GregorianCalendar.MINUTES; var SECONDS = GregorianCalendar.SECONDS; var MILLISECONDS = GregorianCalendar.MILLISECONDS; var DAY_OF_WEEK_IN_MONTH = GregorianCalendar.DAY_OF_WEEK_IN_MONTH; var DAY_OF_YEAR = GregorianCalendar.DAY_OF_YEAR; var DAY_OF_WEEK = GregorianCalendar.DAY_OF_WEEK; var WEEK_OF_MONTH = GregorianCalendar.WEEK_OF_MONTH; var WEEK_OF_YEAR = GregorianCalendar.WEEK_OF_YEAR; var MONTH_LENGTH = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; // 0-based var LEAP_MONTH_LENGTH = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; // 0-based var ONE_SECOND = 1000; var ONE_MINUTE = 60 * ONE_SECOND; var ONE_HOUR = 60 * ONE_MINUTE; var ONE_DAY = 24 * ONE_HOUR; var ONE_WEEK = ONE_DAY * 7; var EPOCH_OFFSET = 719163; // Fixed date of January 1, 1970 (Gregorian) var mod = Utils.mod; var _isLeapYear = Utils.isLeapYear; var floorDivide = Math.floor; var MIN_VALUES = [undefined, 1, // YEAR GregorianCalendar.JANUARY, // MONTH 1, // DAY_OF_MONTH 0, // HOUR_OF_DAY 0, // MINUTE 0, // SECONDS 0, // MILLISECONDS 1, // WEEK_OF_YEAR undefined, // WEEK_OF_MONTH 1, // DAY_OF_YEAR GregorianCalendar.SUNDAY, // DAY_OF_WEEK 1]; // DAY_OF_WEEK_IN_MONTH var MAX_VALUES = [undefined, 292278994, // YEAR GregorianCalendar.DECEMBER, // MONTH undefined, // DAY_OF_MONTH 23, // HOUR_OF_DAY 59, // MINUTE 59, // SECONDS 999, // MILLISECONDS undefined, // WEEK_OF_YEAR undefined, // WEEK_OF_MONTH undefined, // DAY_OF_YEAR GregorianCalendar.SATURDAY, // DAY_OF_WEEK undefined]; // ------------------- private start // DAY_OF_WEEK_IN_MONTH function getMonthLength(year, month) { return _isLeapYear(year) ? LEAP_MONTH_LENGTH[month] : MONTH_LENGTH[month]; } function getYearLength(year) { return _isLeapYear(year) ? 366 : 365; } function adjustDayOfMonth(self) { var fields = self.fields; var year = fields[YEAR]; var month = fields[MONTH]; var monthLen = getMonthLength(year, month); var dayOfMonth = fields[DAY_OF_MONTH]; if (dayOfMonth > monthLen) { self.set(DAY_OF_MONTH, monthLen); } } function getDayOfWeekDateOnOrBefore(fixedDate, dayOfWeek) { // 1.1.1 is monday // one week has 7 days return fixedDate - mod(fixedDate - dayOfWeek, 7); } function getWeekNumber(self, fixedDay1, fixedDate) { var fixedDay1st = getDayOfWeekDateOnOrBefore(fixedDay1 + 6, self.firstDayOfWeek); var nDays = fixedDay1st - fixedDay1; if (nDays >= self.minimalDaysInFirstWeek) { fixedDay1st -= 7; } var normalizedDayOfPeriod = fixedDate - fixedDay1st; return floorDivide(normalizedDayOfPeriod / 7) + 1; } // ------------------- private end GregorianCalendar.prototype = { constructor: GregorianCalendar, isGregorianCalendar: 1, /* * Determines if current year is a leap year. * Returns true if the given year is a leap year. To specify BC year numbers, * 1 - year number must be given. For example, year BC 4 is specified as -3. * @returns {Boolean} true if the given year is a leap year; false otherwise. * @method * @member Date.Gregorian */ isLeapYear: function isLeapYear() { return _isLeapYear(this.getYear()); }, /* * Return local info for current date instance * @returns {Object} */ getLocale: function getLocale() { return this.locale; }, /* * Returns the minimum value for * the given calendar field of this GregorianCalendar instance. * The minimum value is defined as the smallest value * returned by the get method for any possible time value, * taking into consideration the current values of the getFirstDayOfWeek, * getMinimalDaysInFirstWeek. * @param field the calendar field. * @returns {Number} the minimum value for the given calendar field. */ getActualMinimum: function getActualMinimum(field) { if (MIN_VALUES[field] !== undefined) { return MIN_VALUES[field]; } if (field === WEEK_OF_MONTH) { var cal = this.clone(); cal.clear(); cal.set(this.fields[YEAR], this.fields[MONTH], 1); return cal.get(WEEK_OF_MONTH); } throw new Error('minimum value not defined!'); }, /* * Returns the maximum value for the given calendar field * of this GregorianCalendar instance. * The maximum value is defined as the largest value returned * by the get method for any possible time value, taking into consideration * the current values of the getFirstDayOfWeek, getMinimalDaysInFirstWeek methods. * @param field the calendar field. * @returns {Number} the maximum value for the given calendar field. */ getActualMaximum: function getActualMaximum(field) { if (MAX_VALUES[field] !== undefined) { return MAX_VALUES[field]; } var value = undefined; var fields = this.fields; switch (field) { case DAY_OF_MONTH: value = getMonthLength(fields[YEAR], fields[MONTH]); break; case WEEK_OF_YEAR: var endOfYear = this.clone(); endOfYear.clear(); endOfYear.set(fields[YEAR], GregorianCalendar.DECEMBER, 31); value = endOfYear.get(WEEK_OF_YEAR); if (value === 1) { value = 52; } break; case WEEK_OF_MONTH: var endOfMonth = this.clone(); endOfMonth.clear(); endOfMonth.set(fields[YEAR], fields[MONTH], getMonthLength(fields[YEAR], fields[MONTH])); value = endOfMonth.get(WEEK_OF_MONTH); break; case DAY_OF_YEAR: value = getYearLength(fields[YEAR]); break; case DAY_OF_WEEK_IN_MONTH: value = toInt((getMonthLength(fields[YEAR], fields[MONTH]) - 1) / 7) + 1; break; default: break; } if (value === undefined) { throw new Error('maximum value not defined!'); } return value; }, /* * Determines if the given calendar field has a value set, * including cases that the value has been set by internal fields calculations * triggered by a get method call. * @param field the calendar field to be cleared. * @returns {boolean} true if the given calendar field has a value set; false otherwise. */ isSet: function isSet(field) { return this.fields[field] !== undefined; }, /* * Converts the time value (millisecond offset from the Epoch) * to calendar field values. * @protected */ computeFields: function computeFields() { var time = this.time; var timezoneOffset = this.timezoneOffset * ONE_MINUTE; var fixedDate = toInt(timezoneOffset / ONE_DAY); var timeOfDay = timezoneOffset % ONE_DAY; fixedDate += toInt(time / ONE_DAY); timeOfDay += time % ONE_DAY; if (timeOfDay >= ONE_DAY) { timeOfDay -= ONE_DAY; fixedDate++; } else { while (timeOfDay < 0) { timeOfDay += ONE_DAY; fixedDate--; } } fixedDate += EPOCH_OFFSET; var date = Utils.getGregorianDateFromFixedDate(fixedDate); var year = date.year; var fields = this.fields; fields[YEAR] = year; fields[MONTH] = date.month; fields[DAY_OF_MONTH] = date.dayOfMonth; fields[DAY_OF_WEEK] = date.dayOfWeek; if (timeOfDay !== 0) { fields[HOUR_OF_DAY] = toInt(timeOfDay / ONE_HOUR); var r = timeOfDay % ONE_HOUR; fields[MINUTE] = toInt(r / ONE_MINUTE); r %= ONE_MINUTE; fields[SECONDS] = toInt(r / ONE_SECOND); fields[MILLISECONDS] = r % ONE_SECOND; } else { fields[HOUR_OF_DAY] = fields[MINUTE] = fields[SECONDS] = fields[MILLISECONDS] = 0; } var fixedDateJan1 = Utils.getFixedDate(year, GregorianCalendar.JANUARY, 1); var dayOfYear = fixedDate - fixedDateJan1 + 1; var fixDateMonth1 = fixedDate - date.dayOfMonth + 1; fields[DAY_OF_YEAR] = dayOfYear; fields[DAY_OF_WEEK_IN_MONTH] = toInt((date.dayOfMonth - 1) / 7) + 1; var weekOfYear = getWeekNumber(this, fixedDateJan1, fixedDate); // 本周没有足够的时间在当前年 if (weekOfYear === 0) { // If the date belongs to the last week of the // previous year, use the week number of "12/31" of // the "previous" year. var fixedDec31 = fixedDateJan1 - 1; var prevJan1 = fixedDateJan1 - getYearLength(year - 1); weekOfYear = getWeekNumber(this, prevJan1, fixedDec31); } else // 本周是年末最后一周,可能有足够的时间在新的一年 if (weekOfYear >= 52) { var nextJan1 = fixedDateJan1 + getYearLength(year); var nextJan1st = getDayOfWeekDateOnOrBefore(nextJan1 + 6, this.firstDayOfWeek); var nDays = nextJan1st - nextJan1; // 本周有足够天数在新的一年 if (nDays >= this.minimalDaysInFirstWeek && // 当天确实在本周,weekOfYear === 53 时是不需要这个判断 fixedDate >= nextJan1st - 7) { weekOfYear = 1; } } fields[WEEK_OF_YEAR] = weekOfYear; fields[WEEK_OF_MONTH] = getWeekNumber(this, fixDateMonth1, fixedDate); this.fieldsComputed = true; }, /* * Converts calendar field values to the time value * (millisecond offset from the Epoch). * @protected */ computeTime: function computeTime() { var year = undefined; var fields = this.fields; if (this.isSet(YEAR)) { year = fields[YEAR]; } else { year = new Date().getFullYear(); } var timeOfDay = 0; if (this.isSet(HOUR_OF_DAY)) { timeOfDay += fields[HOUR_OF_DAY]; } timeOfDay *= 60; timeOfDay += fields[MINUTE] || 0; timeOfDay *= 60; timeOfDay += fields[SECONDS] || 0; timeOfDay *= 1000; timeOfDay += fields[MILLISECONDS] || 0; var fixedDate = 0; fields[YEAR] = year; fixedDate = fixedDate + this.getFixedDate(); // millis represents local wall-clock time in milliseconds. var millis = (fixedDate - EPOCH_OFFSET) * ONE_DAY + timeOfDay; millis -= this.timezoneOffset * ONE_MINUTE; this.time = millis; this.computeFields(); }, /* * Fills in any unset fields in the calendar fields. First, * the computeTime() method is called if the time value (millisecond offset from the Epoch) * has not been calculated from calendar field values. * Then, the computeFields() method is called to calculate all calendar field values. * @protected */ complete: function complete() { if (this.time === undefined) { this.computeTime(); } if (!this.fieldsComputed) { this.computeFields(); } }, getFixedDate: function getFixedDate() { var self = this; var fields = self.fields; var firstDayOfWeekCfg = self.firstDayOfWeek; var year = fields[YEAR]; var month = GregorianCalendar.JANUARY; if (self.isSet(MONTH)) { month = fields[MONTH]; if (month > GregorianCalendar.DECEMBER) { year += toInt(month / 12); month %= 12; } else if (month < GregorianCalendar.JANUARY) { year += floorDivide(month / 12); month = mod(month, 12); } } // Get the fixed date since Jan 1, 1 (Gregorian). We are on // the first day of either `month' or January in 'year'. var fixedDate = Utils.getFixedDate(year, month, 1); var firstDayOfWeek = undefined; var dayOfWeek = self.firstDayOfWeek; if (self.isSet(DAY_OF_WEEK)) { dayOfWeek = fields[DAY_OF_WEEK]; } if (self.isSet(MONTH)) { if (self.isSet(DAY_OF_MONTH)) { fixedDate += fields[DAY_OF_MONTH] - 1; } else { if (self.isSet(WEEK_OF_MONTH)) { firstDayOfWeek = getDayOfWeekDateOnOrBefore(fixedDate + 6, firstDayOfWeekCfg); // If we have enough days in the first week, then // move to the previous week. if (firstDayOfWeek - fixedDate >= self.minimalDaysInFirstWeek) { firstDayOfWeek -= 7; } if (dayOfWeek !== firstDayOfWeekCfg) { firstDayOfWeek = getDayOfWeekDateOnOrBefore(firstDayOfWeek + 6, dayOfWeek); } fixedDate = firstDayOfWeek + 7 * (fields[WEEK_OF_MONTH] - 1); } else { var dowim = undefined; if (self.isSet(DAY_OF_WEEK_IN_MONTH)) { dowim = fields[DAY_OF_WEEK_IN_MONTH]; } else { dowim = 1; } var lastDate = 7 * dowim; if (dowim < 0) { lastDate = getMonthLength(year, month) + 7 * (dowim + 1); } fixedDate = getDayOfWeekDateOnOrBefore(fixedDate + lastDate - 1, dayOfWeek); } } } else { // We are on the first day of the year. if (self.isSet(DAY_OF_YEAR)) { fixedDate += fields[DAY_OF_YEAR] - 1; } else if (self.isSet(WEEK_OF_YEAR)) { firstDayOfWeek = getDayOfWeekDateOnOrBefore(fixedDate + 6, firstDayOfWeekCfg); // If we have enough days in the first week, then move // to the previous week. if (firstDayOfWeek - fixedDate >= self.minimalDaysInFirstWeek) { firstDayOfWeek -= 7; } if (dayOfWeek !== firstDayOfWeekCfg) { firstDayOfWeek = getDayOfWeekDateOnOrBefore(firstDayOfWeek + 6, dayOfWeek); } fixedDate = firstDayOfWeek + 7 * (fields[WEEK_OF_YEAR] - 1); } } return fixedDate; }, /* * Returns this Calendar's time value in milliseconds * @member Date.Gregorian * @returns {Number} the current time as UTC milliseconds from the epoch. */ getTime: function getTime() { if (this.time === undefined) { this.computeTime(); } return this.time; }, /* * Sets this Calendar's current time from the given long value. * @param time the new time in UTC milliseconds from the epoch. */ setTime: function setTime(time) { this.time = time; this.fieldsComputed = false; this.complete(); }, /* * Returns the value of the given calendar field. * @param field the given calendar field. * @returns {Number} the value for the given calendar field. */ get: function get(field) { this.complete(); return this.fields[field]; }, /* * Returns the year of the given calendar field. * @method getYear * @returns {Number} the year for the given calendar field. */ /* * Returns the month of the given calendar field. * @method getMonth * @returns {Number} the month for the given calendar field. */ /* * Returns the day of month of the given calendar field. * @method getDayOfMonth * @returns {Number} the day of month for the given calendar field. */ /* * Returns the hour of day of the given calendar field. * @method getHourOfDay * @returns {Number} the hour of day for the given calendar field. */ /* * Returns the minute of the given calendar field. * @method getMinute * @returns {Number} the minute for the given calendar field. */ /* * Returns the second of the given calendar field. * @method getSecond * @returns {Number} the second for the given calendar field. */ /* * Returns the millisecond of the given calendar field. * @method getMilliSecond * @returns {Number} the millisecond for the given calendar field. */ /* * Returns the week of year of the given calendar field. * @method getWeekOfYear * @returns {Number} the week of year for the given calendar field. */ /* * Returns the week of month of the given calendar field. * @method getWeekOfMonth * @returns {Number} the week of month for the given calendar field. */ /* * Returns the day of year of the given calendar field. * @method getDayOfYear * @returns {Number} the day of year for the given calendar field. */ /* * Returns the day of week of the given calendar field. * @method getDayOfWeek * @returns {Number} the day of week for the given calendar field. */ /* * Returns the day of week in month of the given calendar field. * @method getDayOfWeekInMonth * @returns {Number} the day of week in month for the given calendar field. */ /* * Sets the given calendar field to the given value. * @param field the given calendar field. * @param v the value to be set for the given calendar field. */ set: function set(field, v) { var len = arguments.length; if (len === 2) { this.fields[field] = v; } else if (len < MILLISECONDS + 1) { for (var i = 0; i < len; i++) { this.fields[YEAR + i] = arguments[i]; } } else { throw new Error('illegal arguments for GregorianCalendar set'); } this.time = undefined; }, /* * Set the year of the given calendar field. * @method setYear */ /* * Set the month of the given calendar field. * @method setMonth */ /* * Set the day of month of the given calendar field. * @method setDayOfMonth */ /* * Set the hour of day of the given calendar field. * @method setHourOfDay */ /* * Set the minute of the given calendar field. * @method setMinute */ /* * Set the second of the given calendar field. * @method setSecond */ /* * Set the millisecond of the given calendar field. * @method setMilliSecond */ /* * Set the week of year of the given calendar field. * @method setWeekOfYear */ /* * Set the week of month of the given calendar field. * @method setWeekOfMonth */ /* * Set the day of year of the given calendar field. * @method setDayOfYear */ /* * Set the day of week of the given calendar field. * @method setDayOfWeek */ /* * Set the day of week in month of the given calendar field. * @method setDayOfWeekInMonth */ /* * add for specified field based on two rules: * * - Add rule 1. The value of field after the call minus the value of field before the * call is amount, modulo any overflow that has occurred in field * Overflow occurs when a field value exceeds its range and, * as a result, the next larger field is incremented or * decremented and the field value is adjusted back into its range. * * - Add rule 2. If a smaller field is expected to be invariant, * but it is impossible for it to be equal to its * prior value because of changes in its minimum or maximum after * field is changed, then its value is adjusted to be as close * as possible to its expected value. A smaller field represents a * smaller unit of time. HOUR_OF_DAY is a smaller field than * DAY_OF_MONTH. No adjustment is made to smaller fields * that are not expected to be invariant. The calendar system * determines what fields are expected to be invariant. * * * @example * use('date/gregorian',function(S, GregorianCalendar){ * const d = new GregorianCalendar(); * d.set(2012, GregorianCalendar.JANUARY, 31); * d.add(Gregorian.MONTH,1); * // 2012-2-29 * document.writeln('<p>'+d.getYear()+'-'+d.getMonth()+'-'+d.getDayOfWeek()) * d.add(Gregorian.MONTH,12); * // 2013-2-28 * document.writeln('<p>'+d.getYear()+'-'+d.getMonth()+'-'+d.getDayOfWeek()) * }); * * @param field the calendar field. * @param {Number} amount he amount of date or time to be added to the field. */ add: function add(field, a) { if (!a) { return; } var amount = a; var self = this; var fields = self.fields; // computer and retrieve original value var value = self.get(field); if (field === YEAR) { value += amount; self.set(YEAR, value); adjustDayOfMonth(self); } else if (field === MONTH) { value += amount; var yearAmount = floorDivide(value / 12); value = mod(value, 12); if (yearAmount) { self.set(YEAR, fields[YEAR] + yearAmount); } self.set(MONTH, value); adjustDayOfMonth(self); } else { switch (field) { case HOUR_OF_DAY: amount *= ONE_HOUR; break; case MINUTE: amount *= ONE_MINUTE; break; case SECONDS: amount *= ONE_SECOND; break; case MILLISECONDS: break; case WEEK_OF_MONTH: case WEEK_OF_YEAR: case DAY_OF_WEEK_IN_MONTH: amount *= ONE_WEEK; break; case DAY_OF_WEEK: case DAY_OF_YEAR: case DAY_OF_MONTH: amount *= ONE_DAY; break; default: throw new Error('illegal field for add'); } self.setTime(self.time + amount); } }, /* * add the year of the given calendar field. * @method addYear * @param {Number} amount the signed amount to add to field. */ /* * add the month of the given calendar field. * @method addMonth * @param {Number} amount the signed amount to add to field. */ /* * add the day of month of the given calendar field. * @method addDayOfMonth * @param {Number} amount the signed amount to add to field. */ /* * add the hour of day of the given calendar field. * @method addHourOfDay * @param {Number} amount the signed amount to add to field. */ /* * add the minute of the given calendar field. * @method addMinute * @param {Number} amount the signed amount to add to field. */ /* * add the second of the given calendar field. * @method addSecond * @param {Number} amount the signed amount to add to field. */ /* * add the millisecond of the given calendar field. * @method addMilliSecond * @param {Number} amount the signed amount to add to field. */ /* * add the week of year of the given calendar field. * @method addWeekOfYear * @param {Number} amount the signed amount to add to field. */ /* * add the week of month of the given calendar field. * @method addWeekOfMonth * @param {Number} amount the signed amount to add to field. */ /* * add the day of year of the given calendar field. * @method addDayOfYear * @param {Number} amount the signed amount to add to field. */ /* * add the day of week of the given calendar field. * @method addDayOfWeek * @param {Number} amount the signed amount to add to field. */ /* * add the day of week in month of the given calendar field. * @method addDayOfWeekInMonth * @param {Number} amount the signed amount to add to field. */ /* * Get rolled value for the field * @protected */ getRolledValue: function getRolledValue(value, a, min, max) { var amount = a; var diff = value - min; var range = max - min + 1; amount %= range; return min + (diff + amount + range) % range; }, /* * Adds a signed amount to the specified calendar field without changing larger fields. * A negative roll amount means to subtract from field without changing * larger fields. If the specified amount is 0, this method performs nothing. * * * * @example * const d = new GregorianCalendar(); * d.set(1999, GregorianCalendar.AUGUST, 31); * // 1999-4-30 * // Tuesday June 1, 1999 * d.set(1999, GregorianCalendar.JUNE, 1); * d.add(Gregorian.WEEK_OF_MONTH,-1); // === d.add(Gregorian.WEEK_OF_MONTH, * d.get(Gregorian.WEEK_OF_MONTH)); * // 1999-06-29 * * * @param field the calendar field. * @param {Number} amount the signed amount to add to field. */ roll: function roll(field, amount) { if (!amount) { return; } var self = this; // computer and retrieve original value var value = self.get(field); var min = self.getActualMinimum(field); var max = self.getActualMaximum(field); value = self.getRolledValue(value, amount, min, max); self.set(field, value); // consider compute time priority switch (field) { case MONTH: adjustDayOfMonth(self); break; default: // other fields are set already when get self.updateFieldsBySet(field); break; } }, /* * keep field stable. * * 2015-09-29 setMonth 2 vs rollSetMonth 2 * */ rollSet: function rollSet(field, v) { this.set(field, v); switch (field) { case MONTH: adjustDayOfMonth(this); break; default: // other fields are set already when get this.updateFieldsBySet(field); break; } }, /* * roll the year of the given calendar field. * @method rollYear * @param {Number} amount the signed amount to add to field. */ /* * roll the month of the given calendar field. * @param {Number} amount the signed amount to add to field. * @method rollMonth */ /* * roll the day of month of the given calendar field. * @method rollDayOfMonth * @param {Number} amount the signed amount to add to field. */ /* * roll the hour of day of the given calendar field. * @method rollHourOfDay * @param {Number} amount the signed amount to add to field. */ /* * roll the minute of the given calendar field. * @method rollMinute * @param {Number} amount the signed amount to add to field. */ /* * roll the second of the given calendar field. * @method rollSecond * @param {Number} amount the signed amount to add to field. */ /* * roll the millisecond of the given calendar field. * @method rollMilliSecond * @param {Number} amount the signed amount to add to field. */ /* * roll the week of year of the given calendar field. * @method rollWeekOfYear * @param {Number} amount the signed amount to add to field. */ /* * roll the week of month of the given calendar field. * @method rollWeekOfMonth * @param {Number} amount the signed amount to add to field. */ /* * roll the day of year of the given calendar field. * @method rollDayOfYear * @param {Number} amount the signed amount to add to field. */ /* * roll the day of week of the given calendar field. * @method rollDayOfWeek * @param {Number} amount the signed amount to add to field. */ /* * remove other priority fields when call getFixedDate * precondition: other fields are all set or computed * @protected */ updateFieldsBySet: function updateFieldsBySet(field) { var fields = this.fields; switch (field) { case WEEK_OF_MONTH: fields[DAY_OF_MONTH] = undefined; break; case DAY_OF_YEAR: fields[MONTH] = undefined; break; case DAY_OF_WEEK: fields[DAY_OF_MONTH] = undefined; break; case WEEK_OF_YEAR: fields[DAY_OF_YEAR] = undefined; fields[MONTH] = undefined; break; default: break; } }, /* * get current date instance's timezone offset * @returns {Number} */ getTimezoneOffset: function getTimezoneOffset() { return this.timezoneOffset; }, /* * set current date instance's timezone offset */ setTimezoneOffset: function setTimezoneOffset(timezoneOffset) { if (this.timezoneOffset !== timezoneOffset) { this.fieldsComputed = undefined; this.timezoneOffset = timezoneOffset; } }, /* * set first day of week for current date instance */ setFirstDayOfWeek: function setFirstDayOfWeek(firstDayOfWeek) { if (this.firstDayOfWeek !== firstDayOfWeek) { this.firstDayOfWeek = firstDayOfWeek; this.fieldsComputed = false; } }, /* * Gets what the first day of the week is; e.g., SUNDAY in the U.S., MONDAY in France. * @returns {Number} the first day of the week. */ getFirstDayOfWeek: function getFirstDayOfWeek() { return this.firstDayOfWeek; }, /* * Sets what the minimal days required in the first week of the year are; For example, * if the first week is defined as one that contains the first day of the first month of a year, * call this method with value 1. * If it must be a full week, use value 7. * @param minimalDaysInFirstWeek the given minimal days required in the first week of the year. */ setMinimalDaysInFirstWeek: function setMinimalDaysInFirstWeek(minimalDaysInFirstWeek) { if (this.minimalDaysInFirstWeek !== minimalDaysInFirstWeek) { this.minimalDaysInFirstWeek = minimalDaysInFirstWeek; this.fieldsComputed = false; } }, /* * Gets what the mini