فهرست منبع

move moment-related functions into separate file, real methods

Adam Shaw 8 سال پیش
والد
کامیت
472dfb0b01
3فایلهای تغییر یافته به همراه187 افزوده شده و 176 حذف شده
  1. 1 0
      src.json
  2. 1 176
      src/Calendar.js
  3. 185 0
      src/Calendar.moment.js

+ 1 - 0
src.json

@@ -34,6 +34,7 @@
     "Toolbar.js",
     "Toolbar.js",
     "Calendar.js",
     "Calendar.js",
     "Calendar.options.js",
     "Calendar.options.js",
+    "Calendar.moment.js",
     "defaults.js",
     "defaults.js",
     "locale.js",
     "locale.js",
     "Header.js",
     "Header.js",

+ 1 - 176
src/Calendar.js

@@ -425,182 +425,7 @@ function Calendar_constructor(element, overrides) {
 
 
 
 
 
 
-	// Locale-data Internals
-	// -----------------------------------------------------------------------------------
-	// Apply overrides to the current locale's data
-
-	var localeData;
-
-	// Called immediately, and when any of the options change.
-	// Happens before any internal objects rebuild or rerender, because this is very core.
-	t.bindOptions([
-		'locale', 'monthNames', 'monthNamesShort', 'dayNames', 'dayNamesShort', 'firstDay', 'weekNumberCalculation'
-	], function(locale, monthNames, monthNamesShort, dayNames, dayNamesShort, firstDay, weekNumberCalculation) {
-
-		// normalize
-		if (weekNumberCalculation === 'iso') {
-			weekNumberCalculation = 'ISO'; // normalize
-		}
-
-		localeData = createObject( // make a cheap copy
-			getMomentLocaleData(locale) // will fall back to en
-		);
-
-		if (monthNames) {
-			localeData._months = monthNames;
-		}
-		if (monthNamesShort) {
-			localeData._monthsShort = monthNamesShort;
-		}
-		if (dayNames) {
-			localeData._weekdays = dayNames;
-		}
-		if (dayNamesShort) {
-			localeData._weekdaysShort = dayNamesShort;
-		}
-
-		if (firstDay == null && weekNumberCalculation === 'ISO') {
-			firstDay = 1;
-		}
-		if (firstDay != null) {
-			var _week = createObject(localeData._week); // _week: { dow: # }
-			_week.dow = firstDay;
-			localeData._week = _week;
-		}
-
-		if ( // whitelist certain kinds of input
-			weekNumberCalculation === 'ISO' ||
-			weekNumberCalculation === 'local' ||
-			typeof weekNumberCalculation === 'function'
-		) {
-			localeData._fullCalendar_weekCalc = weekNumberCalculation; // moment-ext will know what to do with it
-		}
-
-		// If the internal current date object already exists, move to new locale.
-		// We do NOT need to do this technique for event dates, because this happens when converting to "segments".
-		if (t.currentDate) {
-			localizeMoment(t.currentDate); // sets to localeData
-		}
-	});
-
-
-	// Calendar-specific Date Utilities
-	// -----------------------------------------------------------------------------------
-
-
-	t.defaultAllDayEventDuration = moment.duration(t.options.defaultAllDayEventDuration);
-	t.defaultTimedEventDuration = moment.duration(t.options.defaultTimedEventDuration);
-
-
-	// Builds a moment using the settings of the current calendar: timezone and locale.
-	// Accepts anything the vanilla moment() constructor accepts.
-	t.moment = function() {
-		var mom;
-
-		if (t.options.timezone === 'local') {
-			mom = FC.moment.apply(null, arguments);
-
-			// Force the moment to be local, because FC.moment doesn't guarantee it.
-			if (mom.hasTime()) { // don't give ambiguously-timed moments a local zone
-				mom.local();
-			}
-		}
-		else if (t.options.timezone === 'UTC') {
-			mom = FC.moment.utc.apply(null, arguments); // process as UTC
-		}
-		else {
-			mom = FC.moment.parseZone.apply(null, arguments); // let the input decide the zone
-		}
-
-		localizeMoment(mom);
-
-		return mom;
-	};
-
-
-	// Updates the given moment's locale settings to the current calendar locale settings.
-	function localizeMoment(mom) {
-		mom._locale = localeData;
-	}
-	t.localizeMoment = localizeMoment;
-
-
-	// Returns a boolean about whether or not the calendar knows how to calculate
-	// the timezone offset of arbitrary dates in the current timezone.
-	t.getIsAmbigTimezone = function() {
-		return t.options.timezone !== 'local' && t.options.timezone !== 'UTC';
-	};
-
-
-	// Returns a copy of the given date in the current timezone. Has no effect on dates without times.
-	t.applyTimezone = function(date) {
-		if (!date.hasTime()) {
-			return date.clone();
-		}
-
-		var zonedDate = t.moment(date.toArray());
-		var timeAdjust = date.time() - zonedDate.time();
-		var adjustedZonedDate;
-
-		// Safari sometimes has problems with this coersion when near DST. Adjust if necessary. (bug #2396)
-		if (timeAdjust) { // is the time result different than expected?
-			adjustedZonedDate = zonedDate.clone().add(timeAdjust); // add milliseconds
-			if (date.time() - adjustedZonedDate.time() === 0) { // does it match perfectly now?
-				zonedDate = adjustedZonedDate;
-			}
-		}
-
-		return zonedDate;
-	};
-
-
-	// Returns a moment for the current date, as defined by the client's computer or from the `now` option.
-	// Will return an moment with an ambiguous timezone.
-	t.getNow = function() {
-		var now = t.options.now;
-		if (typeof now === 'function') {
-			now = now();
-		}
-		return t.moment(now).stripZone();
-	};
-
-
-	// Get an event's normalized end date. If not present, calculate it from the defaults.
-	t.getEventEnd = function(event) {
-		if (event.end) {
-			return event.end.clone();
-		}
-		else {
-			return t.getDefaultEventEnd(event.allDay, event.start);
-		}
-	};
-
-
-	// Given an event's allDay status and start date, return what its fallback end date should be.
-	// TODO: rename to computeDefaultEventEnd
-	t.getDefaultEventEnd = function(allDay, zonedStart) {
-		var end = zonedStart.clone();
-
-		if (allDay) {
-			end.stripTime().add(t.defaultAllDayEventDuration);
-		}
-		else {
-			end.add(t.defaultTimedEventDuration);
-		}
-
-		if (t.getIsAmbigTimezone()) {
-			end.stripZone(); // we don't know what the tzo should be
-		}
-
-		return end;
-	};
-
-
-	// Produces a human-readable string for the given duration.
-	// Side-effect: changes the locale of the given duration.
-	t.humanizeDuration = function(duration) {
-		return duration.locale(t.options.locale).humanize();
-	};
+	t.initMomentInternals(); // needs to happen after options hash initialized
 
 
 
 
 
 

+ 185 - 0
src/Calendar.moment.js

@@ -0,0 +1,185 @@
+
+Calendar.mixin({
+
+	defaultAllDayEventDuration: null,
+	defaultTimedEventDuration: null,
+	localeData: null,
+
+
+	initMomentInternals: function() {
+		var _this = this;
+
+		this.defaultAllDayEventDuration = moment.duration(this.options.defaultAllDayEventDuration);
+		this.defaultTimedEventDuration = moment.duration(this.options.defaultTimedEventDuration);
+
+		// Called immediately, and when any of the options change.
+		// Happens before any internal objects rebuild or rerender, because this is very core.
+		this.bindOptions([
+			'locale', 'monthNames', 'monthNamesShort', 'dayNames', 'dayNamesShort', 'firstDay', 'weekNumberCalculation'
+		], function(locale, monthNames, monthNamesShort, dayNames, dayNamesShort, firstDay, weekNumberCalculation) {
+
+			// normalize
+			if (weekNumberCalculation === 'iso') {
+				weekNumberCalculation = 'ISO'; // normalize
+			}
+
+			var localeData = createObject( // make a cheap copy
+				getMomentLocaleData(locale) // will fall back to en
+			);
+
+			if (monthNames) {
+				localeData._months = monthNames;
+			}
+			if (monthNamesShort) {
+				localeData._monthsShort = monthNamesShort;
+			}
+			if (dayNames) {
+				localeData._weekdays = dayNames;
+			}
+			if (dayNamesShort) {
+				localeData._weekdaysShort = dayNamesShort;
+			}
+
+			if (firstDay == null && weekNumberCalculation === 'ISO') {
+				firstDay = 1;
+			}
+			if (firstDay != null) {
+				var _week = createObject(localeData._week); // _week: { dow: # }
+				_week.dow = firstDay;
+				localeData._week = _week;
+			}
+
+			if ( // whitelist certain kinds of input
+				weekNumberCalculation === 'ISO' ||
+				weekNumberCalculation === 'local' ||
+				typeof weekNumberCalculation === 'function'
+			) {
+				localeData._fullCalendar_weekCalc = weekNumberCalculation; // moment-ext will know what to do with it
+			}
+
+			_this.localeData = localeData;
+
+			// If the internal current date object already exists, move to new locale.
+			// We do NOT need to do this technique for event dates, because this happens when converting to "segments".
+			if (_this.currentDate) {
+				_this.localizeMoment(_this.currentDate); // sets to localeData
+			}
+		});
+	},
+
+
+	// Builds a moment using the settings of the current calendar: timezone and locale.
+	// Accepts anything the vanilla moment() constructor accepts.
+	moment: function() {
+		var mom;
+
+		if (this.options.timezone === 'local') {
+			mom = FC.moment.apply(null, arguments);
+
+			// Force the moment to be local, because FC.moment doesn't guarantee it.
+			if (mom.hasTime()) { // don't give ambiguously-timed moments a local zone
+				mom.local();
+			}
+		}
+		else if (this.options.timezone === 'UTC') {
+			mom = FC.moment.utc.apply(null, arguments); // process as UTC
+		}
+		else {
+			mom = FC.moment.parseZone.apply(null, arguments); // let the input decide the zone
+		}
+
+		this.localizeMoment(mom); // TODO
+
+		return mom;
+	},
+
+
+	// Updates the given moment's locale settings to the current calendar locale settings.
+	localizeMoment: function(mom) {
+		mom._locale = this.localeData;
+	},
+
+
+	// Returns a boolean about whether or not the calendar knows how to calculate
+	// the timezone offset of arbitrary dates in the current timezone.
+	getIsAmbigTimezone: function() {
+		return this.options.timezone !== 'local' && this.options.timezone !== 'UTC';
+	},
+
+
+	// Returns a copy of the given date in the current timezone. Has no effect on dates without times.
+	applyTimezone: function(date) {
+		if (!date.hasTime()) {
+			return date.clone();
+		}
+
+		var zonedDate = this.moment(date.toArray());
+		var timeAdjust = date.time() - zonedDate.time();
+		var adjustedZonedDate;
+
+		// Safari sometimes has problems with this coersion when near DST. Adjust if necessary. (bug #2396)
+		if (timeAdjust) { // is the time result different than expected?
+			adjustedZonedDate = zonedDate.clone().add(timeAdjust); // add milliseconds
+			if (date.time() - adjustedZonedDate.time() === 0) { // does it match perfectly now?
+				zonedDate = adjustedZonedDate;
+			}
+		}
+
+		return zonedDate;
+	},
+
+
+	// Returns a moment for the current date, as defined by the client's computer or from the `now` option.
+	// Will return an moment with an ambiguous timezone.
+	getNow: function() {
+		var now = this.options.now;
+		if (typeof now === 'function') {
+			now = now();
+		}
+		return this.moment(now).stripZone();
+	},
+
+
+	// Produces a human-readable string for the given duration.
+	// Side-effect: changes the locale of the given duration.
+	humanizeDuration: function(duration) {
+		return duration.locale(this.options.locale).humanize();
+	},
+
+
+
+	// Event-Specific Date Utilities. TODO: move
+	// -----------------------------------------------------------------------------------------------------------------
+
+
+	// Get an event's normalized end date. If not present, calculate it from the defaults.
+	getEventEnd: function(event) {
+		if (event.end) {
+			return event.end.clone();
+		}
+		else {
+			return this.getDefaultEventEnd(event.allDay, event.start);
+		}
+	},
+
+
+	// Given an event's allDay status and start date, return what its fallback end date should be.
+	// TODO: rename to computeDefaultEventEnd
+	getDefaultEventEnd: function(allDay, zonedStart) {
+		var end = zonedStart.clone();
+
+		if (allDay) {
+			end.stripTime().add(this.defaultAllDayEventDuration);
+		}
+		else {
+			end.add(this.defaultTimedEventDuration);
+		}
+
+		if (this.getIsAmbigTimezone()) {
+			end.stripZone(); // we don't know what the tzo should be
+		}
+
+		return end;
+	}
+
+});