/** * Page * * This object is responsible for * * - retireving current page's properties, like type, title, * - related posts, * - giving the correct way to retrieve data in the markup for each type * * @param {string} dataSelector selector of the element which contains data, default [data-container] * @param {object} props { * @property {string} type - the type of container * @property {string} container - the main container selector (CSS-like notation eg. '.container') * @property {boolean} infinite - true if new pages have to be automatically loaded and appended to this page * @property {number} maxLoads - max number of automatic "infinite" loadings at the end of the page * } */ export class Page { constructor( dataSelector = '[data-container]' ) { try{ this.props = JSON.parse( document.querySelector( dataSelector ).dataset.container ); /// Add current page in the first position this.props.contents.unshift({ title: document.title, url: window.location.href // url: window.location.protocol + '//' + window.location.hostname + ((window.location.port) ? ':'+window.location.port :'') + window.location.pathname }); } catch(e) { /// if current page has no dataSelector /// props are set as follows this.props = { error : `${e.message} or selector ${dataSelector} not found`, type : 'single', title : '', paged : false, contents : [{ title: document.title , url : window.location.href }], infinite : false, // [!] It's the content wrapper, it wraps each content loaded (and the initial content as well) ajaxSelector : '', appendToRequest : '?is_ajax_request' } } } }