| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364 |
- /* 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': ''
- });
- }
- // 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.
- function intersectionToSeg(subjectStart, subjectEnd, intervalStart, intervalEnd) {
- var segStart, segEnd;
- var isStart, isEnd;
- if (subjectEnd > intervalStart && subjectStart < intervalEnd) { // in bounds at all?
- if (subjectStart >= intervalStart) {
- segStart = subjectStart.clone();
- isStart = true;
- }
- else {
- segStart = intervalStart.clone();
- isStart = false;
- }
- if (subjectEnd <= intervalEnd) {
- segEnd = subjectEnd.clone();
- isEnd = true;
- }
- else {
- segEnd = intervalEnd.clone();
- isEnd = false;
- }
- return {
- start: segStart,
- end: segEnd,
- isStart: isStart,
- isEnd: isEnd
- };
- }
- }
- function smartProperty(obj, name) { // get a camel-cased/namespaced property of an object
- obj = obj || {};
- if (obj[name] !== undefined) {
- return obj[name];
- }
- var parts = name.split(/(?=[A-Z])/),
- i = parts.length - 1, res;
- for (; i>=0; i--) {
- res = obj[parts[i].toLowerCase()];
- if (res !== undefined) {
- return res;
- }
- }
- return obj['default'];
- }
- /* Date Utilities
- ----------------------------------------------------------------------------------------------------------------------*/
- var dayIDs = [ 'sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat' ];
- // Diffs the two moments into a Duration where full-days are recorded first, then the remaining time.
- // Moments will have their timezones normalized.
- function dayishDiff(a, b) {
- return moment.duration({
- days: a.clone().stripTime().diff(b.clone().stripTime(), 'days'),
- ms: a.time() - b.time()
- });
- }
- function isNativeDate(input) {
- return Object.prototype.toString.call(input) === '[object Date]' || input instanceof Date;
- }
- function dateCompare(a, b) { // works with Moments and native Dates
- return a - b;
- }
- /* General Utilities
- ----------------------------------------------------------------------------------------------------------------------*/
- fc.applyAll = applyAll; // export
- // Create an object that has the given prototype. Just like Object.create
- function createObject(proto) {
- var f = function() {};
- f.prototype = proto;
- return new f();
- }
- // Copies specifically-owned (non-protoype) properties of `b` onto `a`.
- // FYI, $.extend would copy *all* properties of `b` onto `a`.
- function extend(a, b) {
- for (var i in b) {
- if (b.hasOwnProperty(i)) {
- a[i] = b[i];
- }
- }
- }
- 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);
- }
- // 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);
- }
- };
- }
|