PluginProbe
UpStream: a Project Management Plugin for WordPress / 2.0.7
UpStream: a Project Management Plugin for WordPress v2.0.7
trunk 1.39.0 1.39.1 1.39.2 1.39.3 2.0.7 2.1.0
upstream / templates / assets / js / upstream.js

upstream.js in UpStream: a Project Management Plugin for WordPress 2.0.7, at templates/assets/js/upstream.js

1,024 lines 36.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /**
2 * Resize function without multiple trigger
3 *
4 * Usage:
5 * $(window).smartresize(function(){
6 * code here
7 * });
8 */
9 (function ($, sr) {
10
11 var debounce = function (func, threshold, execAsap) {
12 var timeout;
13
14 return function debounced () {
15 var obj = this, args = arguments;
16
17 function delayed () {
18 if (!execAsap)
19 func.apply(obj, args);
20 timeout = null;
21 }
22
23 if (timeout)
24 clearTimeout(timeout);
25 else if (execAsap)
26 func.apply(obj, args);
27
28 timeout = setTimeout(delayed, threshold || 100);
29 };
30 };
31
32 jQuery.fn[sr] = function (fn) { return fn ? this.bind('resize', debounce(fn)) : this.trigger(sr); };
33
34 })(jQuery, 'smartresize');
35
36 jQuery(document).ready(function ($) {
37 if (typeof $.fn.datepicker.dates === 'undefined') {
38 $.fn.datepicker.dates = {'en': []};
39 }
40
41 $.fn.datepicker.dates['en'] = {
42 days: [
43 upstream.langs.LB_SUNDAY,
44 upstream.langs.LB_MONDAY,
45 upstream.langs.LB_TUESDAY,
46 upstream.langs.LB_WEDNESDAY,
47 upstream.langs.LB_THURSDAY,
48 upstream.langs.LB_FRIDAY,
49 upstream.langs.LB_SATURDAY
50 ],
51 daysShort: [
52 upstream.langs.LB_SUN,
53 upstream.langs.LB_MON,
54 upstream.langs.LB_TUE,
55 upstream.langs.LB_WED,
56 upstream.langs.LB_THU,
57 upstream.langs.LB_FRI,
58 upstream.langs.LB_SAT
59 ],
60 daysMin: [
61 upstream.langs.LB_SU,
62 upstream.langs.LB_MO,
63 upstream.langs.LB_TU,
64 upstream.langs.LB_WE,
65 upstream.langs.LB_TH,
66 upstream.langs.LB_FR,
67 upstream.langs.LB_SA
68 ],
69 months: [
70 upstream.langs.LB_JANUARY,
71 upstream.langs.LB_FEBRUARY,
72 upstream.langs.LB_MARCH,
73 upstream.langs.LB_APRIL,
74 upstream.langs.LB_MAY,
75 upstream.langs.LB_JUNE,
76 upstream.langs.LB_JULY,
77 upstream.langs.LB_AUGUST,
78 upstream.langs.LB_SEPTEMBER,
79 upstream.langs.LB_OCTOBER,
80 upstream.langs.LB_NOVEMBER,
81 upstream.langs.LB_DECEMBER
82 ],
83 monthsShort: [
84 upstream.langs.LB_JAN,
85 upstream.langs.LB_FEB,
86 upstream.langs.LB_MAR,
87 upstream.langs.LB_APR,
88 upstream.langs.LB_MAY,
89 upstream.langs.LB_JUN,
90 upstream.langs.LB_JUL,
91 upstream.langs.LB_AUG,
92 upstream.langs.LB_SEP,
93 upstream.langs.LB_OCT,
94 upstream.langs.LB_NOV,
95 upstream.langs.LB_DEC
96 ],
97 today: upstream.langs.LB_TODAY,
98 clear: upstream.langs.LB_CLEAR,
99 format: 'mm/dd/yyyy',
100 titleFormat: 'MM yyyy', /* Leverages same syntax as 'format' */
101 weekStart: 0
102 };
103
104 $('[data-toggle="tooltip"]').tooltip();
105
106 var setContentHeight = function () {
107 $('.right_col').css('min-height', $(window).height());
108
109 var bodyHeight = $('body').outerHeight(),
110 footerHeight = $('body').hasClass('footer_fixed') ? -10 : $('footer').height(),
111 leftColHeight = $('.left_col').eq(1).height() + $('.sidebar-footer').height(),
112 contentHeight = bodyHeight < leftColHeight ? leftColHeight : bodyHeight;
113
114 contentHeight -= $('.nav_menu').height() + footerHeight;
115
116 $('.right_col').css('min-height', contentHeight);
117 };
118 window.setContentHeight = setContentHeight;
119
120 $('#sidebar-menu').find('a').on('click', function (ev) {
121
122 var $li = $(this).parent();
123
124 if ($li.is('.active')) {
125 $li.removeClass('active active-sm');
126 $('ul:first', $li).slideUp(function () {
127 setContentHeight();
128 });
129 } else {
130 if (!$li.parent().is('.child_menu')) {
131 $('#sidebar-menu').find('li').removeClass('active active-sm');
132 $('#sidebar-menu').find('li ul').slideUp();
133 }
134
135 $li.addClass('active');
136
137 $('ul:first', $li).slideDown(function () {
138 setContentHeight();
139 });
140 }
141 });
142
143 $('#menu_toggle').on('click', function () {
144 if ($('body').hasClass('nav-md')) {
145 $('#sidebar-menu').find('li.active ul').hide();
146 $('#sidebar-menu').find('li.active').addClass('active-sm').removeClass('active');
147 } else {
148 $('#sidebar-menu').find('li.active-sm ul').show();
149 $('#sidebar-menu').find('li.active-sm').addClass('active').removeClass('active-sm');
150 }
151
152 $('body').toggleClass('nav-md nav-sm');
153
154 setContentHeight();
155 });
156
157 var cmp = window.location.href;
158
159 $('#sidebar-menu').find('a[href="' + window.location.href.split('?')[0] + '"]').parent('li').addClass('current-page');
160
161 $('#sidebar-menu').find('a').filter(function () {
162 return this.href == window.location.href.split('?')[0];
163 }).parent('li').addClass('current-page').parents('ul').slideDown(function () {
164 setContentHeight();
165 }).parent().addClass('active');
166
167 $(window).smartresize(function () {
168 setContentHeight();
169 });
170
171 setContentHeight();
172
173 if ($.fn.mCustomScrollbar) {
174 $('.menu_fixed').mCustomScrollbar({
175 autoHideScrollbar: true,
176 theme: 'minimal',
177 mouseWheel: {preventDefault: true}
178 });
179 }
180
181 $(window).trigger('upstream-sidebar');
182 });
183
184
185
186 jQuery(document).ready(function ($) {
187 $('.collapse-link').on('click', function () {
188 var $boxPanel = $(this).closest('.x_panel'),
189 $icon = $(this).find('i'),
190 $boxContent = $boxPanel.find('.x_content');
191
192
193 if ($boxPanel.attr('style')) {
194 $boxContent.slideToggle(200, function () {
195 $boxPanel.removeAttr('style');
196 });
197 } else {
198 $boxContent.slideToggle(200);
199 $boxPanel.css('height', 'auto');
200 }
201
202 $icon.toggleClass('fa-chevron-up fa-chevron-down');
203
204 var state = $icon.hasClass('fa-chevron-up') ? 'opened' : 'closed',
205 section = $boxPanel.data('section');
206
207
208 $.ajax({
209 url: upstream.ajaxurl,
210 type: 'post',
211 data: {
212 action: 'upstream_collapse_update',
213 nonce: upstream.security,
214 section: section,
215 state: state
216 }
217 });
218 });
219
220 $('.close-link').click(function () {
221 var $boxPanel = $(this).closest('.x_panel');
222
223 $boxPanel.remove();
224 });
225
226 $('#project-dashboard.sortable').sortable({
227 placeholder: 'ui-state-highlight',
228 cancel: 'input,textarea,button,select,option,.ui-state-disabled,.navbar-right',
229 handle: '.x_title i.sortable_handler',
230 update: function (event, ui) {
231 var rows = $('#project-dashboard').sortable('toArray');
232
233
234 $.ajax({
235 url: upstream.ajaxurl,
236 type: 'post',
237 data: {
238 action: 'upstream_panel_order_update',
239 nonce: upstream.security,
240 rows: rows
241 }
242 });
243 }
244 });
245 $('#project-dashboard.sortable .x_title').disableSelection();
246 });
247
248
249 (function (window, document, $, NProgress, undefined) {
250 if (!NProgress) return;
251
252 NProgress.start();
253
254 $(window).on('load', function () {
255 NProgress.done();
256 });
257 })(window, window.document, jQuery, NProgress || null);
258
259 (function (window, document, $, undefined) {
260 $(document).ready(function () {
261 $('.c-discussion').on('click', '.o-comment[data-id] a[data-action="comment.go_to_reply"]', function (e) {
262 e.preventDefault();
263
264 var targetComment = $($(this).attr('href'));
265 if (targetComment.length === 0) {
266 console.error('Comment not found.');
267 return;
268 }
269
270 var wrapper = $(targetComment.parents('.c-discussion'));
271
272 wrapper.animate({
273 scrollTop: targetComment.get(0).offsetTop
274 }, function () {
275 targetComment.addClass('s-highlighted');
276 setTimeout(function () {
277 targetComment.removeClass('s-highlighted');
278 }, 750);
279 });
280 });
281
282 function highlighComment (comment_selector) {
283 var comment = $(comment_selector);
284 if (comment.length === 0) return;
285
286 var wrapper = $($(comment.parents('.c-comments')).get(0));
287 if (wrapper.length === 0) return;
288
289 var SCROLL_DURATION = 250;
290 var HIGHLIGH_CLASS = 's-highlighted';
291
292 var scrollElToOffset = function (subject, offset, onSuccessCallback) {
293 subject.animate({
294 scrollTop: offset,
295 duration: SCROLL_DURATION
296 }, onSuccessCallback);
297 };
298
299 var doHighlighComment = function (comment) {
300 comment.addClass(HIGHLIGH_CLASS);
301 setTimeout(function () {
302 comment.removeClass(HIGHLIGH_CLASS);
303 }, 750);
304 };
305
306 var wrapperDataType = wrapper.attr('data-type');
307 if (wrapperDataType === 'project') {
308 var bodyHasScrolled = false;
309 scrollElToOffset($('html, body'), comment.offset().top, function () {
310 if (bodyHasScrolled) return;
311
312 bodyHasScrolled = true;
313
314 scrollElToOffset(wrapper, comment.get(0).offsetTop, function () {
315 doHighlighComment(comment);
316 });
317 });
318 } else {
319 var tr = $($(wrapper.parents('tr')).get(0));
320
321 $('> td[data-name]', tr).trigger('click');
322
323 var bodyHasScrolled = false;
324 scrollElToOffset($('html, body'), tr.offset().top, function () {
325 if (bodyHasScrolled) return;
326
327 bodyHasScrolled = true;
328
329 comment = $('tr.child ' + comment_selector, tr.parent());
330 wrapper = $($(comment.parents('.c-comments')).get(0));
331
332 scrollElToOffset(wrapper, comment.get(0).offsetTop - comment.height(), function () {
333 doHighlighComment(comment);
334 });
335 });
336 }
337 }
338
339 setTimeout(function () {
340 var commentIdRegexp = new RegExp(/^\#comment\-\d+/i);
341 if (commentIdRegexp.test(window.location.hash)) {
342 highlighComment(window.location.hash);
343 }
344 }, 250);
345 });
346 })(window, window.document, jQuery || {});
347
348 (function (window, document, $, $data, TableExport, undefined) {
349 $(document).ready(function () {
350 $('.o-data-table tr[data-id] a[data-toggle="up-modal"]').on('click', function (e) {
351 e.preventDefault();
352 e.stopPropagation();
353
354 var self = $(this);
355 var tr = self.parents('tr[data-id]');
356
357 var modal = new Modal({
358 el: self.attr('data-up-target')
359 });
360
361 modal.on('show', function (modal, e) {
362 $('[data-column]', tr).each(function () {
363 var columnEl = $(this);
364 var columnName = $(this).attr('data-column');
365
366 if (['notes', 'description', 'comments'].indexOf(columnName) >= 0) {
367 $('[data-column="' + columnName + '"]', modal.el).html(columnEl.html());
368 } else {
369 $('[data-column="' + columnName + '"]', modal.el).text(columnEl.text());
370 }
371 });
372 });
373
374 modal.on('hidden', function (modal, e) {
375 $('[data-column]', modal.el).html('');
376 });
377
378 modal.show();
379 });
380
381 function createOrderDirectionEl (direction) {
382 var span = $('<span></span>', {
383 class: 'pull-right o-order-direction'
384 });
385
386 span.append($('<i></i>', {
387 class: 'fa fa-sort' + (!direction ? '' : (direction === 'ASC' ? '-asc' : '-desc'))
388 }));
389
390 return span;
391 }
392
393 function orderTable (columnName, direction, table) {
394 var trs = $('> tbody > tr[data-id]', table);
395
396 if (trs.length === 0) return;
397
398 var data = [];
399
400 var tr, columnValue;
401
402 var data = [];
403 trs.each(function (trIndex) {
404 var tr = $(this);
405
406 if ($('[data-column="' + columnName + '"]', tr).attr('data-order')) {
407 columnValue = $('[data-column="' + columnName + '"]', tr).attr('data-order');
408 } else {
409 columnValue = $('[data-column="' + columnName + '"]', tr).attr('data-value') || '';
410 }
411
412 data.push({
413 index: trIndex,
414 value: columnValue.toUpperCase(),
415 children: $('> tbody > tr[data-parent="' + tr.attr('data-id') + '"]', table).clone()
416 });
417 });
418
419 data.sort(function (a, b) {
420 var comparison = a.value.localeCompare(b.value);
421
422 if (direction === 'DESC' && comparison !== 0) {
423 comparison *= -1;
424 }
425
426 return comparison;
427 });
428
429 $('> tbody > tr', table).remove();
430
431 $.each(data, function (trNewIndex) {
432 var tr = $(trs.get(this.index), table);
433
434 tr.removeClass('t-row-odd t-row-even');
435
436 if ((trNewIndex + 1) % 2 === 0) {
437 tr.addClass('t-row-even');
438 } else {
439 tr.addClass('t-row-odd');
440 }
441
442 $('> tbody', table).append(tr);
443
444 if (this.children.length > 0) {
445 $('> tbody', table).append(this.children);
446 }
447 });
448
449 table.attr('data-order-dir', direction)
450 .attr('data-ordered-by', columnName);
451
452 var th = $('thead tr th[data-column="' + columnName + '"]', table);
453 $('.o-order-direction', th).remove();
454
455 th.append(createOrderDirectionEl(direction));
456 }
457
458 $('.o-data-table').on('click', 'thead th.is-orderable[role="button"]', function (e) {
459 e.preventDefault();
460
461 var self = $(this);
462 var wrapper = $(self.parent());
463 var table = wrapper.parents('table');
464 var column = self.attr('data-column');
465
466 $('.o-order-direction', wrapper).remove();
467 $('th.is-orderable[role="button"]', wrapper).append(createOrderDirectionEl(null));
468 $('.o-order-direction', self).remove();
469
470 if (self.hasClass('is-ordered')) {
471 var orderDir = (self.attr('data-order-dir') || 'DESC').toUpperCase();
472 var newOrderDir = orderDir === 'DESC' ? 'ASC' : 'DESC';
473 } else {
474 $('.is-ordered', wrapper).removeClass('is-ordered');
475 $('th[data-order-dir]', wrapper).attr('data-order-dir', null);
476
477 var newOrderDir = 'ASC';
478 }
479
480 self.attr('data-order-dir', newOrderDir);
481 self.append(createOrderDirectionEl(newOrderDir));
482 self.addClass('is-ordered');
483
484 orderTable(column, newOrderDir, $(self.parents('table.o-data-table')));
485
486 $.ajax({
487 url: upstream.ajaxurl,
488 type: 'post',
489 data: {
490 action: 'upstream_ordering_update',
491 nonce: upstream.security,
492 column: column,
493 orderDir: newOrderDir,
494 tableId: table.attr('id')
495 }
496 });
497 });
498
499 function sortTable (columnName, columnValue, filtersWrapper) {
500 var table = $(filtersWrapper.attr('data-target'));
501 var filtersMap = [];
502
503 var filters = $('[data-column]', filtersWrapper);
504 filters.each(function () {
505 var self = $(this);
506
507 var value = self.val();
508 if (typeof value === 'string') {
509 value = value.trim();
510 } else if (value) {
511 value = value.filter(function (elValue) { return elValue.length > 0; });
512 } else {
513 value = '';
514 }
515
516 filtersMap[self.attr('data-column')] = value.length > 0 ? value : null;
517 filtersMap.push({
518 column: self.attr('data-column'),
519 value: value.length > 0 ? value : null,
520 comparator: self.attr('data-compare-operator') || 'exact'
521 });
522 });
523
524 $('tbody tr[data-empty-row]', table).remove();
525
526 var filtersHasChanged = false;
527 var trs = $('tbody tr[data-id]', table);
528
529 trs.removeClass('is-filtered');
530
531 trs.each(function (trIndex) {
532
533
534 var tr = $(this);
535 var shouldDisplay = true;
536
537 var filter, filterIndex, filterColumnValue, columnValue, comparator, dataWrapper;
538 for (filterIndex = 0; filterIndex < filtersMap.length; filterIndex++) {
539 filter = filtersMap[filterIndex];
540 if (filter.value === null) {
541 continue;
542 }
543
544 dataWrapper = $('[data-column="' + filter.column + '"]', tr);
545 if (dataWrapper.length === 0) {
546 dataWrapper = $('[data-column="' + filter.column + '"]', $('tbody tr[data-parent="' + tr.attr('data-id') + '"]'));
547 if (dataWrapper.length === 0) {
548 continue;
549 } else {
550 dataWrapper = $('[data-value]', dataWrapper);
551 }
552 }
553
554 columnValue = dataWrapper.attr('data-value');
555
556 if (filter.comparator === 'contains') {
557 if (typeof filter.value === 'string') {
558 comparator = new RegExp(filter.value, 'i');
559 if (!comparator.test(columnValue)) {
560 shouldDisplay = false;
561 break;
562 }
563 } else {
564 for (var valueIndex in filter.value) {
565 comparator = new RegExp(filter.value[valueIndex], 'i');
566 var found = false;
567
568 if (filter.value[valueIndex] === '__none__') {
569 if (!columnValue || columnValue === '__none__') {
570 found = true;
571 break;
572 }
573 }
574 else {
575 if (comparator.test(columnValue)) {
576 found = true;
577 break;
578 }
579 if (!found) {
580 shouldDisplay = false;
581 break;
582 }
583
584 }
585 }
586 }
587 } else if (filter.comparator === 'exact') {
588 if (typeof filter.value === 'string') {
589 if (columnValue != filter.value) {
590 shouldDisplay = false;
591 break;
592 }
593 } else {
594 var found = false;
595 for (var valueIndex in filter.value) {
596 if (filter.value[valueIndex] === '__none__') {
597 if (!columnValue || columnValue === '__none__') {
598 found = true;
599 break;
600 }
601 } else {
602 if (columnValue === filter.value[valueIndex] || columnValue.split(',').indexOf(filter.value[valueIndex]) >= 0) {
603 found = true;
604 break;
605 }
606 }
607 }
608 if (!found) {
609 shouldDisplay = false;
610 break;
611 }
612
613 }
614 } else if (filter.comparator === '>') {
615 if (columnValue <= filter.value) {
616 shouldDisplay = false;
617 break;
618 }
619 } else if (filter.comparator === '>=') {
620 if (columnValue < filter.value) {
621 shouldDisplay = false;
622 break;
623 }
624 } else if (filter.comparator === '<') {
625 if (columnValue >= filter.value) {
626 shouldDisplay = false;
627 break;
628 }
629 } else if (filter.comparator === '<=') {
630 if (columnValue > filter.value) {
631 shouldDisplay = false;
632 break;
633 }
634 } else if (filter.value === '__none__') {
635 if (columnValue && columnValue != '__none__') {
636 shouldDisplay = false;
637 break;
638 }
639 } else {
640 if (typeof filter.value === 'string') {
641 if (columnValue.localeCompare(filter.value) != 0) {
642 shouldDisplay = false;
643 break;
644 }
645 } else {
646 var found = false;
647 for (var valueIndex in filter.value) {
648 if (filter.value[valueIndex] === '__none__') {
649 if (!columnValue || columnValue === '__none__') {
650 found = true;
651 break;
652 }
653 } else {
654 if (columnValue.localeCompare(filter.value[valueIndex]) != 0) {
655 found = true;
656 break;
657 }
658 }
659
660 if (!found) {
661 shouldDisplay = false;
662 break;
663 }
664 }
665
666
667 }
668 }
669
670 if (!shouldDisplay) {
671 break;
672 }
673
674 }
675
676 if (shouldDisplay) {
677 tr.addClass('is-filtered').show();
678 } else {
679 tr.hide();
680 }
681
682
683
684
685 });
686
687
688 var filteredRows = $('tbody tr[data-id]:visible', table);
689 if (filteredRows.length === 0) {
690 var thsCount = $('thead th', table).length;
691 var tr = $('<tr data-empty-row><td colspan="' + thsCount + '" class="text-center">'+upstream.langs.LB_NO_RESULTS+'</td></tr>');
692 var tBody = $('tbody', table);
693
694 if (tBody.children('[data-empty-row]').length === 0) {
695 tBody.append(tr);
696 }
697 } else {
698 $('tbody tr[data-id]:visible', table).addClass('is-filtered');
699 }
700
701 $(table).parent().find('.sub_count').html(filteredRows.length);
702 displayValue = filteredRows.length > 0 ? 'inline' : 'none';
703 $(table).parent().find('.sub_count').parent().find("span.p_count").css("display", displayValue);
704
705 $('#pro_count').html(filteredRows.length);
706 $('#pro_count').parent().find("span.p_count").css("display", displayValue);
707 }
708
709 var filterDataTable = function (e, self) {
710 if (typeof e === 'object' && e != null) {
711 e.preventDefault();
712 }
713
714 if (typeof self === 'undefined') {
715 self = $(this);
716 } else {
717 self = $(self);
718 }
719
720 var filterColumn = self.attr('data-column');
721 var filterValue = self.val() || [];
722
723 sortTable(filterColumn, filterValue || '', $(self.parents('form').get(0)));
724 };
725
726 $('.c-data-table .c-data-table__filters .o-select2').on('change', filterDataTable);
727
728 var statusFilter = $('.c-data-table__filters #projects-filters .o-select2[data-column="status"]')[0];
729 if (typeof statusFilter !== 'undefined') {
730 filterDataTable(null, statusFilter);
731 }
732 statusFilter = $('.c-data-table__filters #tasks-filters .o-select2[data-column="status"]')[0];
733 if (typeof statusFilter !== 'undefined') {
734 filterDataTable(null, statusFilter);
735 }
736 statusFilter = $('.c-data-table__filters #bugs-filters .o-select2[data-column="status"]')[0];
737 if (typeof statusFilter !== 'undefined') {
738 filterDataTable(null, statusFilter);
739 }
740
741 $('.c-data-table .c-data-table__filters input[type="search"]').on('keyup', function (e) {
742 e.preventDefault();
743
744 var self = $(this);
745
746 if (!self.attr('data-searchurl') || typeof(self.attr('data-searchurl')) === 'undefined') {
747 var filterColumn = self.attr('data-column');
748
749 var value = self.val();
750
751 var wrapper = $(self.parents('.c-data-table__filters'));
752 $('.form-control[data-column="' + filterColumn + '"]', wrapper).val(value);
753
754 sortTable(filterColumn, value, $(self.parents('form').get(0)));
755 } else if (e.keyCode == 13) {
756 var searchurl = self.attr('data-searchurl');
757 window.location = searchurl.replace('_SEARCH_STR_', encodeURIComponent(self.val()) );
758 }
759 });
760
761
762 $('.o-data-table').on('click', 'tr.is-expandable > td:first-child', function (e) {
763 var self = $(this);
764 var tr = $(self.parents('tr[data-id]').get(0));
765 var table = $(tr.parents('.o-data-table').get(0));
766 var trChild = $('tr[data-parent="' + tr.attr('data-id') + '"]', table);
767
768 if (trChild.length > 0) {
769 if (!tr.hasClass('is-expanded')) {
770 tr.addClass('is-expanded')
771 .attr('aria-expanded', 'true');
772 trChild.show();
773
774 $('.fa.fa-angle-right', self)
775 .removeClass('fa-angle-right')
776 .addClass('fa-angle-down');
777 } else {
778 tr.removeClass('is-expanded')
779 .attr('aria-expanded', 'false');
780 trChild.hide();
781
782 $('.fa.fa-angle-down', self)
783 .removeClass('fa-angle-down')
784 .addClass('fa-angle-right');
785 }
786 }
787 });
788
789 if (typeof($data.datepickerDateFormat) === 'undefined' || ! $data.datepickerDateFormat) {
790 alert('UpStream requires you to set a valid WordPress date format. To do so, go to your WordPress admin dashboard and click Settings -> General. Under "Date Format" choose one of the available formats.');
791 }
792
793 try {
794 $('.r-datepicker').datepicker({
795 todayBtn: 'linked',
796 clearBtn: true,
797 autoclose: true,
798 keyboardNavigation: false,
799 format: 'yyyy-mm-dd'
800 }).on('change', function (e) {
801
802 });
803
804 $('.o-datepicker').datepicker({
805 todayBtn: 'linked',
806 clearBtn: true,
807 autoclose: true,
808 keyboardNavigation: false,
809 format: $data.datepickerDateFormat
810 }).on('change', function (e) {
811 var self = $(this);
812
813 var value = self.datepicker('getDate');
814
815 if (value) {
816 value = ((+new Date(value)) / 1000) - (60 * (new Date()).getTimezoneOffset());
817 }
818
819 var hiddenField = $('#' + self.attr('id') + '_timestamp');
820 if (hiddenField.length > 0) {
821 hiddenField.val(value);
822 }
823 });
824 } catch (err) {
825 alert('UpStream requires you to set a valid date format (found "' + $data.datepickerDateFormat + '"). To do so, go to your WordPress admin dashboard and click Settings -> General. Under "Date Format" choose one of the available formats, or contact us with this message for help.');
826 }
827
828 $('.c-data-table .c-data-table__filters .o-datepicker').on('change', function () {
829 var self = $(this);
830
831 var filterColumn = self.attr('data-column');
832 var wrapper = $(self.parents('.c-data-table__filters'));
833 var value = self.val();
834
835 var hiddenField = $('#' + self.attr('id') + '_timestamp');
836 if (hiddenField.length > 0) {
837 sortTable(hiddenField.attr('data-column'), value, $(self.parents('form').get(0)));
838 }
839 });
840
841 function cloneTableForExport (table) {
842 var clonedTable = table.clone();
843 $('thead tr', clonedTable).prepend($('<th>#</th>'));
844
845 $('tbody tr:not(.is-filtered)', clonedTable).remove();
846
847 var visibleIndex = 0;
848 $('tbody tr[data-id]', clonedTable).each(function () {
849 visibleIndex++;
850
851 $(this).prepend($('<td>' + visibleIndex + '</td>'));
852 });
853
854 return clonedTable;
855 }
856
857 function getCurrentDate () {
858 var now = new Date();
859 var currentMonth = now.getMonth() + 1;
860 var currentDate = [
861 now.getFullYear(),
862 (currentMonth < 10 ? '0' + currentMonth : currentMonth),
863 now.getDate()
864 ];
865
866 var fileName = currentDate.join('');
867
868 return fileName;
869 }
870
871 function isExportedDataFormatValid (format) {
872 var allowedFormats = ['txt', 'csv'];
873
874 return allowedFormats.indexOf(format) >= 0;
875 }
876
877 function exportTableData (table, targetFormat, charset) {
878 if (!isExportedDataFormatValid(targetFormat)) {
879 return false;
880 }
881
882 charset = (typeof charset === 'undefined'
883 || !charset
884 ? 'utf-8'
885 : charset
886 );
887
888 var clonedTable = cloneTableForExport(table);
889
890 $('> thead th[data-type="file"]', clonedTable).each(function () {
891 var th = $(this);
892
893 $('> tbody > tr > td[data-column="' + th.attr('data-column') + '"]', clonedTable).each(function () {
894 var td = $(this);
895
896 var a = $('a', td);
897 if (a.length > 0) {
898 td.text(a.attr('href'));
899 }
900 });
901 });
902
903 var data = clonedTable.tableExport({
904 trimWhitespace: true
905 }).getExportData();
906
907 var tableId = table.attr('id');
908
909 var exportedData = data[tableId][targetFormat];
910
911 var dataBlob = new Blob([exportedData.data], {
912 type: exportedData.mimeType + ';charset=' + charset
913 });
914
915 var fileName = getCurrentDate() + '-' + exportedData.filename + exportedData.fileExtension;
916
917 saveAs(dataBlob, fileName);
918
919 return true;
920 }
921
922 $('.c-data-table [data-action="export"]').on('click', function (e) {
923 e.preventDefault();
924
925 var self = $(this);
926 var fileType = (self.attr('data-type') || '').toLowerCase();
927
928 if (!isExportedDataFormatValid(fileType)) {
929 console.error('Invalid mime type "' + fileType + '"');
930 return;
931 }
932
933 var cTable = $(self.parents('.c-data-table'));
934 var table = $('.o-data-table', cTable);
935 if (cTable.length > 0
936 && table.length > 0) {
937 exportTableData(table, fileType);
938 }
939 });
940
941 $('.c-data-table select.form-control:not([multiple])').select2({
942 allowClear: true
943 });
944
945 $('.c-data-table select.form-control[multiple]').select2({
946 allowClear: false
947 });
948
949 $('.c-data-table').each(function () {
950 var self = $(this);
951
952 var table = $('.o-data-table', self);
953 var order_by = table.attr('data-ordered-by') || '';
954 var order_dir = table.attr('data-order-dir') || 'DESC';
955
956 if (order_by.length > 0) {
957 orderTable(order_by, order_dir, table);
958 }
959 });
960
961 (function () {
962 function generateContrastColor (baseColor) {
963 var d = 0;
964
965 var a = 1 - (0.299 * baseColor.r + 0.587 * baseColor.g + 0.114 * baseColor.b) / 255;
966 if (a >= 0.4) {
967 d = 255;
968 }
969
970 var newColor = {
971 r: d,
972 g: d,
973 b: d
974 };
975
976 return newColor;
977 }
978
979 function hexToRGB (hexColor) {
980 var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hexColor);
981 var rgb = result ? {
982 r: parseInt(result[1], 16),
983 g: parseInt(result[2], 16),
984 b: parseInt(result[3], 16)
985 } : null;
986
987 var color = new Color(rgb);
988
989 return color;
990 }
991
992 function componentToHex (c) {
993 var hex = c.toString(16);
994 return hex.length == 1 ? '0' + hex : hex;
995 }
996
997 function rgbToHex (r, g, b) {
998 return '#' + componentToHex(r) + componentToHex(g) + componentToHex(b);
999 }
1000
1001 $('.up-o-label').each(function () {
1002 var self = $(this);
1003
1004 var bgColor = self.css('background-color');
1005 if (bgColor) {
1006 bgColor = bgColor.replace(/rgb\(|\)/ig, '').replace(/\s+/g, '').split(',');
1007 if (bgColor.length === 3) {
1008 bgColor = {
1009 r: bgColor[0],
1010 g: bgColor[1],
1011 b: bgColor[2]
1012 };
1013
1014 var constrastColor = generateContrastColor(bgColor);
1015 var contrastColorHex = rgbToHex(constrastColor.r, constrastColor.g, constrastColor.b);
1016
1017 self.css('color', contrastColorHex);
1018 }
1019 }
1020 });
1021 })();
1022 });
1023 })(window, window.document, jQuery || {}, upstream || {}, TableExport);
1024