util.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  1. // exports
  2. FC.intersectRanges = intersectRanges;
  3. FC.applyAll = applyAll;
  4. FC.debounce = debounce;
  5. FC.isInt = isInt;
  6. FC.htmlEscape = htmlEscape;
  7. FC.cssToStr = cssToStr;
  8. FC.proxy = proxy;
  9. FC.capitaliseFirstLetter = capitaliseFirstLetter;
  10. /* FullCalendar-specific DOM Utilities
  11. ----------------------------------------------------------------------------------------------------------------------*/
  12. // Given the scrollbar widths of some other container, create borders/margins on rowEls in order to match the left
  13. // and right space that was offset by the scrollbars. A 1-pixel border first, then margin beyond that.
  14. function compensateScroll(rowEls, scrollbarWidths) {
  15. if (scrollbarWidths.left) {
  16. rowEls.css({
  17. 'border-left-width': 1,
  18. 'margin-left': scrollbarWidths.left - 1
  19. });
  20. }
  21. if (scrollbarWidths.right) {
  22. rowEls.css({
  23. 'border-right-width': 1,
  24. 'margin-right': scrollbarWidths.right - 1
  25. });
  26. }
  27. }
  28. // Undoes compensateScroll and restores all borders/margins
  29. function uncompensateScroll(rowEls) {
  30. rowEls.css({
  31. 'margin-left': '',
  32. 'margin-right': '',
  33. 'border-left-width': '',
  34. 'border-right-width': ''
  35. });
  36. }
  37. // Make the mouse cursor express that an event is not allowed in the current area
  38. function disableCursor() {
  39. $('body').addClass('fc-not-allowed');
  40. }
  41. // Returns the mouse cursor to its original look
  42. function enableCursor() {
  43. $('body').removeClass('fc-not-allowed');
  44. }
  45. // Given a total available height to fill, have `els` (essentially child rows) expand to accomodate.
  46. // By default, all elements that are shorter than the recommended height are expanded uniformly, not considering
  47. // any other els that are already too tall. if `shouldRedistribute` is on, it considers these tall rows and
  48. // reduces the available height.
  49. function distributeHeight(els, availableHeight, shouldRedistribute) {
  50. // *FLOORING NOTE*: we floor in certain places because zoom can give inaccurate floating-point dimensions,
  51. // and it is better to be shorter than taller, to avoid creating unnecessary scrollbars.
  52. var minOffset1 = Math.floor(availableHeight / els.length); // for non-last element
  53. var minOffset2 = Math.floor(availableHeight - minOffset1 * (els.length - 1)); // for last element *FLOORING NOTE*
  54. var flexEls = []; // elements that are allowed to expand. array of DOM nodes
  55. var flexOffsets = []; // amount of vertical space it takes up
  56. var flexHeights = []; // actual css height
  57. var usedHeight = 0;
  58. undistributeHeight(els); // give all elements their natural height
  59. // find elements that are below the recommended height (expandable).
  60. // important to query for heights in a single first pass (to avoid reflow oscillation).
  61. els.each(function(i, el) {
  62. var minOffset = i === els.length - 1 ? minOffset2 : minOffset1;
  63. var naturalOffset = $(el).outerHeight(true);
  64. if (naturalOffset < minOffset) {
  65. flexEls.push(el);
  66. flexOffsets.push(naturalOffset);
  67. flexHeights.push($(el).height());
  68. }
  69. else {
  70. // this element stretches past recommended height (non-expandable). mark the space as occupied.
  71. usedHeight += naturalOffset;
  72. }
  73. });
  74. // readjust the recommended height to only consider the height available to non-maxed-out rows.
  75. if (shouldRedistribute) {
  76. availableHeight -= usedHeight;
  77. minOffset1 = Math.floor(availableHeight / flexEls.length);
  78. minOffset2 = Math.floor(availableHeight - minOffset1 * (flexEls.length - 1)); // *FLOORING NOTE*
  79. }
  80. // assign heights to all expandable elements
  81. $(flexEls).each(function(i, el) {
  82. var minOffset = i === flexEls.length - 1 ? minOffset2 : minOffset1;
  83. var naturalOffset = flexOffsets[i];
  84. var naturalHeight = flexHeights[i];
  85. var newHeight = minOffset - (naturalOffset - naturalHeight); // subtract the margin/padding
  86. if (naturalOffset < minOffset) { // we check this again because redistribution might have changed things
  87. $(el).height(newHeight);
  88. }
  89. });
  90. }
  91. // Undoes distrubuteHeight, restoring all els to their natural height
  92. function undistributeHeight(els) {
  93. els.height('');
  94. }
  95. // Given `els`, a jQuery set of <td> cells, find the cell with the largest natural width and set the widths of all the
  96. // cells to be that width.
  97. // PREREQUISITE: if you want a cell to take up width, it needs to have a single inner element w/ display:inline
  98. function matchCellWidths(els) {
  99. var maxInnerWidth = 0;
  100. els.find('> span').each(function(i, innerEl) {
  101. var innerWidth = $(innerEl).outerWidth();
  102. if (innerWidth > maxInnerWidth) {
  103. maxInnerWidth = innerWidth;
  104. }
  105. });
  106. maxInnerWidth++; // sometimes not accurate of width the text needs to stay on one line. insurance
  107. els.width(maxInnerWidth);
  108. return maxInnerWidth;
  109. }
  110. // Turns a container element into a scroller if its contents is taller than the allotted height.
  111. // Returns true if the element is now a scroller, false otherwise.
  112. // NOTE: this method is best because it takes weird zooming dimensions into account
  113. function setPotentialScroller(containerEl, height) {
  114. containerEl.height(height).addClass('fc-scroller');
  115. // are scrollbars needed?
  116. if (containerEl[0].scrollHeight - 1 > containerEl[0].clientHeight) { // !!! -1 because IE is often off-by-one :(
  117. return true;
  118. }
  119. unsetScroller(containerEl); // undo
  120. return false;
  121. }
  122. // Takes an element that might have been a scroller, and turns it back into a normal element.
  123. function unsetScroller(containerEl) {
  124. containerEl.height('').removeClass('fc-scroller');
  125. }
  126. /* General DOM Utilities
  127. ----------------------------------------------------------------------------------------------------------------------*/
  128. FC.getOuterRect = getOuterRect;
  129. FC.getClientRect = getClientRect;
  130. FC.getContentRect = getContentRect;
  131. FC.getScrollbarWidths = getScrollbarWidths;
  132. // borrowed from https://github.com/jquery/jquery-ui/blob/1.11.0/ui/core.js#L51
  133. function getScrollParent(el) {
  134. var position = el.css('position'),
  135. scrollParent = el.parents().filter(function() {
  136. var parent = $(this);
  137. return (/(auto|scroll)/).test(
  138. parent.css('overflow') + parent.css('overflow-y') + parent.css('overflow-x')
  139. );
  140. }).eq(0);
  141. return position === 'fixed' || !scrollParent.length ? $(el[0].ownerDocument || document) : scrollParent;
  142. }
  143. // Queries the outer bounding area of a jQuery element.
  144. // Returns a rectangle with absolute coordinates: left, right (exclusive), top, bottom (exclusive).
  145. function getOuterRect(el) {
  146. var offset = el.offset();
  147. return {
  148. left: offset.left,
  149. right: offset.left + el.outerWidth(),
  150. top: offset.top,
  151. bottom: offset.top + el.outerHeight()
  152. };
  153. }
  154. // Queries the area within the margin/border/scrollbars of a jQuery element. Does not go within the padding.
  155. // Returns a rectangle with absolute coordinates: left, right (exclusive), top, bottom (exclusive).
  156. // NOTE: should use clientLeft/clientTop, but very unreliable cross-browser.
  157. function getClientRect(el) {
  158. var offset = el.offset();
  159. var scrollbarWidths = getScrollbarWidths(el);
  160. var left = offset.left + getCssFloat(el, 'border-left-width') + scrollbarWidths.left;
  161. var top = offset.top + getCssFloat(el, 'border-top-width') + scrollbarWidths.top;
  162. return {
  163. left: left,
  164. right: left + el[0].clientWidth, // clientWidth includes padding but NOT scrollbars
  165. top: top,
  166. bottom: top + el[0].clientHeight // clientHeight includes padding but NOT scrollbars
  167. };
  168. }
  169. // Queries the area within the margin/border/padding of a jQuery element. Assumed not to have scrollbars.
  170. // Returns a rectangle with absolute coordinates: left, right (exclusive), top, bottom (exclusive).
  171. function getContentRect(el) {
  172. var offset = el.offset(); // just outside of border, margin not included
  173. var left = offset.left + getCssFloat(el, 'border-left-width') + getCssFloat(el, 'padding-left');
  174. var top = offset.top + getCssFloat(el, 'border-top-width') + getCssFloat(el, 'padding-top');
  175. return {
  176. left: left,
  177. right: left + el.width(),
  178. top: top,
  179. bottom: top + el.height()
  180. };
  181. }
  182. // Returns the computed left/right/top/bottom scrollbar widths for the given jQuery element.
  183. // NOTE: should use clientLeft/clientTop, but very unreliable cross-browser.
  184. function getScrollbarWidths(el) {
  185. var leftRightWidth = el.innerWidth() - el[0].clientWidth; // the paddings cancel out, leaving the scrollbars
  186. var widths = {
  187. left: 0,
  188. right: 0,
  189. top: 0,
  190. bottom: el.innerHeight() - el[0].clientHeight // the paddings cancel out, leaving the bottom scrollbar
  191. };
  192. if (getIsLeftRtlScrollbars() && el.css('direction') == 'rtl') { // is the scrollbar on the left side?
  193. widths.left = leftRightWidth;
  194. }
  195. else {
  196. widths.right = leftRightWidth;
  197. }
  198. return widths;
  199. }
  200. // Logic for determining if, when the element is right-to-left, the scrollbar appears on the left side
  201. var _isLeftRtlScrollbars = null;
  202. function getIsLeftRtlScrollbars() { // responsible for caching the computation
  203. if (_isLeftRtlScrollbars === null) {
  204. _isLeftRtlScrollbars = computeIsLeftRtlScrollbars();
  205. }
  206. return _isLeftRtlScrollbars;
  207. }
  208. function computeIsLeftRtlScrollbars() { // creates an offscreen test element, then removes it
  209. var el = $('<div><div/></div>')
  210. .css({
  211. position: 'absolute',
  212. top: -1000,
  213. left: 0,
  214. border: 0,
  215. padding: 0,
  216. overflow: 'scroll',
  217. direction: 'rtl'
  218. })
  219. .appendTo('body');
  220. var innerEl = el.children();
  221. var res = innerEl.offset().left > el.offset().left; // is the inner div shifted to accommodate a left scrollbar?
  222. el.remove();
  223. return res;
  224. }
  225. // Retrieves a jQuery element's computed CSS value as a floating-point number.
  226. // If the queried value is non-numeric (ex: IE can return "medium" for border width), will just return zero.
  227. function getCssFloat(el, prop) {
  228. return parseFloat(el.css(prop)) || 0;
  229. }
  230. // Returns a boolean whether this was a left mouse click and no ctrl key (which means right click on Mac)
  231. function isPrimaryMouseButton(ev) {
  232. return ev.which == 1 && !ev.ctrlKey;
  233. }
  234. /* Geometry
  235. ----------------------------------------------------------------------------------------------------------------------*/
  236. FC.intersectRects = intersectRects;
  237. // Returns a new rectangle that is the intersection of the two rectangles. If they don't intersect, returns false
  238. function intersectRects(rect1, rect2) {
  239. var res = {
  240. left: Math.max(rect1.left, rect2.left),
  241. right: Math.min(rect1.right, rect2.right),
  242. top: Math.max(rect1.top, rect2.top),
  243. bottom: Math.min(rect1.bottom, rect2.bottom)
  244. };
  245. if (res.left < res.right && res.top < res.bottom) {
  246. return res;
  247. }
  248. return false;
  249. }
  250. // Returns a new point that will have been moved to reside within the given rectangle
  251. function constrainPoint(point, rect) {
  252. return {
  253. left: Math.min(Math.max(point.left, rect.left), rect.right),
  254. top: Math.min(Math.max(point.top, rect.top), rect.bottom)
  255. };
  256. }
  257. // Returns a point that is the center of the given rectangle
  258. function getRectCenter(rect) {
  259. return {
  260. left: (rect.left + rect.right) / 2,
  261. top: (rect.top + rect.bottom) / 2
  262. };
  263. }
  264. // Subtracts point2's coordinates from point1's coordinates, returning a delta
  265. function diffPoints(point1, point2) {
  266. return {
  267. left: point1.left - point2.left,
  268. top: point1.top - point2.top
  269. };
  270. }
  271. /* Object Ordering by Field
  272. ----------------------------------------------------------------------------------------------------------------------*/
  273. FC.parseFieldSpecs = parseFieldSpecs;
  274. FC.compareByFieldSpecs = compareByFieldSpecs;
  275. FC.compareByFieldSpec = compareByFieldSpec;
  276. FC.flexibleCompare = flexibleCompare;
  277. function parseFieldSpecs(input) {
  278. var specs = [];
  279. var tokens = [];
  280. var i, token;
  281. if (typeof input === 'string') {
  282. tokens = input.split(/\s*,\s*/);
  283. }
  284. else if (typeof input === 'function') {
  285. tokens = [ input ];
  286. }
  287. else if ($.isArray(input)) {
  288. tokens = input;
  289. }
  290. for (i = 0; i < tokens.length; i++) {
  291. token = tokens[i];
  292. if (typeof token === 'string') {
  293. specs.push(
  294. token.charAt(0) == '-' ?
  295. { field: token.substring(1), order: -1 } :
  296. { field: token, order: 1 }
  297. );
  298. }
  299. else if (typeof token === 'function') {
  300. specs.push({ func: token });
  301. }
  302. }
  303. return specs;
  304. }
  305. function compareByFieldSpecs(obj1, obj2, fieldSpecs) {
  306. var i;
  307. var cmp;
  308. for (i = 0; i < fieldSpecs.length; i++) {
  309. cmp = compareByFieldSpec(obj1, obj2, fieldSpecs[i]);
  310. if (cmp) {
  311. return cmp;
  312. }
  313. }
  314. return 0;
  315. }
  316. function compareByFieldSpec(obj1, obj2, fieldSpec) {
  317. if (fieldSpec.func) {
  318. return fieldSpec.func(obj1, obj2);
  319. }
  320. return flexibleCompare(obj1[fieldSpec.field], obj2[fieldSpec.field]) *
  321. (fieldSpec.order || 1);
  322. }
  323. function flexibleCompare(a, b) {
  324. if (!a && !b) {
  325. return 0;
  326. }
  327. if (b == null) {
  328. return -1;
  329. }
  330. if (a == null) {
  331. return 1;
  332. }
  333. if ($.type(a) === 'string' || $.type(b) === 'string') {
  334. return String(a).localeCompare(String(b));
  335. }
  336. return a - b;
  337. }
  338. /* FullCalendar-specific Misc Utilities
  339. ----------------------------------------------------------------------------------------------------------------------*/
  340. // Computes the intersection of the two ranges. Returns undefined if no intersection.
  341. // Expects all dates to be normalized to the same timezone beforehand.
  342. // TODO: move to date section?
  343. function intersectRanges(subjectRange, constraintRange) {
  344. var subjectStart = subjectRange.start;
  345. var subjectEnd = subjectRange.end;
  346. var constraintStart = constraintRange.start;
  347. var constraintEnd = constraintRange.end;
  348. var segStart, segEnd;
  349. var isStart, isEnd;
  350. if (subjectEnd > constraintStart && subjectStart < constraintEnd) { // in bounds at all?
  351. if (subjectStart >= constraintStart) {
  352. segStart = subjectStart.clone();
  353. isStart = true;
  354. }
  355. else {
  356. segStart = constraintStart.clone();
  357. isStart = false;
  358. }
  359. if (subjectEnd <= constraintEnd) {
  360. segEnd = subjectEnd.clone();
  361. isEnd = true;
  362. }
  363. else {
  364. segEnd = constraintEnd.clone();
  365. isEnd = false;
  366. }
  367. return {
  368. start: segStart,
  369. end: segEnd,
  370. isStart: isStart,
  371. isEnd: isEnd
  372. };
  373. }
  374. }
  375. /* Date Utilities
  376. ----------------------------------------------------------------------------------------------------------------------*/
  377. FC.computeIntervalUnit = computeIntervalUnit;
  378. FC.divideRangeByDuration = divideRangeByDuration;
  379. FC.divideDurationByDuration = divideDurationByDuration;
  380. FC.multiplyDuration = multiplyDuration;
  381. FC.durationHasTime = durationHasTime;
  382. var dayIDs = [ 'sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat' ];
  383. var intervalUnits = [ 'year', 'month', 'week', 'day', 'hour', 'minute', 'second', 'millisecond' ];
  384. // Diffs the two moments into a Duration where full-days are recorded first, then the remaining time.
  385. // Moments will have their timezones normalized.
  386. function diffDayTime(a, b) {
  387. return moment.duration({
  388. days: a.clone().stripTime().diff(b.clone().stripTime(), 'days'),
  389. ms: a.time() - b.time() // time-of-day from day start. disregards timezone
  390. });
  391. }
  392. // Diffs the two moments via their start-of-day (regardless of timezone). Produces whole-day durations.
  393. function diffDay(a, b) {
  394. return moment.duration({
  395. days: a.clone().stripTime().diff(b.clone().stripTime(), 'days')
  396. });
  397. }
  398. // Diffs two moments, producing a duration, made of a whole-unit-increment of the given unit. Uses rounding.
  399. function diffByUnit(a, b, unit) {
  400. return moment.duration(
  401. Math.round(a.diff(b, unit, true)), // returnFloat=true
  402. unit
  403. );
  404. }
  405. // Computes the unit name of the largest whole-unit period of time.
  406. // For example, 48 hours will be "days" whereas 49 hours will be "hours".
  407. // Accepts start/end, a range object, or an original duration object.
  408. function computeIntervalUnit(start, end) {
  409. var i, unit;
  410. var val;
  411. for (i = 0; i < intervalUnits.length; i++) {
  412. unit = intervalUnits[i];
  413. val = computeRangeAs(unit, start, end);
  414. if (val >= 1 && isInt(val)) {
  415. break;
  416. }
  417. }
  418. return unit; // will be "milliseconds" if nothing else matches
  419. }
  420. // Computes the number of units (like "hours") in the given range.
  421. // Range can be a {start,end} object, separate start/end args, or a Duration.
  422. // Results are based on Moment's .as() and .diff() methods, so results can depend on internal handling
  423. // of month-diffing logic (which tends to vary from version to version).
  424. function computeRangeAs(unit, start, end) {
  425. if (end != null) { // given start, end
  426. return end.diff(start, unit, true);
  427. }
  428. else if (moment.isDuration(start)) { // given duration
  429. return start.as(unit);
  430. }
  431. else { // given { start, end } range object
  432. return start.end.diff(start.start, unit, true);
  433. }
  434. }
  435. // Intelligently divides a range (specified by a start/end params) by a duration
  436. function divideRangeByDuration(start, end, dur) {
  437. var months;
  438. if (durationHasTime(dur)) {
  439. return (end - start) / dur;
  440. }
  441. months = dur.asMonths();
  442. if (Math.abs(months) >= 1 && isInt(months)) {
  443. return end.diff(start, 'months', true) / months;
  444. }
  445. return end.diff(start, 'days', true) / dur.asDays();
  446. }
  447. // Intelligently divides one duration by another
  448. function divideDurationByDuration(dur1, dur2) {
  449. var months1, months2;
  450. if (durationHasTime(dur1) || durationHasTime(dur2)) {
  451. return dur1 / dur2;
  452. }
  453. months1 = dur1.asMonths();
  454. months2 = dur2.asMonths();
  455. if (
  456. Math.abs(months1) >= 1 && isInt(months1) &&
  457. Math.abs(months2) >= 1 && isInt(months2)
  458. ) {
  459. return months1 / months2;
  460. }
  461. return dur1.asDays() / dur2.asDays();
  462. }
  463. // Intelligently multiplies a duration by a number
  464. function multiplyDuration(dur, n) {
  465. var months;
  466. if (durationHasTime(dur)) {
  467. return moment.duration(dur * n);
  468. }
  469. months = dur.asMonths();
  470. if (Math.abs(months) >= 1 && isInt(months)) {
  471. return moment.duration({ months: months * n });
  472. }
  473. return moment.duration({ days: dur.asDays() * n });
  474. }
  475. // Returns a boolean about whether the given duration has any time parts (hours/minutes/seconds/ms)
  476. function durationHasTime(dur) {
  477. return Boolean(dur.hours() || dur.minutes() || dur.seconds() || dur.milliseconds());
  478. }
  479. function isNativeDate(input) {
  480. return Object.prototype.toString.call(input) === '[object Date]' || input instanceof Date;
  481. }
  482. // Returns a boolean about whether the given input is a time string, like "06:40:00" or "06:00"
  483. function isTimeString(str) {
  484. return /^\d+\:\d+(?:\:\d+\.?(?:\d{3})?)?$/.test(str);
  485. }
  486. /* Logging and Debug
  487. ----------------------------------------------------------------------------------------------------------------------*/
  488. FC.log = function() {
  489. var console = window.console;
  490. if (console && console.log) {
  491. return console.log.apply(console, arguments);
  492. }
  493. };
  494. FC.warn = function() {
  495. var console = window.console;
  496. if (console && console.warn) {
  497. return console.warn.apply(console, arguments);
  498. }
  499. else {
  500. return FC.log.apply(FC, arguments);
  501. }
  502. };
  503. /* General Utilities
  504. ----------------------------------------------------------------------------------------------------------------------*/
  505. var hasOwnPropMethod = {}.hasOwnProperty;
  506. // Merges an array of objects into a single object.
  507. // The second argument allows for an array of property names who's object values will be merged together.
  508. function mergeProps(propObjs, complexProps) {
  509. var dest = {};
  510. var i, name;
  511. var complexObjs;
  512. var j, val;
  513. var props;
  514. if (complexProps) {
  515. for (i = 0; i < complexProps.length; i++) {
  516. name = complexProps[i];
  517. complexObjs = [];
  518. // collect the trailing object values, stopping when a non-object is discovered
  519. for (j = propObjs.length - 1; j >= 0; j--) {
  520. val = propObjs[j][name];
  521. if (typeof val === 'object') {
  522. complexObjs.unshift(val);
  523. }
  524. else if (val !== undefined) {
  525. dest[name] = val; // if there were no objects, this value will be used
  526. break;
  527. }
  528. }
  529. // if the trailing values were objects, use the merged value
  530. if (complexObjs.length) {
  531. dest[name] = mergeProps(complexObjs);
  532. }
  533. }
  534. }
  535. // copy values into the destination, going from last to first
  536. for (i = propObjs.length - 1; i >= 0; i--) {
  537. props = propObjs[i];
  538. for (name in props) {
  539. if (!(name in dest)) { // if already assigned by previous props or complex props, don't reassign
  540. dest[name] = props[name];
  541. }
  542. }
  543. }
  544. return dest;
  545. }
  546. // Create an object that has the given prototype. Just like Object.create
  547. function createObject(proto) {
  548. var f = function() {};
  549. f.prototype = proto;
  550. return new f();
  551. }
  552. function copyOwnProps(src, dest) {
  553. for (var name in src) {
  554. if (hasOwnProp(src, name)) {
  555. dest[name] = src[name];
  556. }
  557. }
  558. }
  559. // Copies over certain methods with the same names as Object.prototype methods. Overcomes an IE<=8 bug:
  560. // https://developer.mozilla.org/en-US/docs/ECMAScript_DontEnum_attribute#JScript_DontEnum_Bug
  561. function copyNativeMethods(src, dest) {
  562. var names = [ 'constructor', 'toString', 'valueOf' ];
  563. var i, name;
  564. for (i = 0; i < names.length; i++) {
  565. name = names[i];
  566. if (src[name] !== Object.prototype[name]) {
  567. dest[name] = src[name];
  568. }
  569. }
  570. }
  571. function hasOwnProp(obj, name) {
  572. return hasOwnPropMethod.call(obj, name);
  573. }
  574. // Is the given value a non-object non-function value?
  575. function isAtomic(val) {
  576. return /undefined|null|boolean|number|string/.test($.type(val));
  577. }
  578. function applyAll(functions, thisObj, args) {
  579. if ($.isFunction(functions)) {
  580. functions = [ functions ];
  581. }
  582. if (functions) {
  583. var i;
  584. var ret;
  585. for (i=0; i<functions.length; i++) {
  586. ret = functions[i].apply(thisObj, args) || ret;
  587. }
  588. return ret;
  589. }
  590. }
  591. function firstDefined() {
  592. for (var i=0; i<arguments.length; i++) {
  593. if (arguments[i] !== undefined) {
  594. return arguments[i];
  595. }
  596. }
  597. }
  598. function htmlEscape(s) {
  599. return (s + '').replace(/&/g, '&amp;')
  600. .replace(/</g, '&lt;')
  601. .replace(/>/g, '&gt;')
  602. .replace(/'/g, '&#039;')
  603. .replace(/"/g, '&quot;')
  604. .replace(/\n/g, '<br />');
  605. }
  606. function stripHtmlEntities(text) {
  607. return text.replace(/&.*?;/g, '');
  608. }
  609. // Given a hash of CSS properties, returns a string of CSS.
  610. // Uses property names as-is (no camel-case conversion). Will not make statements for null/undefined values.
  611. function cssToStr(cssProps) {
  612. var statements = [];
  613. $.each(cssProps, function(name, val) {
  614. if (val != null) {
  615. statements.push(name + ':' + val);
  616. }
  617. });
  618. return statements.join(';');
  619. }
  620. function capitaliseFirstLetter(str) {
  621. return str.charAt(0).toUpperCase() + str.slice(1);
  622. }
  623. function compareNumbers(a, b) { // for .sort()
  624. return a - b;
  625. }
  626. function isInt(n) {
  627. return n % 1 === 0;
  628. }
  629. // Returns a method bound to the given object context.
  630. // Just like one of the jQuery.proxy signatures, but without the undesired behavior of treating the same method with
  631. // different contexts as identical when binding/unbinding events.
  632. function proxy(obj, methodName) {
  633. var method = obj[methodName];
  634. return function() {
  635. return method.apply(obj, arguments);
  636. };
  637. }
  638. // Returns a function, that, as long as it continues to be invoked, will not
  639. // be triggered. The function will be called after it stops being called for
  640. // N milliseconds.
  641. // https://github.com/jashkenas/underscore/blob/1.6.0/underscore.js#L714
  642. function debounce(func, wait) {
  643. var timeoutId;
  644. var args;
  645. var context;
  646. var timestamp; // of most recent call
  647. var later = function() {
  648. var last = +new Date() - timestamp;
  649. if (last < wait && last > 0) {
  650. timeoutId = setTimeout(later, wait - last);
  651. }
  652. else {
  653. timeoutId = null;
  654. func.apply(context, args);
  655. if (!timeoutId) {
  656. context = args = null;
  657. }
  658. }
  659. };
  660. return function() {
  661. context = this;
  662. args = arguments;
  663. timestamp = +new Date();
  664. if (!timeoutId) {
  665. timeoutId = setTimeout(later, wait);
  666. }
  667. };
  668. }