bootstrap-year-calendar.js 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110
  1. /* =========================================================
  2. * Bootstrap year calendar v1.1.0
  3. * Repo: https://github.com/Paul-DS/bootstrap-year-calendar
  4. * =========================================================
  5. * Created by Paul David-Sivelle
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. * ========================================================= */
  19. (function($) {
  20. var Calendar = function(element, options) {
  21. this.element = element;
  22. this.element.addClass('calendar');
  23. this._initializeEvents(options);
  24. this._initializeOptions(options);
  25. this.setYear(this.options.startYear);
  26. };
  27. Calendar.prototype = {
  28. constructor: Calendar,
  29. _initializeOptions: function(opt) {
  30. if(opt == null) {
  31. opt = [];
  32. }
  33. this.options = {
  34. startYear: !isNaN(parseInt(opt.startYear)) ? parseInt(opt.startYear) : new Date().getFullYear(),
  35. minDate: opt.minDate instanceof Date ? opt.minDate : null,
  36. maxDate: opt.maxDate instanceof Date ? opt.maxDate : null,
  37. language: (opt.language != null && dates[opt.language] != null) ? opt.language : 'en',
  38. allowOverlap: opt.allowOverlap != null ? opt.allowOverlap : true,
  39. displayWeekNumber: opt.displayWeekNumber != null ? opt.displayWeekNumber : false,
  40. displayDisabledDataSource: opt.displayDisabledDataSource != null ? opt.displayDisabledDataSource : false,
  41. displayHeader: opt.displayHeader != null ? opt.displayHeader : true,
  42. alwaysHalfDay: opt.alwaysHalfDay != null ? opt.alwaysHalfDay : false,
  43. enableRangeSelection: opt.enableRangeSelection != null ? opt.enableRangeSelection : false,
  44. disabledDays: opt.disabledDays instanceof Array ? opt.disabledDays : [],
  45. disabledWeekDays: opt.disabledWeekDays instanceof Array ? opt.disabledWeekDays : [],
  46. hiddenWeekDays: opt.hiddenWeekDays instanceof Array ? opt.hiddenWeekDays : [],
  47. roundRangeLimits: opt.roundRangeLimits != null ? opt.roundRangeLimits : false,
  48. dataSource: opt.dataSource instanceof Array ? opt.dataSource : [],
  49. style: opt.style == 'background' || opt.style == 'border' || opt.style == 'custom' ? opt.style : 'border',
  50. enableContextMenu: opt.enableContextMenu != null ? opt.enableContextMenu : false,
  51. contextMenuItems: opt.contextMenuItems instanceof Array ? opt.contextMenuItems : [],
  52. customDayRenderer : $.isFunction(opt.customDayRenderer) ? opt.customDayRenderer : null,
  53. customDataSourceRenderer : $.isFunction(opt.customDataSourceRenderer) ? opt.customDataSourceRenderer : null,
  54. weekStart: !isNaN(parseInt(opt.weekStart)) ? parseInt(opt.weekStart) : null
  55. };
  56. this._initializeDatasourceColors();
  57. },
  58. _initializeEvents: function(opt) {
  59. if(opt == null) {
  60. opt = [];
  61. }
  62. if(opt.yearChanged) { this.element.bind('yearChanged', opt.yearChanged); }
  63. if(opt.renderEnd) { this.element.bind('renderEnd', opt.renderEnd); }
  64. if(opt.clickDay) { this.element.bind('clickDay', opt.clickDay); }
  65. if(opt.dayContextMenu) { this.element.bind('dayContextMenu', opt.dayContextMenu); }
  66. if(opt.selectRange) { this.element.bind('selectRange', opt.selectRange); }
  67. if(opt.mouseOnDay) { this.element.bind('mouseOnDay', opt.mouseOnDay); }
  68. if(opt.mouseOutDay) { this.element.bind('mouseOutDay', opt.mouseOutDay); }
  69. },
  70. _initializeDatasourceColors: function() {
  71. for(var i = 0; i < this.options.dataSource.length; i++) {
  72. if(this.options.dataSource[i].color == null) {
  73. this.options.dataSource[i].color = colors[i % colors.length];
  74. }
  75. }
  76. },
  77. render: function() {
  78. this.element.empty();
  79. if(this.options.displayHeader) {
  80. this._renderHeader();
  81. }
  82. this._renderBody();
  83. this._renderDataSource();
  84. this._applyEvents();
  85. this.element.find('.months-container').fadeIn(500);
  86. this._triggerEvent('renderEnd', { currentYear: this.options.startYear });
  87. },
  88. _renderHeader: function() {
  89. var header = $(document.createElement('div'));
  90. header.addClass('calendar-header panel panel-default');
  91. var headerTable = $(document.createElement('table'));
  92. var prevDiv = $(document.createElement('th'));
  93. prevDiv.addClass('prev');
  94. if(this.options.minDate != null && this.options.minDate > new Date(this.options.startYear - 1, 11, 31)) {
  95. prevDiv.addClass('disabled');
  96. }
  97. var prevIcon = $(document.createElement('span'));
  98. prevIcon.addClass('glyphicon glyphicon-chevron-left');
  99. prevDiv.append(prevIcon);
  100. headerTable.append(prevDiv);
  101. var prev2YearDiv = $(document.createElement('th'));
  102. prev2YearDiv.addClass('year-title year-neighbor2 hidden-sm hidden-xs');
  103. prev2YearDiv.text(this.options.startYear - 2);
  104. if(this.options.minDate != null && this.options.minDate > new Date(this.options.startYear - 2, 11, 31)) {
  105. prev2YearDiv.addClass('disabled');
  106. }
  107. headerTable.append(prev2YearDiv);
  108. var prevYearDiv = $(document.createElement('th'));
  109. prevYearDiv.addClass('year-title year-neighbor hidden-xs');
  110. prevYearDiv.text(this.options.startYear - 1);
  111. if(this.options.minDate != null && this.options.minDate > new Date(this.options.startYear - 1, 11, 31)) {
  112. prevYearDiv.addClass('disabled');
  113. }
  114. headerTable.append(prevYearDiv);
  115. var yearDiv = $(document.createElement('th'));
  116. yearDiv.addClass('year-title');
  117. yearDiv.text(this.options.startYear);
  118. headerTable.append(yearDiv);
  119. var nextYearDiv = $(document.createElement('th'));
  120. nextYearDiv.addClass('year-title year-neighbor hidden-xs');
  121. nextYearDiv.text(this.options.startYear + 1);
  122. if(this.options.maxDate != null && this.options.maxDate < new Date(this.options.startYear + 1, 0, 1)) {
  123. nextYearDiv.addClass('disabled');
  124. }
  125. headerTable.append(nextYearDiv);
  126. var next2YearDiv = $(document.createElement('th'));
  127. next2YearDiv.addClass('year-title year-neighbor2 hidden-sm hidden-xs');
  128. next2YearDiv.text(this.options.startYear + 2);
  129. if(this.options.maxDate != null && this.options.maxDate < new Date(this.options.startYear + 2, 0, 1)) {
  130. next2YearDiv.addClass('disabled');
  131. }
  132. headerTable.append(next2YearDiv);
  133. var nextDiv = $(document.createElement('th'));
  134. nextDiv.addClass('next');
  135. if(this.options.maxDate != null && this.options.maxDate < new Date(this.options.startYear + 1, 0, 1)) {
  136. nextDiv.addClass('disabled');
  137. }
  138. var nextIcon = $(document.createElement('span'));
  139. nextIcon.addClass('glyphicon glyphicon-chevron-right');
  140. nextDiv.append(nextIcon);
  141. headerTable.append(nextDiv);
  142. header.append(headerTable);
  143. this.element.append(header);
  144. },
  145. _renderBody: function() {
  146. var monthsDiv = $(document.createElement('div'));
  147. monthsDiv.addClass('months-container');
  148. for(var m = 0; m < 12; m++) {
  149. /* Container */
  150. var monthDiv = $(document.createElement('div'));
  151. monthDiv.addClass('month-container');
  152. monthDiv.data('month-id', m);
  153. var firstDate = new Date(this.options.startYear, m, 1);
  154. var table = $(document.createElement('table'));
  155. table.addClass('month');
  156. /* Month header */
  157. var thead = $(document.createElement('thead'));
  158. var titleRow = $(document.createElement('tr'));
  159. var titleCell = $(document.createElement('th'));
  160. titleCell.addClass('month-title');
  161. titleCell.attr('colspan', this.options.displayWeekNumber ? 8 : 7);
  162. titleCell.text(dates[this.options.language].months[m]);
  163. titleRow.append(titleCell);
  164. thead.append(titleRow);
  165. var headerRow = $(document.createElement('tr'));
  166. if(this.options.displayWeekNumber) {
  167. var weekNumberCell = $(document.createElement('th'));
  168. weekNumberCell.addClass('week-number');
  169. weekNumberCell.text(dates[this.options.language].weekShort);
  170. headerRow.append(weekNumberCell);
  171. }
  172. var weekStart = this.options.weekStart ? this.options.weekStart : dates[this.options.language].weekStart;
  173. var d = weekStart;
  174. do
  175. {
  176. var headerCell = $(document.createElement('th'));
  177. headerCell.addClass('day-header');
  178. headerCell.text(dates[this.options.language].daysMin[d]);
  179. if(this._isHidden(d)) {
  180. headerCell.addClass('hidden');
  181. }
  182. headerRow.append(headerCell);
  183. d++;
  184. if(d >= 7)
  185. d = 0;
  186. }
  187. while(d != weekStart)
  188. thead.append(headerRow);
  189. table.append(thead);
  190. /* Days */
  191. var currentDate = new Date(firstDate.getTime());
  192. var lastDate = new Date(this.options.startYear, m + 1, 0);
  193. while(currentDate.getDay() != weekStart)
  194. {
  195. currentDate.setDate(currentDate.getDate() - 1);
  196. }
  197. while(currentDate <= lastDate)
  198. {
  199. var row = $(document.createElement('tr'));
  200. if(this.options.displayWeekNumber) {
  201. var weekNumberCell = $(document.createElement('td'));
  202. weekNumberCell.addClass('week-number');
  203. weekNumberCell.text(this.getWeekNumber(currentDate));
  204. row.append(weekNumberCell);
  205. }
  206. do
  207. {
  208. var cell = $(document.createElement('td'));
  209. cell.addClass('day');
  210. if(this._isHidden(currentDate.getDay())) {
  211. cell.addClass('hidden');
  212. }
  213. if(currentDate < firstDate) {
  214. cell.addClass('old');
  215. }
  216. else if(currentDate > lastDate) {
  217. cell.addClass('new');
  218. }
  219. else {
  220. if(this._isDisabled(currentDate)) {
  221. cell.addClass('disabled');
  222. }
  223. var cellContent = $(document.createElement('div'));
  224. cellContent.addClass('day-content');
  225. cellContent.text(currentDate.getDate());
  226. cell.append(cellContent);
  227. if(this.options.customDayRenderer) {
  228. this.options.customDayRenderer(cellContent, currentDate);
  229. }
  230. }
  231. row.append(cell);
  232. currentDate.setDate(currentDate.getDate() + 1);
  233. }
  234. while(currentDate.getDay() != weekStart)
  235. table.append(row);
  236. }
  237. monthDiv.append(table);
  238. monthsDiv.append(monthDiv);
  239. }
  240. this.element.append(monthsDiv);
  241. },
  242. _renderDataSource: function() {
  243. var _this = this;
  244. if(this.options.dataSource != null && this.options.dataSource.length > 0) {
  245. this.element.find('.month-container').each(function() {
  246. var month = $(this).data('month-id');
  247. var firstDate = new Date(_this.options.startYear, month, 1);
  248. var lastDate = new Date(_this.options.startYear, month + 1, 1);
  249. if((_this.options.minDate == null || lastDate > _this.options.minDate) && (_this.options.maxDate == null || firstDate <= _this.options.maxDate))
  250. {
  251. var monthData = [];
  252. for(var i = 0; i < _this.options.dataSource.length; i++) {
  253. if(!(_this.options.dataSource[i].startDate >= lastDate) || (_this.options.dataSource[i].endDate < firstDate)) {
  254. monthData.push(_this.options.dataSource[i]);
  255. }
  256. }
  257. if(monthData.length > 0) {
  258. $(this).find('.day-content').each(function() {
  259. var currentDate = new Date(_this.options.startYear, month, $(this).text());
  260. var nextDate = new Date(_this.options.startYear, month, currentDate.getDate() + 1);
  261. var dayData = [];
  262. if((_this.options.minDate == null || currentDate >= _this.options.minDate) && (_this.options.maxDate == null || currentDate <= _this.options.maxDate))
  263. {
  264. for(var i = 0; i < monthData.length; i++) {
  265. if(monthData[i].startDate < nextDate && monthData[i].endDate >= currentDate) {
  266. dayData.push(monthData[i]);
  267. }
  268. }
  269. if(dayData.length > 0 && (_this.options.displayDisabledDataSource || !_this._isDisabled(currentDate)))
  270. {
  271. _this._renderDataSourceDay($(this), currentDate, dayData);
  272. }
  273. }
  274. });
  275. }
  276. }
  277. });
  278. }
  279. },
  280. _renderDataSourceDay: function(elt, currentDate, events) {
  281. switch(this.options.style)
  282. {
  283. case 'border':
  284. var weight = 0;
  285. if(events.length == 1) {
  286. weight = 4;
  287. }
  288. else if(events.length <= 3) {
  289. weight = 2;
  290. }
  291. else {
  292. elt.parent().css('box-shadow', 'inset 0 -4px 0 0 black');
  293. }
  294. if(weight > 0)
  295. {
  296. var boxShadow = '';
  297. for (var i = 0; i < events.length; i++)
  298. {
  299. if(boxShadow != '') {
  300. boxShadow += ",";
  301. }
  302. boxShadow += 'inset 0 -' + (parseInt(i) + 1) * weight + 'px 0 0 ' + events[i].color;
  303. }
  304. elt.parent().css('box-shadow', boxShadow);
  305. }
  306. break;
  307. case 'background':
  308. elt.parent().css('background-color', events[events.length - 1].color);
  309. var currentTime = currentDate.getTime();
  310. if(events[events.length - 1].startDate.getTime() == currentTime)
  311. {
  312. elt.parent().addClass('day-start');
  313. if(events[events.length - 1].startHalfDay || this.options.alwaysHalfDay) {
  314. elt.parent().addClass('day-half');
  315. // Find color for other half
  316. var otherColor = 'transparent';
  317. for(var i = events.length - 2; i >= 0; i--) {
  318. if(events[i].startDate.getTime() != currentTime || (!events[i].startHalfDay && !this.options.alwaysHalfDay)) {
  319. otherColor = events[i].color;
  320. break;
  321. }
  322. }
  323. elt.parent().css('background', 'linear-gradient(-45deg, ' + events[events.length - 1].color + ', ' + events[events.length - 1].color + ' 49%, ' + otherColor + ' 51%, ' + otherColor + ')');
  324. }
  325. else if(this.options.roundRangeLimits) {
  326. elt.parent().addClass('round-left');
  327. }
  328. }
  329. else if(events[events.length - 1].endDate.getTime() == currentTime)
  330. {
  331. elt.parent().addClass('day-end');
  332. if(events[events.length - 1].endHalfDay || this.options.alwaysHalfDay) {
  333. elt.parent().addClass('day-half');
  334. // Find color for other half
  335. var otherColor = 'transparent';
  336. for(var i = events.length - 2; i >= 0; i--) {
  337. if(events[i].endDate.getTime() != currentTime || (!events[i].endHalfDay && !this.options.alwaysHalfDay)) {
  338. otherColor = events[i].color;
  339. break;
  340. }
  341. }
  342. elt.parent().css('background', 'linear-gradient(135deg, ' + events[events.length - 1].color + ', ' + events[events.length - 1].color + ' 49%, ' + otherColor + ' 51%, ' + otherColor + ')');
  343. }
  344. else if(this.options.roundRangeLimits) {
  345. elt.parent().addClass('round-right');
  346. }
  347. }
  348. break;
  349. case 'custom':
  350. if(this.options.customDataSourceRenderer) {
  351. this.options.customDataSourceRenderer.call(this, elt, currentDate, events);
  352. }
  353. break;
  354. }
  355. },
  356. _applyEvents: function () {
  357. var _this = this;
  358. /* Header buttons */
  359. this.element.find('.year-neighbor, .year-neighbor2').click(function() {
  360. if(!$(this).hasClass('disabled')) {
  361. _this.setYear(parseInt($(this).text()));
  362. }
  363. });
  364. this.element.find('.calendar-header .prev').click(function() {
  365. if(!$(this).hasClass('disabled')) {
  366. _this.element.find('.months-container').animate({'margin-left':'100%'},100, function() {
  367. _this.element.find('.months-container').css('visibility', 'hidden');
  368. _this.element.find('.months-container').css('margin-left', '0');
  369. setTimeout(function() {
  370. _this.setYear(_this.options.startYear - 1);
  371. }, 50);
  372. });
  373. }
  374. });
  375. this.element.find('.calendar-header .next').click(function() {
  376. if(!$(this).hasClass('disabled')) {
  377. _this.element.find('.months-container').animate({'margin-left':'-100%'},100, function() {
  378. _this.element.find('.months-container').css('visibility', 'hidden');
  379. _this.element.find('.months-container').css('margin-left', '0');
  380. setTimeout(function() {
  381. _this.setYear(_this.options.startYear + 1);
  382. }, 50);
  383. });
  384. }
  385. });
  386. var cells = this.element.find('.day:not(.old, .new, .disabled)');
  387. /* Click on date */
  388. cells.click(function(e) {
  389. e.stopPropagation();
  390. var date = _this._getDate($(this));
  391. _this._triggerEvent('clickDay', {
  392. element: $(this),
  393. which: e.which,
  394. date: date,
  395. events: _this.getEvents(date)
  396. });
  397. });
  398. /* Click right on date */
  399. cells.bind('contextmenu', function(e) {
  400. if(_this.options.enableContextMenu)
  401. {
  402. e.preventDefault();
  403. if(_this.options.contextMenuItems.length > 0)
  404. {
  405. _this._openContextMenu($(this));
  406. }
  407. }
  408. var date = _this._getDate($(this));
  409. _this._triggerEvent('dayContextMenu', {
  410. element: $(this),
  411. date: date,
  412. events: _this.getEvents(date)
  413. });
  414. });
  415. /* Range selection */
  416. if(this.options.enableRangeSelection) {
  417. cells.mousedown(function (e) {
  418. if(e.which == 1) {
  419. var currentDate = _this._getDate($(this));
  420. if(_this.options.allowOverlap || _this.getEvents(currentDate).length == 0)
  421. {
  422. _this._mouseDown = true;
  423. _this._rangeStart = _this._rangeEnd = currentDate;
  424. _this._refreshRange();
  425. }
  426. }
  427. });
  428. cells.mouseenter(function (e) {
  429. if (_this._mouseDown) {
  430. var currentDate = _this._getDate($(this));
  431. if(!_this.options.allowOverlap)
  432. {
  433. var newDate = new Date(_this._rangeStart.getTime());
  434. if(newDate < currentDate) {
  435. var nextDate = new Date(newDate.getFullYear(), newDate.getMonth(), newDate.getDate() + 1);
  436. while(newDate < currentDate) {
  437. if(_this.getEvents(nextDate).length > 0)
  438. {
  439. break;
  440. }
  441. newDate.setDate(newDate.getDate() + 1);
  442. nextDate.setDate(nextDate.getDate() + 1);
  443. }
  444. }
  445. else {
  446. var nextDate = new Date(newDate.getFullYear(), newDate.getMonth(), newDate.getDate() - 1);
  447. while(newDate > currentDate) {
  448. if(_this.getEvents(nextDate).length > 0)
  449. {
  450. break;
  451. }
  452. newDate.setDate(newDate.getDate() - 1);
  453. nextDate.setDate(nextDate.getDate() - 1);
  454. }
  455. }
  456. currentDate = newDate;
  457. }
  458. var oldValue = _this._rangeEnd;
  459. _this._rangeEnd = currentDate;
  460. if (oldValue.getTime() != _this._rangeEnd.getTime()) {
  461. _this._refreshRange();
  462. }
  463. }
  464. });
  465. $(window).mouseup(function (e) {
  466. if (_this._mouseDown) {
  467. _this._mouseDown = false;
  468. _this._refreshRange();
  469. var minDate = _this._rangeStart < _this._rangeEnd ? _this._rangeStart : _this._rangeEnd;
  470. var maxDate = _this._rangeEnd > _this._rangeStart ? _this._rangeEnd : _this._rangeStart;
  471. _this._triggerEvent('selectRange', {
  472. startDate: minDate,
  473. endDate: maxDate,
  474. events: _this.getEventsOnRange(minDate, new Date(maxDate.getFullYear(), maxDate.getMonth(), maxDate.getDate() + 1))
  475. });
  476. }
  477. });
  478. }
  479. /* Hover date */
  480. cells.mouseenter(function(e) {
  481. if(!_this._mouseDown)
  482. {
  483. var date = _this._getDate($(this));
  484. _this._triggerEvent('mouseOnDay', {
  485. element: $(this),
  486. date: date,
  487. events: _this.getEvents(date)
  488. });
  489. }
  490. });
  491. cells.mouseleave(function(e) {
  492. var date = _this._getDate($(this));
  493. _this._triggerEvent('mouseOutDay', {
  494. element: $(this),
  495. date: date,
  496. events: _this.getEvents(date)
  497. });
  498. });
  499. /* Responsive management */
  500. setInterval(function() {
  501. var calendarSize = $(_this.element).width();
  502. var monthSize = $(_this.element).find('.month').first().width() + 10;
  503. var monthContainerClass = 'month-container';
  504. if(monthSize * 6 < calendarSize) {
  505. monthContainerClass += ' col-xs-2';
  506. }
  507. else if(monthSize * 4 < calendarSize) {
  508. monthContainerClass += ' col-xs-3';
  509. }
  510. else if(monthSize * 3 < calendarSize) {
  511. monthContainerClass += ' col-xs-4';
  512. }
  513. else if(monthSize * 2 < calendarSize) {
  514. monthContainerClass += ' col-xs-6';
  515. }
  516. else {
  517. monthContainerClass += ' col-xs-12';
  518. }
  519. $(_this.element).find('.month-container').attr('class', monthContainerClass);
  520. }, 300);
  521. },
  522. _refreshRange: function () {
  523. var _this = this;
  524. this.element.find('td.day.range').removeClass('range')
  525. this.element.find('td.day.range-start').removeClass('range-start');
  526. this.element.find('td.day.range-end').removeClass('range-end');
  527. if (this._mouseDown) {
  528. var beforeRange = true;
  529. var afterRange = false;
  530. var minDate = _this._rangeStart < _this._rangeEnd ? _this._rangeStart : _this._rangeEnd;
  531. var maxDate = _this._rangeEnd > _this._rangeStart ? _this._rangeEnd : _this._rangeStart;
  532. this.element.find('.month-container').each(function () {
  533. var monthId = $(this).data('month-id');
  534. if (minDate.getMonth() <= monthId && maxDate.getMonth() >= monthId) {
  535. $(this).find('td.day:not(.old, .new)').each(function () {
  536. var date = _this._getDate($(this));
  537. if (date >= minDate && date <= maxDate) {
  538. $(this).addClass('range');
  539. if (date.getTime() == minDate.getTime()) {
  540. $(this).addClass('range-start');
  541. }
  542. if (date.getTime() == maxDate.getTime()) {
  543. $(this).addClass('range-end');
  544. }
  545. }
  546. });
  547. }
  548. });
  549. }
  550. },
  551. _openContextMenu: function(elt) {
  552. var contextMenu = $('.calendar-context-menu');
  553. if(contextMenu.length > 0) {
  554. contextMenu.hide();
  555. contextMenu.empty();
  556. }
  557. else {
  558. contextMenu = $(document.createElement('div'));
  559. contextMenu.addClass('calendar-context-menu');
  560. $('body').append(contextMenu);
  561. }
  562. var date = this._getDate(elt);
  563. var events = this.getEvents(date);
  564. for(var i = 0; i < events.length; i++) {
  565. var eventItem = $(document.createElement('div'));
  566. eventItem.addClass('item');
  567. eventItem.css('border-left', '4px solid ' + events[i].color);
  568. var eventItemContent = $(document.createElement('div'));
  569. eventItemContent.addClass('content');
  570. eventItemContent.text(events[i].name);
  571. eventItem.append(eventItemContent);
  572. var icon = $(document.createElement('span'));
  573. icon.addClass('glyphicon glyphicon-chevron-right');
  574. eventItem.append(icon);
  575. this._renderContextMenuItems(eventItem, this.options.contextMenuItems, events[i]);
  576. contextMenu.append(eventItem);
  577. }
  578. if(contextMenu.children().length > 0)
  579. {
  580. contextMenu.css('left', elt.offset().left + 25 + 'px');
  581. contextMenu.css('top', elt.offset().top + 25 + 'px');
  582. contextMenu.show();
  583. $(window).one('mouseup', function() {
  584. contextMenu.hide();
  585. });
  586. }
  587. },
  588. _renderContextMenuItems: function(parent, items, evt) {
  589. var subMenu = $(document.createElement('div'));
  590. subMenu.addClass('submenu');
  591. for(var i = 0; i < items.length; i++) {
  592. if(!items[i].visible || items[i].visible(evt)) {
  593. var menuItem = $(document.createElement('div'));
  594. menuItem.addClass('item');
  595. var menuItemContent = $(document.createElement('div'));
  596. menuItemContent.addClass('content');
  597. menuItemContent.text(items[i].text);
  598. menuItem.append(menuItemContent);
  599. if(items[i].click) {
  600. (function(index) {
  601. menuItem.click(function() {
  602. items[index].click(evt);
  603. });
  604. })(i);
  605. }
  606. var icon = $(document.createElement('span'));
  607. icon.addClass('glyphicon glyphicon-chevron-right');
  608. menuItem.append(icon);
  609. if(items[i].items && items[i].items.length > 0) {
  610. this._renderContextMenuItems(menuItem, items[i].items, evt);
  611. }
  612. subMenu.append(menuItem);
  613. }
  614. }
  615. if(subMenu.children().length > 0)
  616. {
  617. parent.append(subMenu);
  618. }
  619. },
  620. _getColor: function(colorString) {
  621. var div = $('<div />');
  622. div.css('color', colorString);
  623. },
  624. _getDate: function(elt) {
  625. var day = elt.children('.day-content').text();
  626. var month = elt.closest('.month-container').data('month-id');
  627. var year = this.options.startYear;
  628. return new Date(year, month, day);
  629. },
  630. _triggerEvent: function(eventName, parameters) {
  631. var event = $.Event(eventName);
  632. for(var i in parameters) {
  633. event[i] = parameters[i];
  634. }
  635. this.element.trigger(event);
  636. return event;
  637. },
  638. _isDisabled: function(date) {
  639. if((this.options.minDate != null && date < this.options.minDate) || (this.options.maxDate != null && date > this.options.maxDate))
  640. {
  641. return true;
  642. }
  643. if(this.options.disabledWeekDays.length > 0) {
  644. for(var d = 0; d < this.options.disabledWeekDays.length; d++){
  645. if(date.getDay() == this.options.disabledWeekDays[d]) {
  646. return true;
  647. }
  648. }
  649. }
  650. if(this.options.disabledDays.length > 0) {
  651. for(var d = 0; d < this.options.disabledDays.length; d++){
  652. if(date.getTime() == this.options.disabledDays[d].getTime()) {
  653. return true;
  654. }
  655. }
  656. }
  657. return false;
  658. },
  659. _isHidden: function(day) {
  660. if(this.options.hiddenWeekDays.length > 0) {
  661. for(var d = 0; d < this.options.hiddenWeekDays.length; d++) {
  662. if(day == this.options.hiddenWeekDays[d]) {
  663. return true;
  664. }
  665. }
  666. }
  667. return false;
  668. },
  669. getWeekNumber: function(date) {
  670. var tempDate = new Date(date.getTime());
  671. tempDate.setHours(0, 0, 0, 0);
  672. tempDate.setDate(tempDate.getDate() + 3 - (tempDate.getDay() + 6) % 7);
  673. var week1 = new Date(tempDate.getFullYear(), 0, 4);
  674. return 1 + Math.round(((tempDate.getTime() - week1.getTime()) / 86400000 - 3 + (week1.getDay() + 6) % 7) / 7);
  675. },
  676. getEvents: function(date) {
  677. return this.getEventsOnRange(date, new Date(date.getFullYear(), date.getMonth(), date.getDate() + 1));
  678. },
  679. getEventsOnRange: function(startDate, endDate) {
  680. var events = [];
  681. if(this.options.dataSource && startDate && endDate) {
  682. for(var i = 0; i < this.options.dataSource.length; i++) {
  683. if(this.options.dataSource[i].startDate < endDate && this.options.dataSource[i].endDate >= startDate) {
  684. events.push(this.options.dataSource[i]);
  685. }
  686. }
  687. }
  688. return events;
  689. },
  690. getYear: function() {
  691. return this.options.startYear;
  692. },
  693. setYear: function(year) {
  694. var parsedYear = parseInt(year);
  695. if(!isNaN(parsedYear)) {
  696. this.options.startYear = parsedYear;
  697. this.element.empty();
  698. if(this.options.displayHeader) {
  699. this._renderHeader();
  700. }
  701. var eventResult = this._triggerEvent('yearChanged', { currentYear: this.options.startYear, preventRendering: false });
  702. if(!eventResult.preventRendering) {
  703. this.render();
  704. }
  705. }
  706. },
  707. getMinDate: function() {
  708. return this.options.minDate;
  709. },
  710. setMinDate: function(date, preventRendering) {
  711. if(date instanceof Date) {
  712. this.options.minDate = date;
  713. if(!preventRendering) {
  714. this.render();
  715. }
  716. }
  717. },
  718. getMaxDate: function() {
  719. return this.options.maxDate;
  720. },
  721. setMaxDate: function(date, preventRendering) {
  722. if(date instanceof Date) {
  723. this.options.maxDate = date;
  724. if(!preventRendering) {
  725. this.render();
  726. }
  727. }
  728. },
  729. getStyle: function() {
  730. return this.options.style;
  731. },
  732. setStyle: function(style, preventRendering) {
  733. this.options.style = style == 'background' || style == 'border' || style == 'custom' ? style : 'border';
  734. if(!preventRendering) {
  735. this.render();
  736. }
  737. },
  738. getAllowOverlap: function() {
  739. return this.options.allowOverlap;
  740. },
  741. setAllowOverlap: function(allowOverlap) {
  742. this.options.allowOverlap = allowOverlap;
  743. },
  744. getDisplayWeekNumber: function() {
  745. return this.options.displayWeekNumber;
  746. },
  747. setDisplayWeekNumber: function(displayWeekNumber, preventRendering) {
  748. this.options.displayWeekNumber = displayWeekNumber;
  749. if(!preventRendering) {
  750. this.render();
  751. }
  752. },
  753. getDisplayHeader: function() {
  754. return this.options.displayHeader;
  755. },
  756. setDisplayHeader: function(displayHeader, preventRendering) {
  757. this.options.displayHeader = displayHeader;
  758. if(!preventRendering) {
  759. this.render();
  760. }
  761. },
  762. getDisplayDisabledDataSource: function() {
  763. return this.options.displayDisabledDataSource;
  764. },
  765. setDisplayDisabledDataSource: function(displayDisabledDataSource, preventRendering) {
  766. this.options.displayDisabledDataSource = displayDisabledDataSource;
  767. if(!preventRendering) {
  768. this.render();
  769. }
  770. },
  771. getAlwaysHalfDay: function() {
  772. return this.options.alwaysHalfDay;
  773. },
  774. setAlwaysHalfDay: function(alwaysHalfDay, preventRendering) {
  775. this.options.alwaysHalfDay = alwaysHalfDay;
  776. if(!preventRendering) {
  777. this.render();
  778. }
  779. },
  780. getEnableRangeSelection: function() {
  781. return this.options.enableRangeSelection;
  782. },
  783. setEnableRangeSelection: function(enableRangeSelection, preventRendering) {
  784. this.options.enableRangeSelection = enableRangeSelection;
  785. if(!preventRendering) {
  786. this.render();
  787. }
  788. },
  789. getDisabledDays: function() {
  790. return this.options.disabledDays;
  791. },
  792. setDisabledDays: function(disabledDays, preventRendering) {
  793. this.options.disabledDays = disabledDays instanceof Array ? disabledDays : [];
  794. if(!preventRendering) {
  795. this.render();
  796. }
  797. },
  798. getDisabledWeekDays: function() {
  799. return this.options.disabledWeekDays;
  800. },
  801. setDisabledWeekDays: function(disabledWeekDays, preventRendering) {
  802. this.options.disabledWeekDays = disabledWeekDays instanceof Array ? disabledWeekDays : [];
  803. if(!preventRendering) {
  804. this.render();
  805. }
  806. },
  807. getHiddenWeekDays: function() {
  808. return this.options.hiddenWeekDays;
  809. },
  810. setHiddenWeekDays: function(hiddenWeekDays, preventRendering) {
  811. this.options.hiddenWeekDays = hiddenWeekDays instanceof Array ? hiddenWeekDays : [];
  812. if(!preventRendering) {
  813. this.render();
  814. }
  815. },
  816. getRoundRangeLimits: function() {
  817. return this.options.roundRangeLimits;
  818. },
  819. setRoundRangeLimits: function(roundRangeLimits, preventRendering) {
  820. this.options.roundRangeLimits = roundRangeLimits;
  821. if(!preventRendering) {
  822. this.render();
  823. }
  824. },
  825. getEnableContextMenu: function() {
  826. return this.options.enableContextMenu;
  827. },
  828. setEnableContextMenu: function(enableContextMenu, preventRendering) {
  829. this.options.enableContextMenu = enableContextMenu;
  830. if(!preventRendering) {
  831. this.render();
  832. }
  833. },
  834. getContextMenuItems: function() {
  835. return this.options.contextMenuItems;
  836. },
  837. setContextMenuItems: function(contextMenuItems, preventRendering) {
  838. this.options.contextMenuItems = contextMenuItems instanceof Array ? contextMenuItems : [];
  839. if(!preventRendering) {
  840. this.render();
  841. }
  842. },
  843. getCustomDayRenderer: function() {
  844. return this.options.customDayRenderer;
  845. },
  846. setCustomDayRenderer: function(customDayRenderer, preventRendering) {
  847. this.options.customDayRenderer = $.isFunction(customDayRenderer) ? customDayRenderer : null;
  848. if(!preventRendering) {
  849. this.render();
  850. }
  851. },
  852. getCustomDataSourceRenderer: function() {
  853. return this.options.customDataSourceRenderer;
  854. },
  855. setCustomDataSourceRenderer: function(customDataSourceRenderer, preventRendering) {
  856. this.options.customDataSourceRenderer = $.isFunction(customDataSourceRenderer) ? customDataSourceRenderer : null;
  857. if(!preventRendering) {
  858. this.render();
  859. }
  860. },
  861. getLanguage: function() {
  862. return this.options.language;
  863. },
  864. setLanguage: function(language, preventRendering) {
  865. if(language != null && dates[language] != null) {
  866. this.options.language = language;
  867. if(!preventRendering) {
  868. this.render();
  869. }
  870. }
  871. },
  872. getDataSource: function() {
  873. return this.options.dataSource;
  874. },
  875. setDataSource: function(dataSource, preventRendering) {
  876. this.options.dataSource = dataSource instanceof Array ? dataSource : [];
  877. this._initializeDatasourceColors();
  878. if(!preventRendering) {
  879. this.render();
  880. }
  881. },
  882. getWeekStart: function() {
  883. return this.options.weekStart ? this.options.weekStart : dates[this.options.language].weekStart;
  884. },
  885. setWeekStart: function(weekStart, preventRendering) {
  886. this.options.weekStart = !isNaN(parseInt(weekStart)) ? parseInt(weekStart) : null;
  887. if(!preventRendering) {
  888. this.render();
  889. }
  890. },
  891. addEvent: function(evt, preventRendering) {
  892. this.options.dataSource.push(evt);
  893. if(!preventRendering) {
  894. this.render();
  895. }
  896. }
  897. }
  898. $.fn.calendar = function (options) {
  899. var calendar = new Calendar($(this) ,options);
  900. $(this).data('calendar', calendar);
  901. return calendar;
  902. }
  903. /* Events binding management */
  904. $.fn.yearChanged = function(fct) { $(this).bind('yearChanged', fct); }
  905. $.fn.renderEnd = function(fct) { $(this).bind('renderEnd', fct); }
  906. $.fn.clickDay = function(fct) { $(this).bind('clickDay', fct); }
  907. $.fn.dayContextMenu = function(fct) { $(this).bind('dayContextMenu', fct); }
  908. $.fn.selectRange = function(fct) { $(this).bind('selectRange', fct); }
  909. $.fn.mouseOnDay = function(fct) { $(this).bind('mouseOnDay', fct); }
  910. $.fn.mouseOutDay = function(fct) { $(this).bind('mouseOutDay', fct); }
  911. var dates = $.fn.calendar.dates = {
  912. en: {
  913. days: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"],
  914. daysShort: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"],
  915. daysMin: ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa", "Su"],
  916. months: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
  917. monthsShort: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
  918. weekShort: 'W',
  919. weekStart:0
  920. }
  921. };
  922. var colors = $.fn.calendar.colors = ['#2C8FC9', '#9CB703', '#F5BB00', '#FF4A32', '#B56CE2', '#45A597'];
  923. $(function(){
  924. $('[data-provide="calendar"]').each(function() {
  925. $(this).calendar();
  926. });
  927. });
  928. }(window.jQuery));