IFrame.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /**
  2. * --------------------------------------------
  3. * AdminLTE IFrame.js
  4. * License MIT
  5. * --------------------------------------------
  6. */
  7. import $ from 'jquery'
  8. /**
  9. * Constants
  10. * ====================================================
  11. */
  12. const NAME = 'IFrame'
  13. const DATA_KEY = 'lte.iframe'
  14. const JQUERY_NO_CONFLICT = $.fn[NAME]
  15. const SELECTOR_DATA_TOGGLE = '[data-widget="iframe"]'
  16. const SELECTOR_CONTENT_WRAPPER = '.content-wrapper'
  17. const SELECTOR_CONTENT_IFRAME = `${SELECTOR_CONTENT_WRAPPER} iframe`
  18. const SELECTOR_TAB_NAV = `${SELECTOR_DATA_TOGGLE}.iframe-mode .nav`
  19. const SELECTOR_TAB_NAVBAR_NAV = `${SELECTOR_DATA_TOGGLE}.iframe-mode .navbar-nav`
  20. const SELECTOR_TAB_NAVBAR_NAV_ITEM = `${SELECTOR_TAB_NAVBAR_NAV} .nav-item`
  21. const SELECTOR_TAB_CONTENT = `${SELECTOR_DATA_TOGGLE}.iframe-mode .tab-content`
  22. const SELECTOR_TAB_EMPTY = `${SELECTOR_TAB_CONTENT} .tab-empty`
  23. const SELECTOR_TAB_LOADING = `${SELECTOR_TAB_CONTENT} .tab-loading`
  24. const SELECTOR_SIDEBAR_MENU_ITEM = '.main-sidebar .nav-item > a.nav-link'
  25. const CLASS_NAME_IFRAME_MODE = 'iframe-mode'
  26. const Default = {
  27. click(item) {
  28. return item
  29. },
  30. changed(item) {
  31. return item
  32. },
  33. autoIframeMode: true,
  34. autoShowNewTab: true,
  35. loadingScreen: true
  36. }
  37. /**
  38. * Class Definition
  39. * ====================================================
  40. */
  41. class IFrame {
  42. constructor(element, config) {
  43. this._config = config
  44. this._element = element
  45. this._init()
  46. }
  47. // Public
  48. click(item) {
  49. this._config.click.call(item)
  50. }
  51. changed(item) {
  52. this._config.changed.call(item)
  53. }
  54. createTab(title, link, autoOpen) {
  55. const tabId = `panel-${link.replace('.html', '').replace('./', '').replaceAll('/', '-')}-${Math.floor(Math.random() * 1000)}`
  56. const navId = `tab-${link.replace('.html', '').replace('./', '').replaceAll('/', '-')}-${Math.floor(Math.random() * 1000)}`
  57. const newNavItem = `<li class="nav-item" role="presentation"><a class="nav-link" data-toggle="row" id="${navId}" href="#${tabId}" role="tab" aria-controls="${tabId}" aria-selected="false">${title}</a></li>`
  58. $(SELECTOR_TAB_NAVBAR_NAV).append(newNavItem)
  59. const newTabItem = `<div class="tab-pane fade" id="${tabId}" role="tabpanel" aria-labelledby="${navId}"><iframe src="${link}"></iframe></div>`
  60. $(SELECTOR_TAB_CONTENT).append(newTabItem)
  61. if (autoOpen) {
  62. this.switchTab(`#${navId}`, this._config.loadingScreen)
  63. }
  64. }
  65. openTabSidebar(item) {
  66. let $item = $(item).clone()
  67. if ($item.attr('href') === undefined) {
  68. $item = $(item).parent('a').clone()
  69. }
  70. const title = $item.find('p').text()
  71. const link = $item.attr('href')
  72. if (link === '#' || link === '' || link === undefined) {
  73. return
  74. }
  75. this.createTab(title, link, this._config.autoShowNewTab)
  76. }
  77. switchTab(item, loadingScreen = null) {
  78. $(SELECTOR_TAB_EMPTY).hide()
  79. $(`${SELECTOR_TAB_NAVBAR_NAV} .active`).tab('dispose').removeClass('active')
  80. this._fixHeight()
  81. const $item = $(item)
  82. if (loadingScreen) {
  83. const $loadingScreen = $(SELECTOR_TAB_LOADING)
  84. $loadingScreen.fadeIn()
  85. const tabId = $item.attr('href')
  86. $(`${tabId} iframe`).ready(() => {
  87. if (typeof loadingScreen === 'number') {
  88. setTimeout(() => {
  89. $loadingScreen.fadeOut()
  90. }, loadingScreen)
  91. } else {
  92. $loadingScreen.fadeOut()
  93. }
  94. })
  95. }
  96. $item.tab('show')
  97. $item.parents('li').addClass('active')
  98. }
  99. // Private
  100. _init() {
  101. if (window.frameElement && this._config.autoIframeMode) {
  102. $('body').addClass(CLASS_NAME_IFRAME_MODE)
  103. } else if ($(SELECTOR_CONTENT_WRAPPER).hasClass(CLASS_NAME_IFRAME_MODE)) {
  104. this._setupListeners()
  105. this._fixHeight(true)
  106. }
  107. }
  108. _setupListeners() {
  109. $(window).on('resize', () => {
  110. setTimeout(() => {
  111. this._fixHeight()
  112. }, 1)
  113. })
  114. $(document).on('click', SELECTOR_SIDEBAR_MENU_ITEM, e => {
  115. e.preventDefault()
  116. this.openTabSidebar(e.target)
  117. })
  118. $(document).on('click', SELECTOR_TAB_NAVBAR_NAV_ITEM, e => {
  119. e.preventDefault()
  120. this.switchTab(e.target)
  121. })
  122. }
  123. _fixHeight(tabEmpty = false) {
  124. const contentWrapperHeight = parseFloat($(SELECTOR_CONTENT_WRAPPER).css('min-height'))
  125. const navbarHeight = $(SELECTOR_TAB_NAV).outerHeight()
  126. if (tabEmpty == true) {
  127. setTimeout(() => {
  128. $(`${SELECTOR_TAB_EMPTY}, ${SELECTOR_TAB_LOADING}`).height(contentWrapperHeight - navbarHeight)
  129. }, 50)
  130. } else {
  131. $(SELECTOR_CONTENT_IFRAME).height(contentWrapperHeight - navbarHeight)
  132. }
  133. }
  134. // Static
  135. static _jQueryInterface(operation) {
  136. let data = $(this).data(DATA_KEY)
  137. const _options = $.extend({}, Default, $(this).data())
  138. if (!data) {
  139. data = new IFrame(this, _options)
  140. $(this).data(DATA_KEY, data)
  141. }
  142. if (typeof operation === 'string' && operation.match(/openTabSidebar/)) {
  143. data[operation]()
  144. }
  145. }
  146. }
  147. /**
  148. * Data API
  149. * ====================================================
  150. */
  151. $(window).on('load', () => {
  152. IFrame._jQueryInterface.call($(SELECTOR_DATA_TOGGLE))
  153. })
  154. /**
  155. * jQuery API
  156. * ====================================================
  157. */
  158. $.fn[NAME] = IFrame._jQueryInterface
  159. $.fn[NAME].Constructor = IFrame
  160. $.fn[NAME].noConflict = function () {
  161. $.fn[NAME] = JQUERY_NO_CONFLICT
  162. return IFrame._jQueryInterface
  163. }
  164. export default IFrame