/ckan/public/base/javascript/main.js

https://gitlab.com/iislod/ckan · JavaScript · 112 lines · 61 code · 15 blank · 36 comment · 9 complexity · c6ebf2b48eaaaa0d43174b31f89d3821 MD5 · raw file

  1. // Global ckan namespace
  2. this.ckan = this.ckan || {};
  3. (function (ckan, jQuery) {
  4. ckan.PRODUCTION = 'production';
  5. ckan.DEVELOPMENT = 'development';
  6. ckan.TESTING = 'testing';
  7. /* Initialises the CKAN JavaScript setting up environment variables and
  8. * loading localisations etc. Should be called once the page is ready.
  9. *
  10. * Examples
  11. *
  12. * jQuery(function () {
  13. * ckan.initialize();
  14. * });
  15. *
  16. * Returns nothing.
  17. */
  18. ckan.initialize = function () {
  19. var body = jQuery('body');
  20. var locale = jQuery('html').attr('lang');
  21. var browserLocale = window.navigator.userLanguage || window.navigator.language;
  22. var location = window.location;
  23. var root = location.protocol + '//' + location.host;
  24. function getRootFromData(key) {
  25. return (body.data(key) || root).replace(/\/$/, '');
  26. }
  27. ckan.SITE_ROOT = getRootFromData('siteRoot');
  28. ckan.LOCALE_ROOT = getRootFromData('localeRoot');
  29. // Convert all datetimes to the users timezone
  30. jQuery('.automatic-local-datetime').each(function() {
  31. moment.locale(browserLocale);
  32. var date = moment(jQuery(this).data('datetime'));
  33. if (date.isValid()) {
  34. jQuery(this).html(date.format("LL, LT ([UTC]Z)"));
  35. }
  36. jQuery(this).show();
  37. })
  38. // Load the localisations before instantiating the modules.
  39. ckan.sandbox().client.getLocaleData(locale).done(function (data) {
  40. ckan.i18n.load(data);
  41. ckan.module.initialize();
  42. });
  43. if (jQuery.fn.popover !== undefined) {
  44. jQuery('[data-target="popover"]').popover();
  45. }
  46. };
  47. /* Returns a full url for the current site with the provided path appended.
  48. *
  49. * path - A path to append to the url (default: '/')
  50. * includeLocale - If true the current locale will be added to the page.
  51. *
  52. * Examples
  53. *
  54. * var imageUrl = sandbox.url('/my-image.png');
  55. * // => http://example.ckan.org/my-image.png
  56. *
  57. * var imageUrl = sandbox.url('/my-image.png', true);
  58. * // => http://example.ckan.org/en/my-image.png
  59. *
  60. * var localeUrl = sandbox.url(true);
  61. * // => http://example.ckan.org/en
  62. *
  63. * Returns a url string.
  64. */
  65. ckan.url = function (path, includeLocale) {
  66. if (typeof path === 'boolean') {
  67. includeLocale = path;
  68. path = null;
  69. }
  70. path = (path || '').replace(/^\//, '');
  71. var root = includeLocale ? ckan.LOCALE_ROOT : ckan.SITE_ROOT;
  72. return path ? root + '/' + path : root;
  73. };
  74. ckan.sandbox.extend({url: ckan.url});
  75. if (ckan.ENV !== ckan.TESTING) {
  76. jQuery(function () {
  77. ckan.initialize();
  78. });
  79. }
  80. })(this.ckan, this.jQuery);
  81. // Forces this to redraw in Internet Explorer 7
  82. // This is useful for when IE7 doesn't properly render parts of the page after
  83. // some dom manipulation has happened
  84. this.jQuery.fn.ie7redraw = function() {
  85. if (jQuery('html').hasClass('ie7')) {
  86. jQuery(this).css('zoom', 1);
  87. }
  88. };
  89. // Show / hide filters for mobile
  90. $(function() {
  91. $(".show-filters").click(function() {
  92. $("body").addClass("filters-modal");
  93. });
  94. $(".hide-filters").click(function() {
  95. $("body").removeClass("filters-modal");
  96. });
  97. });