import $ from 'jquery'; import Finder from '../utils/finder'; import { getInitialRoute, getStore, setInitialRoute } from './index'; // import getFilters from '../utils/get-filters'; let XHRUUID = 0; const GRAV_CONFIG = typeof global.GravConfig !== 'undefined' ? global.GravConfig : global.GravAdmin.config; export const Instances = {}; const isInViewport = (elem) => { const bounding = elem.getBoundingClientRect(); const titlebar = document.querySelector('#titlebar'); const offset = titlebar ? titlebar.getBoundingClientRect().height : 0; return ( bounding.top >= offset && bounding.left >= 0 && bounding.bottom <= (window.innerHeight || document.documentElement.clientHeight) && bounding.right <= (window.innerWidth || document.documentElement.clientWidth) ); }; export class FlexPages { constructor(container, data) { this.container = $(container); this.data = data; const dataLoad = this.dataLoad; this.finder = new Finder( this.container, (parent, callback) => { return dataLoad.call(this, parent, callback); }, { labelKey: 'title', defaultPath: getInitialRoute(), itemTrigger: '[data-flexpages-expand]', createItem: function(item) { return FlexPages.createItem(this.config, item, this); }, createItemContent: function(item) { return FlexPages.createItemContent(this.config, item, this); } } ); this.finder.$emitter.on('leaf-selected', (item) => { setInitialRoute({ route: item.route.raw }); }); this.finder.$emitter.on('interior-selected', (item) => { setInitialRoute({ route: item.route.raw }); }); /* this.finder.$emitter.on('leaf-selected', (item) => { console.log('selected', item); this.finder.emit('create-column', () => this.createSimpleColumn(item)); }); this.finder.$emitter.on('item-selected', (selected) => { console.log('selected', selected); // for future use only - create column-card creation for file with details like in macOS finder // this.finder.$emitter('create-column', () => this.createSimpleColumn(selected)); }); */ this.finder.$emitter.on('column-created', () => { this.container[0].scrollLeft = this.container[0].scrollWidth - this.container[0].clientWidth; }); } static createItem(config, item, finder) { const listItem = $('
  • '); const listItemClasses = [config.className.item]; // const href = `${GRAV_CONFIG.current_url}/${item.route.raw}`.replace('//', '/'); const link = $('
    '); const createItemContent = config.createItemContent || finder.createItemContent; const fragment = createItemContent.call(this, item); link.append(fragment) // .attr('href', href) .attr('tabindex', -1); if (item.url) { link.attr('href', item.url); listItemClasses.push(item.className); } if (item[config.childKey]) { listItemClasses.push(config.className[config.childKey]); } if (item.filters_hit) { listItemClasses.push('filters-hit'); } listItem.addClass(listItemClasses.join(' ')); listItem.append(link) .attr('data-fjs-item', item[config.itemKey]); listItem[0]._item = item; return listItem; } static createItemContent(config, item) { const frag = document.createDocumentFragment(); const route = `${GRAV_CONFIG.current_url}/${item.route.raw}`.replace('//', '/'); const title = $('
    '); const link = $(``); const icon = $(``); if (item.extras && item.extras.lang) { let status = ''; if (item.extras.translated) { status = 'translated'; } if (item.extras.lang === 'n/a') { status = 'not-available'; } const lang = $(`${item.extras.lang}`); lang.appendTo(icon); } if (item.extras && item.extras && (item.extras.published_date || item.extras.unpublished_date)) { const clock = $(''); clock.appendTo(icon); } const info = $(`${item.title} ${item.route.display}`); const actions = $(''); let dotdotdot = null; if (item.extras) { const LANG_URL = $('[data-lang-url]').data('langUrl'); dotdotdot = $('
    '); dotdotdot.on('click', (event) => { if (!dotdotdot.find('.dropdown-menu').length) { let tags = ''; let langs = ''; item.extras.tags.forEach((tag) => { tags += `${tag}`; }); const translations = item.extras.langs || {}; Object.keys(translations).forEach((lang) => { const translated = translations[lang]; langs += `
    ${lang ? lang : 'default'}`; }); const canPreview = item.extras.actions.includes('preview') && (!(item.extras.tags.includes('non-routable') || item.extras.tags.includes('unpublished'))); const canEdit = item.extras.actions.includes('edit'); const canCopy = item.extras.actions.includes('copy'); const canMove = false; // item.extras.actions.includes('move'); const canDelete = item.extras.actions.includes('delete'); const ul = $(``); ul.appendTo(dotdotdot); } return true; }); } if (item.child_count) { const button = $('