common.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Copyright (C) 2011 - present Instructure, Inc.
  3. *
  4. * This file is part of Canvas.
  5. *
  6. * Canvas is free software: you can redistribute it and/or modify it under
  7. * the terms of the GNU Affero General Public License as published by the Free
  8. * Software Foundation, version 3 of the License.
  9. *
  10. * Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
  11. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. * A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  13. * details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License along
  16. * with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. // true modules that we use in this file
  19. import $ from 'jquery'
  20. import _ from 'underscore'
  21. import I18n from 'i18n!common'
  22. import Backbone from 'Backbone'
  23. import updateSubnavMenuToggle from 'jsx/subnav_menu/updateSubnavMenuToggle'
  24. import splitAssetString from 'compiled/str/splitAssetString'
  25. import {isMathMLOnPage, loadMathJax} from 'mathml'
  26. // modules that do their own thing on every page that simply need to be required
  27. import 'translations/_core_en'
  28. import 'jquery.ajaxJSON'
  29. import 'jquery.google-analytics'
  30. import 'reminders'
  31. import 'jquery.instructure_forms'
  32. import 'instructure'
  33. import 'ajax_errors'
  34. import 'page_views'
  35. import 'compiled/behaviors/authenticity_token'
  36. import 'compiled/behaviors/ujsLinks'
  37. import 'compiled/behaviors/admin-links'
  38. import 'compiled/behaviors/activate'
  39. import 'compiled/behaviors/elementToggler'
  40. import 'compiled/behaviors/tooltip'
  41. import 'compiled/behaviors/ic-super-toggle'
  42. import 'compiled/behaviors/instructure_inline_media_comment'
  43. import 'compiled/behaviors/ping'
  44. import 'compiled/behaviors/broken-images'
  45. import 'LtiThumbnailLauncher'
  46. import 'compiled/badge_counts'
  47. // Other stuff several bundles use.
  48. // If any of these really arn't used on most pages,
  49. // we should remove them from this list, since this
  50. // loads them on every page
  51. import 'media_comments'
  52. import 'jqueryui/effects/drop'
  53. import 'jqueryui/progressbar'
  54. import 'jqueryui/tabs'
  55. import 'compiled/registration/incompleteRegistrationWarning'
  56. import 'moment'
  57. $('html').removeClass('scripts-not-loaded')
  58. $('.help_dialog_trigger').click((event) => {
  59. event.preventDefault()
  60. require.ensure([], (require) => {
  61. const helpDialog = require('compiled/helpDialog')
  62. helpDialog.open()
  63. }, 'helpDialogAsyncChunk')
  64. })
  65. $('#skip_navigation_link').on('click', function (event) {
  66. // preventDefault so we dont change the hash
  67. // this will make nested apps that use the hash happy
  68. event.preventDefault()
  69. $($(this).attr('href')).attr('tabindex', -1).focus()
  70. })
  71. // show and hide the courses vertical menu when the user clicks the hamburger button
  72. // This was in the courses bundle, but it sometimes needs to work in places that don't
  73. // load that bundle.
  74. const WIDE_BREAKPOINT = 1200
  75. function resetMenuItemTabIndexes () {
  76. // in testing this, it seems that $(document).width() returns 15px less than what it should.
  77. const tabIndex = (
  78. $('body').hasClass('course-menu-expanded') ||
  79. $(document).width() >= WIDE_BREAKPOINT - 15
  80. ) ? 0 : -1
  81. $('#section-tabs li a').attr('tabIndex', tabIndex)
  82. }
  83. $(resetMenuItemTabIndexes)
  84. $(window).on('resize', _.debounce(resetMenuItemTabIndexes, 50))
  85. $('body').on('click', '#courseMenuToggle', () => {
  86. $('body').toggleClass('course-menu-expanded')
  87. updateSubnavMenuToggle()
  88. $('#left-side').css({
  89. display: $('body').hasClass('course-menu-expanded') ? 'block' : 'none'
  90. })
  91. resetMenuItemTabIndexes()
  92. })
  93. // Backbone routes
  94. $('body').on('click', '[data-pushstate]', function (event) {
  95. event.preventDefault()
  96. Backbone.history.navigate($(this).attr('href'), true)
  97. })
  98. if (
  99. window.ENV.NEW_USER_TUTORIALS &&
  100. window.ENV.NEW_USER_TUTORIALS.is_enabled &&
  101. (window.ENV.context_asset_string && (splitAssetString(window.ENV.context_asset_string)[0] === 'courses'))
  102. ) {
  103. require.ensure([], (require) => {
  104. const initializeNewUserTutorials = require('jsx/new_user_tutorial/initializeNewUserTutorials')
  105. initializeNewUserTutorials()
  106. }, 'NewUserTutorialsAsyncChunk')
  107. }
  108. // edge < 15 does not support css vars
  109. // edge >= 15 claims to, but is currently broken
  110. const edge = window.navigator.userAgent.indexOf("Edge") > -1
  111. const supportsCSSVars = !edge && window.CSS && window.CSS.supports && window.CSS.supports('(--foo: red)')
  112. if (!supportsCSSVars) {
  113. require.ensure([], (require) => {
  114. window.canvasCssVariablesPolyfill = require('jsx/canvasCssVariablesPolyfill')
  115. }, 'canvasCssVariablesPolyfill')
  116. }
  117. $(() => {
  118. if (isMathMLOnPage()) loadMathJax('MML_HTMLorMML.js')
  119. })