util.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. fc.applyAll = applyAll;
  2. // Create an object that has the given prototype.
  3. // Just like Object.create
  4. function createObject(proto) {
  5. var f = function() {};
  6. f.prototype = proto;
  7. return new f();
  8. }
  9. // Copies specifically-owned (non-protoype) properties of `b` onto `a`.
  10. // FYI, $.extend would copy *all* properties of `b` onto `a`.
  11. function extend(a, b) {
  12. for (var i in b) {
  13. if (b.hasOwnProperty(i)) {
  14. a[i] = b[i];
  15. }
  16. }
  17. }
  18. /* Date
  19. -----------------------------------------------------------------------------*/
  20. var dayIDs = ['sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat'];
  21. // diffs the two moments into a Duration where full-days are recorded first,
  22. // then the remaining time.
  23. function dayishDiff(d1, d0) {
  24. return moment.duration({
  25. days: d1.clone().stripTime().diff(d0.clone().stripTime(), 'days'),
  26. ms: d1.time() - d0.time()
  27. });
  28. }
  29. function isNativeDate(input) {
  30. return Object.prototype.toString.call(input) === '[object Date]' ||
  31. input instanceof Date;
  32. }
  33. /* Event Element Binding
  34. -----------------------------------------------------------------------------*/
  35. function lazySegBind(container, segs, bindHandlers) {
  36. container.unbind('mouseover').mouseover(function(ev) {
  37. var parent=ev.target, e,
  38. i, seg;
  39. while (parent != this) {
  40. e = parent;
  41. parent = parent.parentNode;
  42. }
  43. if ((i = e._fci) !== undefined) {
  44. e._fci = undefined;
  45. seg = segs[i];
  46. bindHandlers(seg.event, seg.element, seg);
  47. $(ev.target).trigger(ev);
  48. }
  49. ev.stopPropagation();
  50. });
  51. }
  52. /* Element Dimensions
  53. -----------------------------------------------------------------------------*/
  54. function setOuterWidth(element, width, includeMargins) {
  55. for (var i=0, e; i<element.length; i++) {
  56. e = $(element[i]);
  57. e.width(Math.max(0, width - hsides(e, includeMargins)));
  58. }
  59. }
  60. function setOuterHeight(element, height, includeMargins) {
  61. for (var i=0, e; i<element.length; i++) {
  62. e = $(element[i]);
  63. e.height(Math.max(0, height - vsides(e, includeMargins)));
  64. }
  65. }
  66. function hsides(element, includeMargins) {
  67. return hpadding(element) + hborders(element) + (includeMargins ? hmargins(element) : 0);
  68. }
  69. function hpadding(element) {
  70. return (parseFloat($.css(element[0], 'paddingLeft', true)) || 0) +
  71. (parseFloat($.css(element[0], 'paddingRight', true)) || 0);
  72. }
  73. function hmargins(element) {
  74. return (parseFloat($.css(element[0], 'marginLeft', true)) || 0) +
  75. (parseFloat($.css(element[0], 'marginRight', true)) || 0);
  76. }
  77. function hborders(element) {
  78. return (parseFloat($.css(element[0], 'borderLeftWidth', true)) || 0) +
  79. (parseFloat($.css(element[0], 'borderRightWidth', true)) || 0);
  80. }
  81. function vsides(element, includeMargins) {
  82. return vpadding(element) + vborders(element) + (includeMargins ? vmargins(element) : 0);
  83. }
  84. function vpadding(element) {
  85. return (parseFloat($.css(element[0], 'paddingTop', true)) || 0) +
  86. (parseFloat($.css(element[0], 'paddingBottom', true)) || 0);
  87. }
  88. function vmargins(element) {
  89. return (parseFloat($.css(element[0], 'marginTop', true)) || 0) +
  90. (parseFloat($.css(element[0], 'marginBottom', true)) || 0);
  91. }
  92. function vborders(element) {
  93. return (parseFloat($.css(element[0], 'borderTopWidth', true)) || 0) +
  94. (parseFloat($.css(element[0], 'borderBottomWidth', true)) || 0);
  95. }
  96. /* Misc Utils
  97. -----------------------------------------------------------------------------*/
  98. //TODO: arraySlice
  99. //TODO: isFunction, grep ?
  100. function noop() { }
  101. function dateCompare(a, b) { // works with moments too
  102. return a - b;
  103. }
  104. function arrayMax(a) {
  105. return Math.max.apply(Math, a);
  106. }
  107. function smartProperty(obj, name) { // get a camel-cased/namespaced property of an object
  108. obj = obj || {};
  109. if (obj[name] !== undefined) {
  110. return obj[name];
  111. }
  112. var parts = name.split(/(?=[A-Z])/),
  113. i=parts.length-1, res;
  114. for (; i>=0; i--) {
  115. res = obj[parts[i].toLowerCase()];
  116. if (res !== undefined) {
  117. return res;
  118. }
  119. }
  120. return obj['default'];
  121. }
  122. function htmlEscape(s) {
  123. return (s + '').replace(/&/g, '&amp;')
  124. .replace(/</g, '&lt;')
  125. .replace(/>/g, '&gt;')
  126. .replace(/'/g, '&#039;')
  127. .replace(/"/g, '&quot;')
  128. .replace(/\n/g, '<br />');
  129. }
  130. function stripHTMLEntities(text) {
  131. return text.replace(/&.*?;/g, '');
  132. }
  133. function disableTextSelection(element) {
  134. element
  135. .attr('unselectable', 'on')
  136. .css('MozUserSelect', 'none')
  137. .bind('selectstart.ui', function() { return false; });
  138. }
  139. /*
  140. function enableTextSelection(element) {
  141. element
  142. .attr('unselectable', 'off')
  143. .css('MozUserSelect', '')
  144. .unbind('selectstart.ui');
  145. }
  146. */
  147. function markFirstLast(e) { // TODO: use CSS selectors instead
  148. e.children()
  149. .removeClass('fc-first fc-last')
  150. .filter(':first-child')
  151. .addClass('fc-first')
  152. .end()
  153. .filter(':last-child')
  154. .addClass('fc-last');
  155. }
  156. function getSkinCss(event, opt) {
  157. var source = event.source || {};
  158. var eventColor = event.color;
  159. var sourceColor = source.color;
  160. var optionColor = opt('eventColor');
  161. var backgroundColor =
  162. event.backgroundColor ||
  163. eventColor ||
  164. source.backgroundColor ||
  165. sourceColor ||
  166. opt('eventBackgroundColor') ||
  167. optionColor;
  168. var borderColor =
  169. event.borderColor ||
  170. eventColor ||
  171. source.borderColor ||
  172. sourceColor ||
  173. opt('eventBorderColor') ||
  174. optionColor;
  175. var textColor =
  176. event.textColor ||
  177. source.textColor ||
  178. opt('eventTextColor');
  179. var statements = [];
  180. if (backgroundColor) {
  181. statements.push('background-color:' + backgroundColor);
  182. }
  183. if (borderColor) {
  184. statements.push('border-color:' + borderColor);
  185. }
  186. if (textColor) {
  187. statements.push('color:' + textColor);
  188. }
  189. return statements.join(';');
  190. }
  191. function applyAll(functions, thisObj, args) {
  192. if ($.isFunction(functions)) {
  193. functions = [ functions ];
  194. }
  195. if (functions) {
  196. var i;
  197. var ret;
  198. for (i=0; i<functions.length; i++) {
  199. ret = functions[i].apply(thisObj, args) || ret;
  200. }
  201. return ret;
  202. }
  203. }
  204. function firstDefined() {
  205. for (var i=0; i<arguments.length; i++) {
  206. if (arguments[i] !== undefined) {
  207. return arguments[i];
  208. }
  209. }
  210. }