libs/ng-lib/src/lib/scully-content/scully-content.component.js
/**
     * upgrade a **href** attributes to links that respect the Angular router
     * and don't do a full page reload. Only works on links that are found in the
     * Scully route config file.
     * @param elm the element containing the **hrefs**
     */
upgrade a **href** attributes to links that respect the Angular router
and don't do a full page reload. Only works on links that are found in the
Scully route config file.
@param elm the element containing the **hrefs
upgrade a ** href ** attributes to links that respect the Angular router and don ' t do a full page reload . Only works on links that are found in the Scully route config file . @param elm the element containing the ** hrefs
upgradeToRoutelink(elm) {
        return __awaiter(this, void 0, void 0, function* () {
            if (!['A', 'BUTTON'].includes(elm.tagName)) {
                return;
            }
            const hash = elm.dataset.hash;
            if (hash) {
                elm.setAttribute('href', '#' + hash);
                elm.setAttribute('onclick', '');
                elm.onclick = (ev) => {
                    ev.preventDefault();
                    const destination = document.getElementById(hash);
                    if (destination) {
                        const url = new URL(window.location.href);
                        url.hash = hash;
                        history.replaceState('', '', url.toString());
                        destination.scrollIntoView();
                    }
                };
                return;
            }
            const routes = yield this.routes;
            const href = elm.getAttribute('href');
            const lnk = basePathOnly(href.toLowerCase());
            const route = routes.find((r) => basePathOnly(r.route.toLowerCase()) === lnk);
            if (lnk && route && !lnk.startsWith('#')) {
                elm.onclick = (ev) => __awaiter(this, void 0, void 0, function* () {
                    const splitRoute = route.route.split(`/`);
                    const curSplit = location.pathname.split('/');
                    curSplit.pop();
                    ev.preventDefault();
                    const routed = yield this.router.navigate(splitRoute).catch((e) => {
                        console.error('routing error', e);
                        return false;
                    });
                    if (!routed) {
                        return;
                    }
                    if (curSplit.every((part, i) => splitRoute[i] === part) && splitRoute.length !== curSplit.length + 1) {
                        setTimeout(() => this.replaceContent(), 10); 
                    }
                });
            }
        });
    }
upgradeToRoutelink ( elm ) { return __awaiter ( this , void 0 , void 0 , function * ( ) { if ( ! [ 'A' , 'BUTTON' ] . includes ( elm . tagName ) ) { return ; } const hash = elm . dataset . hash ; if ( hash ) { elm . setAttribute ( 'href' , '#' + hash ) ; elm . setAttribute ( 'onclick' , '' ) ; elm . onclick = ( ev ) => { ev . preventDefault ( ) ; const destination = document . getElementById ( hash ) ; if ( destination ) { const url = new URL ( window . location . href ) ; url . hash = hash ; history . replaceState ( '' , '' , url . toString ( ) ) ; destination . scrollIntoView ( ) ; } } ; return ; } const routes = yield this . routes ; const href = elm . getAttribute ( 'href' ) ; const lnk = basePathOnly ( href . toLowerCase ( ) ) ; const route = routes . find ( ( r ) => basePathOnly ( r . route . toLowerCase ( ) ) === lnk ) ; if ( lnk && route && ! lnk . startsWith ( '#' ) ) { elm . onclick = ( ev ) => __awaiter ( this , void 0 , void 0 , function * ( ) { const splitRoute = route . route . split ( ` ` ) ; const curSplit = location . pathname . split ( '/' ) ; curSplit . pop ( ) ; ev . preventDefault ( ) ; const routed = yield this . router . navigate ( splitRoute ) . catch ( ( e ) => { console . error ( 'routing error' , e ) ; return false ; } ) ; if ( ! routed ) { return ; } if ( curSplit . every ( ( part , i ) => splitRoute [ i ] === part ) && splitRoute . length !== curSplit . length + 1 ) { setTimeout ( ( ) => this . replaceContent ( ) , 10 ) ; } } ) ; } } ) ; }
upgrade a **href** attributes to links that respect the Angular router
 and don't do a full page reload.
upgrade a ** href ** attributes to links that respect the Angular router and don ' t do a full page reload .
/** only upgrade routes known by scully. */ // loose last "part" of route /** check for the same route with different "data", and NOT a 1 level higher (length), and is not a fragment of th same page */ // a small delay, so we are sure the angular parts in the page are settled enough