dist/bbn.js
/**
     * Follows a link and if needed by sending the corresponding Ajax request and executing bbn.fn.defaultPreLinkFunction.
     * 
     * Once bbn has been initiated this function will be triggered every time a link is clicked.  
     * It accepts the same arguments as seen in treatAjaxArguments but will tipically just be called with a URL,
     * the defaultLinkURL functions being in charge of loading the content
     * 
     * @method   link
     * @todo     Manage anchors + returned data unclear
     * @global   
     * @memberof bbn.fn
     * 
     * @example
     * ```javascript
     * // Will open in a new window/tab
     * bbn.fn.link('https://nytimes.com');
     * // Will send an Ajax request
     * bbn.fn.link('my/page');
     * // Will open your default email program
     * bbn.fn.link('mailto:postmaster@test.com');
     * ```
     * 
     * @returns   
     */
Follows a link and if needed by sending the corresponding Ajax request and executing bbn.fn.defaultPreLinkFunction.

Once bbn has been initiated this function will be triggered every time a link is clicked.
It accepts the same arguments as seen in treatAjaxArguments but will tipically just be called with a URL,
the defaultLinkURL functions being in charge of loading the content

@method   link
@todo     Manage anchors + returned data unclear
@global
@memberof bbn.fn



@returns
Follows a link and if needed by sending the corresponding Ajax request and executing bbn . fn . defaultPreLinkFunction . Once bbn has been initiated this function will be triggered every time a link is clicked . It accepts the same arguments as seen in treatAjaxArguments but will tipically just be called with a URL the defaultLinkURL functions being in charge of loading the content @method link @todo Manage anchors + returned data unclear @global @memberof bbn . fn @returns
link(){
      let cfg = bbn.fn.treatAjaxArguments(arguments),
          ok = 1;
      if ( cfg === true ){
        return true;
      }
      if ( !cfg ){
        return bbn.fn.link(window.location.href);
      }
      if ( cfg.url.indexOf('javascript:') === 0 ){
        return true;
      }
      if ( cfg.url.indexOf('data:') === 0 ){
        return true;
      }
      if ( cfg.url.indexOf('#') === 0 ){
        location.href = bbn.env.url + cfg.url;
        return true;
      }
      else if ( cfg.url.indexOf('mailto:') === 0 ){
        bbn.env.ignoreUnload = true;
        window.location.href = cfg.url;
        setTimeout(() => {
          bbn.env.ignoreUnload = false;
        }, 0)
        return false;
      }
      if ( bbn.fn.getLoader(cfg.url) ){
        return false;
      }
      if ( ((cfg.url.indexOf("http://") === 0) || (cfg.url.indexOf("https://") === 0)) &&
        (cfg.url.indexOf(bbn.env.host) !== 0) ){
        if ( cfg.e ){
          cfg.e.preventDefault();
        }
        window.open(cfg.url);
        return false;
      }
      else if ( (cfg.url !== bbn.env.params.join("/")) || (cfg.force === 1) ){
        if ( cfg.successFn ){
          ok = cfg.successFn(cfg.url);
        }
        else if ( bbn.fn.defaultPreLinkFunction ){
          ok = bbn.fn.defaultPreLinkFunction(cfg.url, cfg.force, cfg.ele);
          if ( ok.data !== undefined ){
            bbn.fn.extend(cfg.obj, ok.data);
            ok = 1;
          }
        }
        if ( ok ){
          if ( ok !== 1 && (typeof ok === 'string') ){
            cfg.url = ok;
          }
          let errSt = bbn._("The Ajax call to") + ' ' + cfg.url + ' ';
          return bbn.fn.ajax(cfg.url, cfg.datatype, cfg.obj, function(res){
            if ( !res ){
              bbn.fn.log(errSt + bbn._("returned no answer"));
            }
            if ( bbn.fn.isObject(res) ){
              if ( (Object.keys(res).length === 0) ){
                bbn.fn.log(errSt + bbn._("returned an empty object"));
              }
              if ( res.new_url ){
                res.old_path = cfg.url;
                cfg.url = res.new_url;
              }
              else if ( res.url && (cfg.url !== res.url) ){
                res.old_path = cfg.url;
              }
            }
            if (
              bbn.fn.callback(cfg.url, res, cfg.successFn, null, cfg.ele) &&
              res.noNav === undefined
            ){
              if ( bbn.env.path.indexOf(cfg.url) !== 0 ){
                bbn.fn.setNavigationVars(cfg.url, (res.title ? res.title + ' - ' : '' ) + bbn.env.siteTitle);
              }
            }
          }, cfg.errorFn || null);
        }
      }
      return true;
    }
