| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453 |
- // exports
- fc.intersectionToSeg = intersectionToSeg;
- fc.applyAll = applyAll;
- fc.debounce = debounce;
- /* FullCalendar-specific DOM Utilities
- ----------------------------------------------------------------------------------------------------------------------*/
- // Given the scrollbar widths of some other container, create borders/margins on rowEls in order to match the left
- // and right space that was offset by the scrollbars. A 1-pixel border first, then margin beyond that.
- function compensateScroll(rowEls, scrollbarWidths) {
- if (scrollbarWidths.left) {
- rowEls.css({
- 'border-left-width': 1,
- 'margin-left': scrollbarWidths.left - 1
- });
- }
- if (scrollbarWidths.right) {
- rowEls.css({
- 'border-right-width': 1,
- 'margin-right': scrollbarWidths.right - 1
- });
- }
- }
- // Undoes compensateScroll and restores all borders/margins
- function uncompensateScroll(rowEls) {
- rowEls.css({
- 'margin-left': '',
- 'margin-right': '',
- 'border-left-width': '',
- 'border-right-width': ''
- });
- }
- // Make the mouse cursor express that an event is not allowed in the current area
- function disableCursor() {
- $('body').addClass('fc-not-allowed');
- }
- // Returns the mouse cursor to its original look
- function enableCursor() {
- $('body').removeClass('fc-not-allowed');
- }
- // Given a total available height to fill, have `els` (essentially child rows) expand to accomodate.
- // By default, all elements that are shorter than the recommended height are expanded uniformly, not considering
- // any other els that are already too tall. if `shouldRedistribute` is on, it considers these tall rows and
- // reduces the available height.
- function distributeHeight(els, availableHeight, shouldRedistribute) {
- // *FLOORING NOTE*: we floor in certain places because zoom can give inaccurate floating-point dimensions,
- // and it is better to be shorter than taller, to avoid creating unnecessary scrollbars.
- var minOffset1 = Math.floor(availableHeight / els.length); // for non-last element
- var minOffset2 = Math.floor(availableHeight - minOffset1 * (els.length - 1)); // for last element *FLOORING NOTE*
- var flexEls = []; // elements that are allowed to expand. array of DOM nodes
- var flexOffsets = []; // amount of vertical space it takes up
- var flexHeights = []; // actual css height
- var usedHeight = 0;
- undistributeHeight(els); // give all elements their natural height
- // find elements that are below the recommended height (expandable).
- // important to query for heights in a single first pass (to avoid reflow oscillation).
- els.each(function(i, el) {
- var minOffset = i === els.length - 1 ? minOffset2 : minOffset1;
- var naturalOffset = $(el).outerHeight(true);
- if (naturalOffset < minOffset) {
- flexEls.push(el);
- flexOffsets.push(naturalOffset);
- flexHeights.push($(el).height());
- }
- else {
- // this element stretches past recommended height (non-expandable). mark the space as occupied.
- usedHeight += naturalOffset;
- }
- });
- // readjust the recommended height to only consider the height available to non-maxed-out rows.
- if (shouldRedistribute) {
- availableHeight -= usedHeight;
- minOffset1 = Math.floor(availableHeight / flexEls.length);
- minOffset2 = Math.floor(availableHeight - minOffset1 * (flexEls.length - 1)); // *FLOORING NOTE*
- }
- // assign heights to all expandable elements
- $(flexEls).each(function(i, el) {
- var minOffset = i === flexEls.length - 1 ? minOffset2 : minOffset1;
- var naturalOffset = flexOffsets[i];
- var naturalHeight = flexHeights[i];
- var newHeight = minOffset - (naturalOffset - naturalHeight); // subtract the margin/padding
- if (naturalOffset < minOffset) { // we check this again because redistribution might have changed things
- $(el).height(newHeight);
- }
- });
- }
- // Undoes distrubuteHeight, restoring all els to their natural height
- function undistributeHeight(els) {
- els.height('');
- }
- // Given `els`, a jQuery set of <td> cells, find the cell with the largest natural width and set the widths of all the
- // cells to be that width.
- // PREREQUISITE: if you want a cell to take up width, it needs to have a single inner element w/ display:inline
- function matchCellWidths(els) {
- var maxInnerWidth = 0;
- els.find('> *').each(function(i, innerEl) {
- var innerWidth = $(innerEl).outerWidth();
- if (innerWidth > maxInnerWidth) {
- maxInnerWidth = innerWidth;
- }
- });
- maxInnerWidth++; // sometimes not accurate of width the text needs to stay on one line. insurance
- els.width(maxInnerWidth);
- return maxInnerWidth;
- }
- // Turns a container element into a scroller if its contents is taller than the allotted height.
- // Returns true if the element is now a scroller, false otherwise.
- // NOTE: this method is best because it takes weird zooming dimensions into account
- function setPotentialScroller(containerEl, height) {
- containerEl.height(height).addClass('fc-scroller');
- // are scrollbars needed?
- if (containerEl[0].scrollHeight - 1 > containerEl[0].clientHeight) { // !!! -1 because IE is often off-by-one :(
- return true;
- }
- unsetScroller(containerEl); // undo
- return false;
- }
- // Takes an element that might have been a scroller, and turns it back into a normal element.
- function unsetScroller(containerEl) {
- containerEl.height('').removeClass('fc-scroller');
- }
- /* General DOM Utilities
- ----------------------------------------------------------------------------------------------------------------------*/
- // borrowed from https://github.com/jquery/jquery-ui/blob/1.11.0/ui/core.js#L51
- function getScrollParent(el) {
- var position = el.css('position'),
- scrollParent = el.parents().filter(function() {
- var parent = $(this);
- return (/(auto|scroll)/).test(
- parent.css('overflow') + parent.css('overflow-y') + parent.css('overflow-x')
- );
- }).eq(0);
- return position === 'fixed' || !scrollParent.length ? $(el[0].ownerDocument || document) : scrollParent;
- }
- // Given a container element, return an object with the pixel values of the left/right scrollbars.
- // Left scrollbars might occur on RTL browsers (IE maybe?) but I have not tested.
- // PREREQUISITE: container element must have a single child with display:block
- function getScrollbarWidths(container) {
- var containerLeft = container.offset().left;
- var containerRight = containerLeft + container.width();
- var inner = container.children();
- var innerLeft = inner.offset().left;
- var innerRight = innerLeft + inner.outerWidth();
- return {
- left: innerLeft - containerLeft,
- right: containerRight - innerRight
- };
- }
- // Returns a boolean whether this was a left mouse click and no ctrl key (which means right click on Mac)
- function isPrimaryMouseButton(ev) {
- return ev.which == 1 && !ev.ctrlKey;
- }
- /* FullCalendar-specific Misc Utilities
- ----------------------------------------------------------------------------------------------------------------------*/
- // Creates a basic segment with the intersection of the two ranges. Returns undefined if no intersection.
- // Expects all dates to be normalized to the same timezone beforehand.
- // TODO: move to date section?
- function intersectionToSeg(subjectRange, constraintRange) {
- var subjectStart = subjectRange.start;
- var subjectEnd = subjectRange.end;
- var constraintStart = constraintRange.start;
- var constraintEnd = constraintRange.end;
- var segStart, segEnd;
- var isStart, isEnd;
- if (subjectEnd > constraintStart && subjectStart < constraintEnd) { // in bounds at all?
- if (subjectStart >= constraintStart) {
- segStart = subjectStart.clone();
- isStart = true;
- }
- else {
- segStart = constraintStart.clone();
- isStart = false;
- }
- if (subjectEnd <= constraintEnd) {
- segEnd = subjectEnd.clone();
- isEnd = true;
- }
- else {
- segEnd = constraintEnd.clone();
- isEnd = false;
- }
- return {
- start: segStart,
- end: segEnd,
- isStart: isStart,
- isEnd: isEnd
- };
- }
- }
- /* Date Utilities
- ----------------------------------------------------------------------------------------------------------------------*/
- var dayIDs = [ 'sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat' ];
- var intervalUnits = [ 'year', 'month', 'week', 'day', 'hour', 'minute', 'second', 'millisecond' ];
- // Diffs the two moments into a Duration where full-days are recorded first, then the remaining time.
- // Moments will have their timezones normalized.
- function diffDayTime(a, b) {
- return moment.duration({
- days: a.clone().stripTime().diff(b.clone().stripTime(), 'days'),
- ms: a.time() - b.time() // time-of-day from day start. disregards timezone
- });
- }
- // Diffs the two moments via their start-of-day (regardless of timezone). Produces whole-day durations.
- function diffDay(a, b) {
- return moment.duration({
- days: a.clone().stripTime().diff(b.clone().stripTime(), 'days')
- });
- }
- // Computes the unit name of the largest whole-unit period of time.
- // For example, 48 hours will be "days" whereas 49 hours will be "hours".
- // Accepts start/end, a range object, or an original duration object.
- function computeIntervalUnit(start, end) {
- var i, unit;
- var val;
- for (i = 0; i < intervalUnits.length; i++) {
- unit = intervalUnits[i];
- val = computeRangeAs(unit, start, end);
- if (val >= 1 && isInt(val)) {
- break;
- }
- }
- return unit; // will be "milliseconds" if nothing else matches
- }
- // Computes the number of units (like "hours") in the given range.
- // Range can be a {start,end} object, separate start/end args, or a Duration.
- // Results are based on Moment's .as() and .diff() methods, so results can depend on internal handling
- // of month-diffing logic (which tends to vary from version to version).
- function computeRangeAs(unit, start, end) {
- if (end != null) { // given start, end
- return end.diff(start, unit, true);
- }
- else if (moment.isDuration(start)) { // given duration
- return start.as(unit);
- }
- else { // given { start, end } range object
- return start.end.diff(start.start, unit, true);
- }
- }
- function isNativeDate(input) {
- return Object.prototype.toString.call(input) === '[object Date]' || input instanceof Date;
- }
- // Returns a boolean about whether the given input is a time string, like "06:40:00" or "06:00"
- function isTimeString(str) {
- return /^\d+\:\d+(?:\:\d+\.?(?:\d{3})?)?$/.test(str);
- }
- /* General Utilities
- ----------------------------------------------------------------------------------------------------------------------*/
- var hasOwnPropMethod = {}.hasOwnProperty;
- // Create an object that has the given prototype. Just like Object.create
- function createObject(proto) {
- var f = function() {};
- f.prototype = proto;
- return new f();
- }
- function copyOwnProps(src, dest) {
- for (var name in src) {
- if (hasOwnProp(src, name)) {
- dest[name] = src[name];
- }
- }
- }
- // Copies over certain methods with the same names as Object.prototype methods. Overcomes an IE<=8 bug:
- // https://developer.mozilla.org/en-US/docs/ECMAScript_DontEnum_attribute#JScript_DontEnum_Bug
- function copyNativeMethods(src, dest) {
- var names = [ 'constructor', 'toString', 'valueOf' ];
- var i, name;
- for (i = 0; i < names.length; i++) {
- name = names[i];
- if (src[name] !== Object.prototype[name]) {
- dest[name] = src[name];
- }
- }
- }
- function hasOwnProp(obj, name) {
- return hasOwnPropMethod.call(obj, name);
- }
- // Is the given value a non-object non-function value?
- function isAtomic(val) {
- return /undefined|null|boolean|number|string/.test($.type(val));
- }
- function applyAll(functions, thisObj, args) {
- if ($.isFunction(functions)) {
- functions = [ functions ];
- }
- if (functions) {
- var i;
- var ret;
- for (i=0; i<functions.length; i++) {
- ret = functions[i].apply(thisObj, args) || ret;
- }
- return ret;
- }
- }
- function firstDefined() {
- for (var i=0; i<arguments.length; i++) {
- if (arguments[i] !== undefined) {
- return arguments[i];
- }
- }
- }
- function htmlEscape(s) {
- return (s + '').replace(/&/g, '&')
- .replace(/</g, '<')
- .replace(/>/g, '>')
- .replace(/'/g, ''')
- .replace(/"/g, '"')
- .replace(/\n/g, '<br />');
- }
- function stripHtmlEntities(text) {
- return text.replace(/&.*?;/g, '');
- }
- function capitaliseFirstLetter(str) {
- return str.charAt(0).toUpperCase() + str.slice(1);
- }
- function compareNumbers(a, b) { // for .sort()
- return a - b;
- }
- function isInt(n) {
- return n % 1 === 0;
- }
- // Returns a function, that, as long as it continues to be invoked, will not
- // be triggered. The function will be called after it stops being called for
- // N milliseconds.
- // https://github.com/jashkenas/underscore/blob/1.6.0/underscore.js#L714
- function debounce(func, wait) {
- var timeoutId;
- var args;
- var context;
- var timestamp; // of most recent call
- var later = function() {
- var last = +new Date() - timestamp;
- if (last < wait && last > 0) {
- timeoutId = setTimeout(later, wait - last);
- }
- else {
- timeoutId = null;
- func.apply(context, args);
- if (!timeoutId) {
- context = args = null;
- }
- }
- };
- return function() {
- context = this;
- args = arguments;
- timestamp = +new Date();
- if (!timeoutId) {
- timeoutId = setTimeout(later, wait);
- }
- };
- }
|