<commit_before>/// <reference path="../define.ts"/>
/// <reference path="app.balancer.ts"/>
/// <reference path="app.page.utility.ts"/>
/// <reference path="../library/utility.ts"/>

/* MODEL */

module MODULE.MODEL.APP {

  export class PageFetch {

    constructor(

    private model_: ModelInterface,
    private page_: PageInterface,
    private balancer_: BalancerInterface,
    private setting_: SettingInterface,
    private event_: JQueryEventObject,
    private success_: (setting: SettingInterface, event: JQueryEventObject, data: string, textStatus: string, $xhr: JQueryXHR, host: string, bind: JQueryXHR[]) => any,
    private failure_: (setting: SettingInterface, event: JQueryEventObject, data: string, textStatus: string, $xhr: JQueryXHR, host: string, bind: JQueryXHR[]) => any
    ) {
      this.main_();
    }
    
    private util_ = LIBRARY.Utility

    private host_: string
    private data_: string
    private textStatus_: string
    private jqXHR_: JQueryXHR
    private binds_: JQueryXHR[] = []
    private jsons_: {}[] = []
    private errorThrown_: string

    private main_(): void {
      var that = this,
          setting = this.setting_,
          event = this.event_ = jQuery.extend(true, {}, this.event_),
          wait: number = <number>this.util_.fire(setting.wait, setting, [event, setting, setting.origLocation.cloneNode(), setting.destLocation.cloneNode()]);

      var speedcheck = setting.speedcheck, speed = this.model_.speed;
      speedcheck && (speed.fire = event.timeStamp);
      speedcheck && speed.time.splice(0, 100, 0);
      speedcheck && speed.name.splice(0, 100, 'pjax(' + speed.time.slice(-1) + ')');

      var cache: CacheInterface;
      switch (setting.cache[event.type.toLowerCase()] && event.type.toLowerCase()) {
        case EVENT.CLICK:
          cache = this.model_.getCache(setting.destLocation.href);
          break;

        case EVENT.SUBMIT:
          cache = setting.cache[(<HTMLFormElement>event.currentTarget).method.toLowerCase()] ? this.model_.getCache(setting.destLocation.href) : cache;
          break;

        case EVENT.POPSTATE:
          cache = this.model_.getCache(setting.destLocation.href);
          break;
      }

      var $xhr = this.model_.getPageXHR();
      if ($xhr && $xhr.readyState < 4 && $xhr.location && this.model_.comparePageByUrl($xhr.location.href, setting.destLocation.href)) {
        return;
      }

      this.dispatchEvent(document, setting.nss.event.pjax.fetch, false, false);

      if (cache && cache.jqXHR && 200 === +cache.jqXHR.status) {
        // cache
        speedcheck && speed.name.splice(0, 1, 'cache(' + speed.time.slice(-1) + ')');

        this.host_ = setting.balance.active && this.model_.host().split('//').pop() || '';
        let requestDataLocation = this.balance_(setting.destLocation.href);
        this.model_.setDataXHR(this.bind_(<JQueryAjaxSettings[]>this.util_.fire(setting.bind, setting, [event, setting, setting.origLocation.cloneNode(), requestDataLocation.cloneNode()])));

        $xhr = cache.jqXHR;
        $xhr.location = $xhr.location || <HTMLAnchorElement>setting.destLocation.cloneNode();
        this.model_.setPageXHR($xhr);
        this.page_.loadtime = 0;
        this.host_ = this.balancer_.sanitize(cache.host, setting);
        this.data_ = cache.jqXHR.responseText;
        this.textStatus_ = cache.textStatus;
        this.jqXHR_ = cache.jqXHR;
        if (this.model_.isDeferrable) {
          var defer: JQueryDeferred<any> = this.wait_(wait);
          this.page_.setWait(defer);
          jQuery.when(jQuery.Deferred().resolve(this.data_, this.textStatus_, this.jqXHR_), defer, jQuery.when.apply(null, this.model_.getDataXHR()))
          .done(done).fail(fail).always(always);
        } else {
          var context: JQueryAjaxSettings = jQuery.extend({}, jQuery.ajaxSettings, setting.ajax);
          context = context.context || context;
          success.call(context, this.data_, this.textStatus_, this.jqXHR_);
          complete.call(context, this.jqXHR_, this.textStatus_);
        }
      } else if ($xhr && $xhr.follow && !~'error abort timeout parsererror'.indexOf($xhr.statusText)) {
        // preload
        speedcheck && speed.time.splice(0, 1, $xhr.timeStamp - speed.fire);
        speedcheck && speed.name.splice(0, 1, 'preload(' + speed.time.slice(-1) + ')');
        speedcheck && speed.time.push(speed.now() - speed.fire);
        speedcheck && speed.name.push('continue(' + speed.time.slice(-1) + ')');

        this.host_ = setting.balance.active && this.model_.host().split('//').pop() || '';
        let requestDataLocation = this.balance_(setting.destLocation.href);
        this.model_.setDataXHR(this.bind_(<JQueryAjaxSettings[]>this.util_.fire(setting.bind, setting, [event, setting, setting.origLocation.cloneNode(), requestDataLocation.cloneNode()])));

        $xhr.location = <HTMLAnchorElement>setting.destLocation.cloneNode();
        this.model_.setPageXHR($xhr);
        this.host_ = this.model_.host();
        this.page_.loadtime = $xhr.timeStamp;
        var defer: JQueryDeferred<any> = this.wait_(wait);
        this.page_.setWait(defer);
        delete $xhr.timeStamp;
        jQuery.when($xhr, defer, jQuery.when.apply(null, this.model_.getDataXHR()))
        .done(done).fail(fail).always(always);
      } else {
        // default
        this.page_.loadtime = event.timeStamp;
        var ajax: JQueryAjaxSettings = {},
            callbacks: JQueryAjaxSettings = {};

        this.host_ = setting.balance.active && this.model_.host().split('//').pop() || '';
        let requestDataLocation = this.balance_(setting.destLocation.href);
        this.model_.setDataXHR(this.bind_(<JQueryAjaxSettings[]>this.util_.fire(setting.bind, setting, [event, setting, setting.origLocation.cloneNode(), requestDataLocation.cloneNode()])));

        let requestPageLocation = this.balance_(setting.destLocation.href);
        ajax.url = !setting.server.query ? requestPageLocation.href
                                         : [
                                             requestPageLocation.protocol,
                                             '//',
                                             requestPageLocation.host,
                                             requestPageLocation.pathname.replace(/^\/?/, '/'),
                                             requestPageLocation.search.replace(/&*$/, '&' + setting.server.query).replace(/^\??&/, '?').replace(/(\?|&)$/, ''),
                                             requestPageLocation.hash
                                           ].join('');
        switch (event.type.toLowerCase()) {
          case EVENT.CLICK:
            ajax.type = 'GET';<commit_msg>Modify `bind` feature<commit_after>/// <reference path="../define.ts"/>
/// <reference path="app.balancer.ts"/>
/// <reference path="app.page.utility.ts"/>
/// <reference path="../library/utility.ts"/>

/* MODEL */

module MODULE.MODEL.APP {

  export class PageFetch {

    constructor(

    private model_: ModelInterface,
    private page_: PageInterface,
    private balancer_: BalancerInterface,
    private setting_: SettingInterface,
    private event_: JQueryEventObject,
    private success_: (setting: SettingInterface, event: JQueryEventObject, data: string, textStatus: string, $xhr: JQueryXHR, host: string, bind: JQueryXHR[]) => any,
    private failure_: (setting: SettingInterface, event: JQueryEventObject, data: string, textStatus: string, $xhr: JQueryXHR, host: string, bind: JQueryXHR[]) => any
    ) {
      this.main_();
    }
    
    private util_ = LIBRARY.Utility

    private host_: string
    private data_: string
    private textStatus_: string
    private jqXHR_: JQueryXHR
    private binds_: JQueryXHR[] = []
    private jsons_: {}[] = []
    private errorThrown_: string

    private main_(): void {
      var that = this,
          setting = this.setting_,
          event = this.event_ = jQuery.extend(true, {}, this.event_),
          wait: number = <number>this.util_.fire(setting.wait, setting, [event, setting, setting.origLocation.cloneNode(), setting.destLocation.cloneNode()]);

      var speedcheck = setting.speedcheck, speed = this.model_.speed;
      speedcheck && (speed.fire = event.timeStamp);
      speedcheck && speed.time.splice(0, 100, 0);
      speedcheck && speed.name.splice(0, 100, 'pjax(' + speed.time.slice(-1) + ')');

      var cache: CacheInterface;
      switch (setting.cache[event.type.toLowerCase()] && event.type.toLowerCase()) {
        case EVENT.CLICK:
          cache = this.model_.getCache(setting.destLocation.href);
          break;

        case EVENT.SUBMIT:
          cache = setting.cache[(<HTMLFormElement>event.currentTarget).method.toLowerCase()] ? this.model_.getCache(setting.destLocation.href) : cache;
          break;

        case EVENT.POPSTATE:
          cache = this.model_.getCache(setting.destLocation.href);
          break;
      }

      var $xhr = this.model_.getPageXHR();
      if ($xhr && $xhr.readyState < 4 && $xhr.location && this.model_.comparePageByUrl($xhr.location.href, setting.destLocation.href)) {
        return;
      }

      this.dispatchEvent(document, setting.nss.event.pjax.fetch, false, false);

      if (cache && cache.jqXHR && 200 === +cache.jqXHR.status) {
        // cache
        speedcheck && speed.name.splice(0, 1, 'cache(' + speed.time.slice(-1) + ')');

        this.host_ = setting.balance.active && this.model_.host().split('//').pop() || '';
        this.model_.setDataXHR(this.bind_(<JQueryAjaxSettings[]>this.util_.fire(setting.bind, setting, [event, setting, setting.origLocation.cloneNode(), setting.destLocation.cloneNode()])));

        $xhr = cache.jqXHR;
        $xhr.location = $xhr.location || <HTMLAnchorElement>setting.destLocation.cloneNode();
        this.model_.setPageXHR($xhr);
        this.page_.loadtime = 0;
        this.host_ = this.balancer_.sanitize(cache.host, setting);
        this.data_ = cache.jqXHR.responseText;
        this.textStatus_ = cache.textStatus;
        this.jqXHR_ = cache.jqXHR;
        if (this.model_.isDeferrable) {
          var defer: JQueryDeferred<any> = this.wait_(wait);
          this.page_.setWait(defer);
          jQuery.when(jQuery.Deferred().resolve(this.data_, this.textStatus_, this.jqXHR_), defer, jQuery.when.apply(null, this.model_.getDataXHR()))
          .done(done).fail(fail).always(always);
        } else {
          var context: JQueryAjaxSettings = jQuery.extend({}, jQuery.ajaxSettings, setting.ajax);
          context = context.context || context;
          success.call(context, this.data_, this.textStatus_, this.jqXHR_);
          complete.call(context, this.jqXHR_, this.textStatus_);
        }
      } else if ($xhr && $xhr.follow && !~'error abort timeout parsererror'.indexOf($xhr.statusText)) {
        // preload
        speedcheck && speed.time.splice(0, 1, $xhr.timeStamp - speed.fire);
        speedcheck && speed.name.splice(0, 1, 'preload(' + speed.time.slice(-1) + ')');
        speedcheck && speed.time.push(speed.now() - speed.fire);
        speedcheck && speed.name.push('continue(' + speed.time.slice(-1) + ')');

        this.host_ = setting.balance.active && this.model_.host().split('//').pop() || '';
        this.model_.setDataXHR(this.bind_(<JQueryAjaxSettings[]>this.util_.fire(setting.bind, setting, [event, setting, setting.origLocation.cloneNode(), setting.destLocation.cloneNode()])));

        $xhr.location = <HTMLAnchorElement>setting.destLocation.cloneNode();
        this.model_.setPageXHR($xhr);
        this.host_ = this.model_.host();
        this.page_.loadtime = $xhr.timeStamp;
        var defer: JQueryDeferred<any> = this.wait_(wait);
        this.page_.setWait(defer);
        delete $xhr.timeStamp;
        jQuery.when($xhr, defer, jQuery.when.apply(null, this.model_.getDataXHR()))
        .done(done).fail(fail).always(always);
      } else {
        // default
        this.page_.loadtime = event.timeStamp;
        var ajax: JQueryAjaxSettings = {},
            callbacks: JQueryAjaxSettings = {};

        this.host_ = setting.balance.active && this.model_.host().split('//').pop() || '';
        this.model_.setDataXHR(this.bind_(<JQueryAjaxSettings[]>this.util_.fire(setting.bind, setting, [event, setting, setting.origLocation.cloneNode(), setting.destLocation.cloneNode()])));

        let requestLocation = this.balance_(setting.destLocation.href);
        ajax.url = !setting.server.query ? requestLocation.href
                                         : [
                                             requestLocation.protocol,
                                             '//',
                                             requestLocation.host,
                                             requestLocation.pathname.replace(/^\/?/, '/'),
                                             requestLocation.search.replace(/&*$/, '&' + setting.server.query).replace(/^\??&/, '?').replace(/(\?|&)$/, ''),
                                             requestLocation.hash
                                           ].join('');
        switch (event.type.toLowerCase()) {
          case EVENT.CLICK:
            ajax.type = 'GET';
{'fraction_non_alphanumeric': 0.1502461188943582, 'fraction_numerical': 0.0036349867474441497, 'mean_word_length': 2.5169107856191744, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 30, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4127591', 'n_tokens_mistral': 3892, 'n_tokens_neox': 3793, 'n_words': 656}