2020-09-16 13:32:22 +00:00
|
|
|
/**
|
|
|
|
* --------------------------------------------
|
|
|
|
* AdminLTE IFrame.js
|
|
|
|
* License MIT
|
|
|
|
* --------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
import $ from 'jquery'
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constants
|
|
|
|
* ====================================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
const NAME = 'IFrame'
|
|
|
|
const DATA_KEY = 'lte.iframe'
|
|
|
|
const JQUERY_NO_CONFLICT = $.fn[NAME]
|
|
|
|
|
|
|
|
const SELECTOR_DATA_TOGGLE = '[data-widget="iframe"]'
|
|
|
|
const SELECTOR_CONTENT_WRAPPER = '.content-wrapper'
|
|
|
|
const SELECTOR_CONTENT_IFRAME = `${SELECTOR_CONTENT_WRAPPER} iframe`
|
2020-09-19 12:16:39 +00:00
|
|
|
const SELECTOR_TAB_NAV = `${SELECTOR_DATA_TOGGLE}.iframe-mode .nav`
|
2020-09-16 13:32:22 +00:00
|
|
|
const SELECTOR_TAB_NAVBAR_NAV = `${SELECTOR_DATA_TOGGLE}.iframe-mode .navbar-nav`
|
2020-09-18 10:54:44 +00:00
|
|
|
const SELECTOR_TAB_NAVBAR_NAV_ITEM = `${SELECTOR_TAB_NAVBAR_NAV} .nav-item`
|
2020-09-16 13:32:22 +00:00
|
|
|
const SELECTOR_TAB_CONTENT = `${SELECTOR_DATA_TOGGLE}.iframe-mode .tab-content`
|
2020-09-18 10:54:44 +00:00
|
|
|
const SELECTOR_TAB_EMPTY = `${SELECTOR_TAB_CONTENT} .tab-empty`
|
2020-09-19 12:51:15 +00:00
|
|
|
const SELECTOR_TAB_LOADING = `${SELECTOR_TAB_CONTENT} .tab-loading`
|
2020-09-18 10:54:44 +00:00
|
|
|
const SELECTOR_SIDEBAR_MENU_ITEM = '.main-sidebar .nav-item > a.nav-link'
|
2020-09-20 08:55:39 +00:00
|
|
|
const SELECTOR_HEADER_MENU_ITEM = '.main-header .nav-item a.nav-link'
|
|
|
|
const SELECTOR_HEADER_DROPDOWN_ITEM = '.main-header a.dropdown-item'
|
2020-09-16 13:32:22 +00:00
|
|
|
const CLASS_NAME_IFRAME_MODE = 'iframe-mode'
|
|
|
|
|
|
|
|
const Default = {
|
2020-09-20 08:07:34 +00:00
|
|
|
tabClick(item) {
|
2020-09-16 13:32:22 +00:00
|
|
|
return item
|
|
|
|
},
|
2020-09-20 08:07:34 +00:00
|
|
|
tabChanged(item) {
|
|
|
|
return item
|
|
|
|
},
|
|
|
|
tabCreated(item) {
|
2020-09-16 13:32:22 +00:00
|
|
|
return item
|
2020-09-19 12:00:55 +00:00
|
|
|
},
|
|
|
|
autoIframeMode: true,
|
2020-09-20 08:55:39 +00:00
|
|
|
autoItemActive: true,
|
2020-09-19 12:51:15 +00:00
|
|
|
autoShowNewTab: true,
|
2020-09-20 07:59:23 +00:00
|
|
|
loadingScreen: true,
|
|
|
|
useNavbarItems: true
|
2020-09-16 13:32:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class Definition
|
|
|
|
* ====================================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
class IFrame {
|
|
|
|
constructor(element, config) {
|
|
|
|
this._config = config
|
|
|
|
this._element = element
|
|
|
|
|
|
|
|
this._init()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Public
|
|
|
|
|
2020-09-20 08:07:34 +00:00
|
|
|
tabClick(item) {
|
|
|
|
this._config.tabClick(item)
|
2020-09-16 13:32:22 +00:00
|
|
|
}
|
|
|
|
|
2020-09-20 08:07:34 +00:00
|
|
|
tabChanged(item) {
|
|
|
|
this._config.tabChanged(item)
|
|
|
|
}
|
|
|
|
|
|
|
|
tabCreated(item) {
|
|
|
|
this._config.tabCreated(item)
|
2020-09-16 13:32:22 +00:00
|
|
|
}
|
|
|
|
|
2020-09-19 12:00:55 +00:00
|
|
|
createTab(title, link, autoOpen) {
|
|
|
|
const tabId = `panel-${link.replace('.html', '').replace('./', '').replaceAll('/', '-')}-${Math.floor(Math.random() * 1000)}`
|
|
|
|
const navId = `tab-${link.replace('.html', '').replace('./', '').replaceAll('/', '-')}-${Math.floor(Math.random() * 1000)}`
|
2020-09-16 13:32:22 +00:00
|
|
|
|
2020-09-18 10:54:44 +00:00
|
|
|
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>`
|
2020-09-16 13:32:22 +00:00
|
|
|
$(SELECTOR_TAB_NAVBAR_NAV).append(newNavItem)
|
|
|
|
|
|
|
|
const newTabItem = `<div class="tab-pane fade" id="${tabId}" role="tabpanel" aria-labelledby="${navId}"><iframe src="${link}"></iframe></div>`
|
|
|
|
$(SELECTOR_TAB_CONTENT).append(newTabItem)
|
2020-09-19 12:00:55 +00:00
|
|
|
|
|
|
|
if (autoOpen) {
|
2020-09-19 12:51:15 +00:00
|
|
|
this.switchTab(`#${navId}`, this._config.loadingScreen)
|
2020-09-19 12:00:55 +00:00
|
|
|
}
|
2020-09-20 08:07:34 +00:00
|
|
|
|
|
|
|
this.tabCreated($(`#${navId}`))
|
2020-09-16 13:32:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
openTabSidebar(item) {
|
2020-09-19 12:00:55 +00:00
|
|
|
let $item = $(item).clone()
|
|
|
|
if ($item.attr('href') === undefined) {
|
|
|
|
$item = $(item).parent('a').clone()
|
|
|
|
}
|
2020-09-16 13:32:22 +00:00
|
|
|
|
2020-09-20 07:59:23 +00:00
|
|
|
let title = $item.find('p').text()
|
|
|
|
if (title === '') {
|
|
|
|
title = $item.text()
|
|
|
|
}
|
2020-09-18 10:54:44 +00:00
|
|
|
|
2020-09-20 07:59:23 +00:00
|
|
|
const link = $item.attr('href')
|
2020-09-19 12:00:55 +00:00
|
|
|
if (link === '#' || link === '' || link === undefined) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
this.createTab(title, link, this._config.autoShowNewTab)
|
|
|
|
}
|
|
|
|
|
2020-09-19 12:51:15 +00:00
|
|
|
switchTab(item, loadingScreen = null) {
|
2020-09-19 12:00:55 +00:00
|
|
|
$(SELECTOR_TAB_EMPTY).hide()
|
2020-09-19 12:51:15 +00:00
|
|
|
$(`${SELECTOR_TAB_NAVBAR_NAV} .active`).tab('dispose').removeClass('active')
|
2020-09-19 12:00:55 +00:00
|
|
|
this._fixHeight()
|
2020-09-19 12:51:15 +00:00
|
|
|
const $item = $(item)
|
2020-09-20 08:55:39 +00:00
|
|
|
const tabId = $item.attr('href')
|
2020-09-19 12:51:15 +00:00
|
|
|
|
|
|
|
if (loadingScreen) {
|
|
|
|
const $loadingScreen = $(SELECTOR_TAB_LOADING)
|
|
|
|
$loadingScreen.fadeIn()
|
|
|
|
$(`${tabId} iframe`).ready(() => {
|
|
|
|
if (typeof loadingScreen === 'number') {
|
|
|
|
setTimeout(() => {
|
|
|
|
$loadingScreen.fadeOut()
|
|
|
|
}, loadingScreen)
|
|
|
|
} else {
|
|
|
|
$loadingScreen.fadeOut()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
$item.tab('show')
|
|
|
|
$item.parents('li').addClass('active')
|
2020-09-20 08:07:34 +00:00
|
|
|
this.tabChanged($item)
|
2020-09-20 08:55:39 +00:00
|
|
|
|
|
|
|
if (this._config.autoItemActive) {
|
|
|
|
this.setItemActive($(`${tabId} iframe`).attr('src'))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setItemActive(href) {
|
|
|
|
$(`${SELECTOR_SIDEBAR_MENU_ITEM}, ${SELECTOR_HEADER_DROPDOWN_ITEM}`).removeClass('active')
|
|
|
|
$(SELECTOR_HEADER_MENU_ITEM).parent().removeClass('active')
|
|
|
|
|
|
|
|
const $headerMenuItem = $(`${SELECTOR_HEADER_MENU_ITEM}[href$="${href}"]`)
|
|
|
|
const $headerDropdownItem = $(`${SELECTOR_HEADER_DROPDOWN_ITEM}[href$="${href}"]`)
|
|
|
|
const $sidebarMenuItem = $(`${SELECTOR_SIDEBAR_MENU_ITEM}[href$="${href}"]`)
|
|
|
|
|
|
|
|
$headerMenuItem.each((i, e) => {
|
|
|
|
$(e).parent().addClass('active')
|
|
|
|
})
|
|
|
|
$headerDropdownItem.each((i, e) => {
|
|
|
|
$(e).addClass('active')
|
|
|
|
})
|
|
|
|
$sidebarMenuItem.each((i, e) => {
|
|
|
|
$(e).addClass('active')
|
|
|
|
$(e).parents('.nav-treeview').prevAll('.nav-link').addClass('active')
|
|
|
|
})
|
2020-09-16 13:32:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Private
|
|
|
|
|
|
|
|
_init() {
|
2020-09-19 12:00:55 +00:00
|
|
|
if (window.frameElement && this._config.autoIframeMode) {
|
|
|
|
$('body').addClass(CLASS_NAME_IFRAME_MODE)
|
|
|
|
} else if ($(SELECTOR_CONTENT_WRAPPER).hasClass(CLASS_NAME_IFRAME_MODE)) {
|
2020-09-16 13:32:22 +00:00
|
|
|
this._setupListeners()
|
2020-09-18 10:54:44 +00:00
|
|
|
this._fixHeight(true)
|
2020-09-16 13:32:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_setupListeners() {
|
2020-09-18 10:54:44 +00:00
|
|
|
$(window).on('resize', () => {
|
|
|
|
setTimeout(() => {
|
|
|
|
this._fixHeight()
|
|
|
|
}, 1)
|
|
|
|
})
|
2020-09-20 08:55:39 +00:00
|
|
|
$(document).on('click', `${SELECTOR_SIDEBAR_MENU_ITEM}, ${SELECTOR_HEADER_MENU_ITEM}, ${SELECTOR_HEADER_DROPDOWN_ITEM}`, e => {
|
2020-09-20 07:59:23 +00:00
|
|
|
e.preventDefault()
|
2020-09-20 08:07:34 +00:00
|
|
|
this.tabClick(e.target)
|
2020-09-20 07:59:23 +00:00
|
|
|
this.openTabSidebar(e.target)
|
|
|
|
})
|
2020-09-18 10:54:44 +00:00
|
|
|
$(document).on('click', SELECTOR_TAB_NAVBAR_NAV_ITEM, e => {
|
|
|
|
e.preventDefault()
|
2020-09-20 08:07:34 +00:00
|
|
|
this.tabClick(e.target)
|
2020-09-19 12:00:55 +00:00
|
|
|
this.switchTab(e.target)
|
2020-09-18 10:54:44 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
_fixHeight(tabEmpty = false) {
|
|
|
|
const contentWrapperHeight = parseFloat($(SELECTOR_CONTENT_WRAPPER).css('min-height'))
|
2020-09-19 12:16:39 +00:00
|
|
|
const navbarHeight = $(SELECTOR_TAB_NAV).outerHeight()
|
2020-09-18 10:54:44 +00:00
|
|
|
if (tabEmpty == true) {
|
|
|
|
setTimeout(() => {
|
2020-09-19 12:51:15 +00:00
|
|
|
$(`${SELECTOR_TAB_EMPTY}, ${SELECTOR_TAB_LOADING}`).height(contentWrapperHeight - navbarHeight)
|
2020-09-18 10:54:44 +00:00
|
|
|
}, 50)
|
|
|
|
} else {
|
2020-09-19 12:16:39 +00:00
|
|
|
$(SELECTOR_CONTENT_IFRAME).height(contentWrapperHeight - navbarHeight)
|
2020-09-18 10:54:44 +00:00
|
|
|
}
|
2020-09-16 13:32:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Static
|
|
|
|
|
|
|
|
static _jQueryInterface(operation) {
|
|
|
|
let data = $(this).data(DATA_KEY)
|
|
|
|
const _options = $.extend({}, Default, $(this).data())
|
|
|
|
|
|
|
|
if (!data) {
|
|
|
|
data = new IFrame(this, _options)
|
|
|
|
$(this).data(DATA_KEY, data)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof operation === 'string' && operation.match(/openTabSidebar/)) {
|
|
|
|
data[operation]()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Data API
|
|
|
|
* ====================================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
$(window).on('load', () => {
|
2020-09-19 12:00:55 +00:00
|
|
|
IFrame._jQueryInterface.call($(SELECTOR_DATA_TOGGLE))
|
2020-09-16 13:32:22 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
/**
|
|
|
|
* jQuery API
|
|
|
|
* ====================================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
$.fn[NAME] = IFrame._jQueryInterface
|
|
|
|
$.fn[NAME].Constructor = IFrame
|
|
|
|
$.fn[NAME].noConflict = function () {
|
|
|
|
$.fn[NAME] = JQUERY_NO_CONFLICT
|
|
|
|
return IFrame._jQueryInterface
|
|
|
|
}
|
|
|
|
|
|
|
|
export default IFrame
|