hardwood.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /**
  2. * Make elastic search tables display two rows of the last column
  3. * and allow for a show more button.
  4. */
  5. (function ($) {
  6. $(function () {
  7. var window_height = $(window).height() || 0,
  8. header_height = $('#navigation').outerHeight() || 0,
  9. footer_height = ($('#footer').outerHeight() || 0) + ($('.main-footer').outerHeight() || 0);
  10. $('#main').css('min-height', window_height - (header_height + footer_height) - 30);
  11. $('#elasticsearch_hits_table tr td:nth-of-type(2)').each(function () {
  12. var text = $(this).html();
  13. var array = text.split('<br>');
  14. if (array.length > 2) {
  15. var div = $('<div />', {});
  16. div.append(array.shift() + '<br>' + array.shift());
  17. var hidden = $('<div />', {'class': 'hidden-hit'}).css('display', 'none');
  18. hidden.append(array.join('<br>'));
  19. div.append(hidden);
  20. div.append('<br>');
  21. var btn = $('<button />', {
  22. 'type': 'button',
  23. 'class': 'btn btn-secondary btn-sm'
  24. }).html('Show More')
  25. .click(function (e) {
  26. e.preventDefault();
  27. var hidden_hit = hidden;
  28. if (hidden_hit.hasClass('is_open')) {
  29. hidden_hit.removeClass('is_open');
  30. hidden_hit.slideUp();
  31. btn.html('Show More');
  32. }
  33. else {
  34. hidden_hit.slideDown();
  35. hidden_hit.addClass('is_open');
  36. btn.html('Show Less');
  37. }
  38. });
  39. div.append(btn);
  40. $(this).html(div);
  41. }
  42. });
  43. // Add table responsive to divs that contain tables as a direct child
  44. $('div').has('table').last().addClass('table-responsive');
  45. $('#block-tripal-elasticsearch-website-search-category .es-category-item').each(function () {
  46. $(this).css('position', 'relative');
  47. var text = $(this).text().split(' ');
  48. var num = text.pop();
  49. text = text.join(' ').split('_').join(' ');
  50. var span = $('<span />', {'class': 'float-right'}).text(num.replace('(', '').replace(')', ''));
  51. span.css({
  52. position: 'absolute',
  53. right: 0,
  54. top: 0,
  55. backgroundColor: '#fff'
  56. });
  57. if ($(this).hasClass('active')) {
  58. $(this).css('color', '#000');
  59. span.html('<i class="fa fa-check"></i>');
  60. }
  61. else {
  62. span.css({
  63. backgroundColor: '#e6e6e6',
  64. borderRadius: 10,
  65. paddingTop: 2,
  66. paddingBottom: 2,
  67. paddingLeft: 7,
  68. paddingRight: 7,
  69. fontSize: 12,
  70. color: '#999'
  71. });
  72. }
  73. $(this).html(text);
  74. $(this).append(span);
  75. });
  76. $('.es-search-form-in-title .btn, .es-search-form-in-home .btn, .elasticsearch-search-input .btn').each(function () {
  77. var attributes = {};
  78. for (var i = 0; i < this.attributes.length; i++) {
  79. attributes[this.attributes[i].nodeName] = this.attributes[i].nodeValue;
  80. }
  81. var searchButton = $('<button />', attributes).html('<i class="fa fa-search"></i>');
  82. $(this).replaceWith(searchButton);
  83. });
  84. $('.elasticsearch-search-input .form-control').attr('style', '');
  85. $(document).on('elasticsearch.completed', function (event) {
  86. $('.elastic-result-block-footer a').attr('class', 'btn btn-primary');
  87. });
  88. $('.tripal_pane').not('.hideTripalPane').each(function () {
  89. var c = $(this).attr('class');
  90. var field = c.match(/^tripal_pane-fieldset-(.*$)/) || [];
  91. if (field.length === 2) {
  92. $('#' + field[1].split(' ')[0]).addClass('active');
  93. }
  94. });
  95. $(document).on('tripal_ds_pane_expanded', function () {
  96. if (typeof Event !== 'undefined') {
  97. window.dispatchEvent(new Event('resize'));
  98. }
  99. else {
  100. // Support IE
  101. var event = window.document.createEvent('UIEvents');
  102. event.initUIEvent('resize', true, false, window, 0);
  103. window.dispatchEvent(event);
  104. }
  105. });
  106. });
  107. /**
  108. * Allow dropdown menus to open by hover on bigger devices.
  109. */
  110. $(function () {
  111. $('.navbar .last > .dropdown-menu').addClass('dropdown-menu-right');
  112. $('.navbar .dropdown > .nav-link').click(function (e) {
  113. if ($(window).width() > 992) {
  114. var href = $(this).attr('href');
  115. if (href !== '#') {
  116. return window.location.href = href;
  117. }
  118. }
  119. });
  120. $('.navbar .dropdown').hover(function () {
  121. if ($(window).width() > 992) {
  122. $(this).find('.dropdown-menu').fadeIn(100);
  123. }
  124. }, function () {
  125. if ($(window).width() > 992) {
  126. $(this).find('.dropdown-menu').fadeOut(100);
  127. }
  128. });
  129. });
  130. /**
  131. * Re-implement Tripal DS
  132. */
  133. $(function () {
  134. function TripalDS() {
  135. this.init();
  136. this.openDefaultPane();
  137. }
  138. /**
  139. * Initiate TripalDS.
  140. */
  141. TripalDS.prototype.init = function () {
  142. var links = $('.tripal_pane-toc-list-item-link');
  143. var _that = this;
  144. links.each(function () {
  145. var id = '.tripal_pane-fieldset-' + $(this).attr('id');
  146. if ($(id).length === 0) {
  147. $(this).parents('.views-row').first().remove();
  148. return;
  149. }
  150. if ($(id).children().not('.field-group-format-title').text().trim().length === 0) {
  151. $(this).parents('.views-row').first().remove();
  152. }
  153. });
  154. links.unbind('click');
  155. links.on('click', function (e) {
  156. e.preventDefault();
  157. // Add active class to the element that was just clicked
  158. $(this).parents('.ds-left')
  159. .find('.tripal_pane-toc-list-item-link.active')
  160. .removeClass('active');
  161. $(this).addClass('active');
  162. var pane = $('.tripal_pane-fieldset-' + $(this).attr('id'));
  163. if (pane.is('.hideTripalPane')) {
  164. $('.tripal_pane').not('.hideTripalPane').addClass('hideTripalPane');
  165. pane.removeClass('hideTripalPane');
  166. var event = $.Event('tripal_ds_pane_expanded');
  167. $(this).trigger(event);
  168. var id = $(this).attr('id');
  169. _that.pushToHistory(id);
  170. $('input[type="hidden"][name="tripal_pane"]').attr('value', id);
  171. }
  172. });
  173. };
  174. /**
  175. * Show the pane that's selected in the url.
  176. */
  177. TripalDS.prototype.openDefaultPane = function () {
  178. var params = this.getURLParameters().filter(function (param) {
  179. return param.name.toLowerCase() === 'tripal_pane';
  180. });
  181. var pane_id = params.length > 0 ? params[0].value : null;
  182. if (!pane_id) {
  183. // We are done here
  184. return;
  185. }
  186. // Find the wanted pane and its link
  187. var $pane_link = $('#' + pane_id);
  188. if (!$pane_link || $pane_link.length === 0) {
  189. if (window.console) {
  190. console.warn('Given pane id does not exist: ' + pane_id + '. Please check the url for more info.');
  191. }
  192. return;
  193. }
  194. // Disable the active link
  195. $('.ds-left')
  196. .find('.tripal_pane-toc-list-item-link.active')
  197. .removeClass('active');
  198. // Hide the active pane
  199. $('.ds-right .showTripalPane').removeClass('showTripalPane').addClass('hideTripalPane');
  200. // Click the link!
  201. $pane_link.trigger('click');
  202. };
  203. /**
  204. * Get the URL params
  205. *
  206. * @return {Array}
  207. */
  208. TripalDS.prototype.getURLParameters = function () {
  209. // Check browser href for a pane to open
  210. var params = window.location.search;
  211. if (params.length > 0) {
  212. // remove the "?" char
  213. var qIndex = params.indexOf('?');
  214. if (qIndex !== -1) {
  215. params = params.slice(qIndex + 1, params.length);
  216. }
  217. // split it into array
  218. return params.split('&').map(function (param) {
  219. var broken = param.split('=');
  220. if (broken.length === 2) {
  221. return {
  222. name: broken[0],
  223. value: broken[1]
  224. };
  225. }
  226. else if (broken.length === 1) {
  227. return {
  228. name: broken[0],
  229. value: null
  230. };
  231. }
  232. else {
  233. return null;
  234. }
  235. });
  236. }
  237. return [];
  238. };
  239. /**
  240. * Push the new state to history
  241. *
  242. * @param {String} id The id of the pane's link
  243. */
  244. TripalDS.prototype.pushToHistory = function (id) {
  245. var params = this.getURLParameters();
  246. var added = false;
  247. for (var i = 0; i < params.length; i++) {
  248. if (params[i] && params[i].name === 'tripal_pane') {
  249. params[i].value = id;
  250. added = true;
  251. }
  252. }
  253. if (!added) {
  254. params.push({
  255. name: 'tripal_pane',
  256. value: id
  257. });
  258. }
  259. var query = [];
  260. for (i = 0; i < params.length; i++) {
  261. var param = params[i];
  262. query.push(param.name + '=' + param.value);
  263. }
  264. query = query.join('&');
  265. if (window.history) {
  266. var state = window.history.state;
  267. var title = document.title;
  268. var path = window.location.pathname;
  269. window.history.replaceState(state, title, path + '?' + query);
  270. }
  271. };
  272. return new TripalDS();
  273. });
  274. $(function () {
  275. $('.help-button-trigger').click(function (e) {
  276. e.preventDefault();
  277. var parent = $(this).parents('.help-button-block').first();
  278. var content = parent.find('.help-button-content');
  279. if (parent.hasClass('open')) {
  280. content.slideUp(function () {
  281. parent.removeClass('open');
  282. });
  283. return;
  284. }
  285. parent.addClass('open');
  286. content.slideDown();
  287. });
  288. });
  289. $(function () {
  290. let opened = init();
  291. if (!opened) {
  292. $('#open-survey-modal').fadeIn();
  293. }
  294. function init() {
  295. var ds = window.localStorage;
  296. // ds.removeItem('hardwoods.survey');
  297. if (!ds) {
  298. return false;
  299. }
  300. $('#open-survey-modal').click(function () {
  301. $('#survey-modal').modal('show');
  302. $(this).fadeOut();
  303. });
  304. $('#give-feedback-btn').click(function () {
  305. $('#survey-modal').modal('hide');
  306. });
  307. $('#survey-modal').on('hide.bs.modal', function () {
  308. ds.setItem('hardwoods.survey', (new Date()).valueOf());
  309. $('#open-survey-modal').fadeIn();
  310. });
  311. var item = ds.getItem('hardwoods.survey');
  312. // 30 days
  313. var seconds = 1000 * 60 * 60 * 60 * 24 * 30;
  314. if (item) {
  315. item = parseInt(item);
  316. if ((new Date()).valueOf() > (item + seconds)) {
  317. ds.removeItem('hardwoods.survey');
  318. }
  319. else {
  320. return false;
  321. }
  322. }
  323. setTimeout(function () {
  324. $('#survey-modal').modal('show');
  325. }, 1000);
  326. return true;
  327. }
  328. // function fix_styling($item) {
  329. // if ($item.children().length > 0) {
  330. // $item.children().each(function () {
  331. // fix_styling($(this));
  332. // });
  333. // }
  334. // else {
  335. // $item.html($item.html().replace(/_/g, '_<wbr>'));
  336. // }
  337. // }
  338. //
  339. // $('td, th').each(function () {
  340. // var $this = $(this);
  341. // if ($this.text().length > 40) {
  342. // $this.children().each(function () {
  343. // fix_styling($(this));
  344. // });
  345. // }
  346. // });
  347. });
  348. })(jQuery);