link ( ) { let cfg = bbn . fn . treatAjaxArguments ( arguments ) , ok = 1 ; if ( cfg === true ) { return true ; } if ( ! cfg ) { return bbn . fn . link ( window . location . href ) ; } if ( cfg . url . indexOf ( 'javascript:' ) === 0 ) { return true ; } if ( cfg . url . indexOf ( 'data:' ) === 0 ) { return true ; } if ( cfg . url . indexOf ( '#' ) === 0 ) { location . href = bbn . env . url + cfg . url ; return true ; } else if ( cfg . url . indexOf ( 'mailto:' ) === 0 ) { bbn . env . ignoreUnload = true ; window . location . href = cfg . url ; setTimeout ( ( ) => { bbn . env . ignoreUnload = false ; } , 0 ) return false ; } if ( bbn . fn . getLoader ( cfg . url ) ) { return false ; } if ( ( ( cfg . url . indexOf ( "http://" ) === 0 ) || ( cfg . url . indexOf ( "https://" ) === 0 ) ) && ( cfg . url . indexOf ( bbn . env . host ) !== 0 ) ) { if ( cfg . e ) { cfg . e . preventDefault ( ) ; } window . open ( cfg . url ) ; return false ; } else if ( ( cfg . url !== bbn . env . params . join ( "/" ) ) || ( cfg . force === 1 ) ) { if ( cfg . successFn ) { ok = cfg . successFn ( cfg . url ) ; } else if ( bbn . fn . defaultPreLinkFunction ) { ok = bbn . fn . defaultPreLinkFunction ( cfg . url , cfg . force , cfg . ele ) ; if ( ok . data !== undefined ) { bbn . fn . extend ( cfg . obj , ok . data ) ; ok = 1 ; } } if ( ok ) { if ( ok !== 1 && ( typeof ok === 'string' ) ) { cfg . url = ok ; } let errSt = bbn . _ ( "The Ajax call to" ) + ' ' + cfg . url + ' ' ; return bbn . fn . ajax ( cfg . url , cfg . datatype , cfg . obj , function ( res ) { if ( ! res ) { bbn . fn . log ( errSt + bbn . _ ( "returned no answer" ) ) ; } if ( bbn . fn . isObject ( res ) ) { if ( ( Object . keys ( res ) . length === 0 ) ) { bbn . fn . log ( errSt + bbn . _ ( "returned an empty object" ) ) ; } if ( res . new_url ) { res . old_path = cfg . url ; cfg . url = res . new_url ; } else if ( res . url && ( cfg . url !== res . url ) ) { res . old_path = cfg . url ; } } if ( bbn . fn . callback ( cfg . url , res , cfg . successFn , null , cfg . ele ) && res . noNav === undefined ) { if ( bbn . env . path . indexOf ( cfg . url ) !== 0 ) { bbn . fn . setNavigationVars ( cfg . url , ( res . title ? res . title + ' - ' : '' ) + bbn . env . siteTitle ) ; } } } , cfg . errorFn || null ) ; } } return true ; }
Follows a link and if needed by sending the corresponding Ajax request and executing bbn.fn.defaultPreLinkFunction.
Follows a link and if needed by sending the corresponding Ajax request and executing bbn . fn . defaultPreLinkFunction .
/* If we can't find a correct link we load the current URL */ /* Just executing the javascript if there is */ /*
        if ( window.history ){
          bbn.env.historyDisabled = true;
          let state = h.state;
          window.history.replaceState(null, state.title, bbn.env.url);
        }
        bbn.env.historyDisabled = false;
        */ /* Mail link */ /* Opens an external page in a new window */ /* The URL is fine so go ahead if something is not already loading */ /* If a second callback is defined, it is triggered instead of defaultPreLinkFunction */ /** todo Do we keep obj in the unique string or do we make that only one concurrent connection to the same address can occur at the same time? */ // If there's nothing in the result, just an empty object, the callback stops here and the URL is not changed // This solution is not very clean (we can't shorten a URL)