<commit_before><polymer-element name="app-state" attributes="header url forceReload regex model" hidden>
  <template>
    <states-helper id="helper"></states-helper>
  </template>
  <script>
    (function() {

      function viewsOfState(viewList, parentList, map) {
        var result = [];

        parentList.forEach(function(state) {
          viewList.forEach(function(viewItems) {
            viewItems.forEach(function(viewItem) {
              if (viewItem.parentNode === state)
                result.push(map ? map(viewItem) : viewItem);
            });
          });
        });

        return result;
      }

      function unloadStatesAndGetHierarchy(targetState, stateHierarchy) {

        stateHierarchy = stateHierarchy || [];

        var unloadSiblingStates = function(state) {
          var next = state.nextElementSibling;
          while (next) {
            if (next.tagName == 'APP-STATE' && next.loaded)
              next.unLoad();
            next = next.nextElementSibling;
          }

          var previous = state.previousElementSibling;
          while (previous) {
            if (previous.tagName == 'APP-STATE' && previous.loaded)
              previous.unLoad();
            previous = previous.previousElementSibling;
          }
        };

        var node = targetState.parentNode;

        stateHierarchy.splice(0, 0, targetState);

        while (node && node.tagName === 'APP-STATE') {
          stateHierarchy.splice(0, 0, node);
          unloadSiblingStates(node);
          node = node.parentNode;
        }

        unloadSiblingStates(targetState);

        var children = targetState.querySelectorAll('app-state');

        [].map.call(children, function(childState) {
          if (childState && childState.tagName == 'APP-STATE' && childState.loaded)
            childState.unLoad();
        });
      }

      function loadState(hierarchy, contentPresenters, loadedPresenters) {

        var visitState = function(state, presenters, childPresenters) {
          var directChildren = presenters.map(function(view) {
            return view.target;
          });

          var childrenOfChildren = childPresenters.map(function(view) {
            return view.target;
          });

          var stateWasLoaded = state.loaded;

          for (var i = 0; i < directChildren.length; i++) {
            var presenter = presenters[i];
            if (childrenOfChildren.indexOf(directChildren[i]) < 0) {
              if (stateWasLoaded) {
                if (typeof(state.forceReload) != 'undefined') {
                  state.unLoad();
                  stateWasLoaded = false;
                }
                if (!presenter.instance)
                  presenter.loadContent(state.getModel());
                else {
                  presenter.changeModel(state.getModel());
                }
              } else {
                presenter.loadContent(state.getModel());
              }
            }
          }

          if (!stateWasLoaded) {
            state.loaded = true;
            console.log('State loaded:' + state.id);
          }
        };


        loadedPresenters = loadedPresenters || [];

        for (var i = 0; i < hierarchy.length; i++) {
          var state = hierarchy[i];
          var presenters = viewsOfState(contentPresenters, [state]);
          var childPresenters = viewsOfState(contentPresenters, hierarchy.slice(i + 1));
          visitState.call(this, state, presenters, childPresenters);
        }
      }

      Polymer({

        get parentState() {
          var node = this.parentNode;
          while(node) {
            if (node.tagName == "APP-STATE")
              return node;
            node = node.parentNode;
          }
        },

        getAppStates: function() {
          var node = this.parentNode;
          while (node) {
            if (node.tagName == 'STATE-MANAGER')
              return node;
            node = node.parentNode;
          }
        },

        created: function() {
          this.stateViewCache = document.createElement('view-registry').getCache('virtual-view:target');
        },

        ready: function() {
          this.appStates = this.getAppStates();
          if (!this.appStates)
            this.appStates = document.querySelector('body /deep/ state-manager');
        },

        getModel: function() {
          var url = this.appStates.parseUrl();
          var urlParams = this.$.helper.routeArguments(this.url, url.path, url.search, this.regex);

          var model = this.model, node = this.parentNode;

          while(!model && node && (node.tagName == 'APP-STATE' || node.tagName == 'STATE-MANAGER')) {
            model = node.model;
            node = node.parentNode;
          }

          if (model && model !== this.model) {
            var instance = Object.create(model);
            model = instance;
          }

          return {
            model: model,
            urlParams: urlParams,
            state: this
          };
        },

        load: function() {
          if (this.hasAttribute('redirect')) {
            this.appStates.go(this.getAttribute('redirect'), {replace: true});
            return;
          }
          var hierarchy = [];
          unloadStatesAndGetHierarchy(this, hierarchy);
          loadState.call(this, hierarchy, this.stateViewCache.itemsArray());
        },
<commit_msg>fixed some minor bugs<commit_after><polymer-element name="app-state" attributes="header url forceReload regex model" hidden>
  <template>
    <states-helper id="helper"></states-helper>
  </template>
  <script>
    (function() {

      function viewsOfState(viewList, parentList, map) {
        var result = [];

        parentList.forEach(function(state) {
          viewList.forEach(function(viewItems) {
            viewItems.forEach(function(viewItem) {
              if (viewItem.parentNode === state)
                result.push(map ? map(viewItem) : viewItem);
            });
          });
        });

        return result;
      }

      function unloadStatesAndGetHierarchy(targetState, stateHierarchy) {

        stateHierarchy = stateHierarchy || [];

        var unloadSiblingStates = function(state) {
          var next = state.nextElementSibling;
          while (next) {
            if (next.tagName == 'APP-STATE' && next.loaded)
              next.unLoad();
            next = next.nextElementSibling;
          }

          var previous = state.previousElementSibling;
          while (previous) {
            if (previous.tagName == 'APP-STATE' && previous.loaded)
              previous.unLoad();
            previous = previous.previousElementSibling;
          }
        };

        var node = targetState.parentNode;

        stateHierarchy.splice(0, 0, targetState);

        while (node && node.tagName === 'APP-STATE') {
          stateHierarchy.splice(0, 0, node);
          unloadSiblingStates(node);
          node = node.parentNode;
        }

        unloadSiblingStates(targetState);

        var children = targetState.querySelectorAll('app-state');

        [].map.call(children, function(childState) {
          if (childState && childState.tagName == 'APP-STATE' && childState.loaded)
            childState.unLoad();
        });
      }

      function loadState(hierarchy, contentPresenters, loadedPresenters) {

        var visitState = function(state, presenters, childPresenters) {
          var directChildren = presenters.map(function(view) {
            return view.target;
          });

          var childrenOfChildren = childPresenters.map(function(view) {
            return view.target;
          });

          var stateWasLoaded = state.loaded;

          for (var i = 0; i < directChildren.length; i++) {
            var presenter = presenters[i];
            if (childrenOfChildren.indexOf(directChildren[i]) < 0) {
              if (stateWasLoaded) {
                if (typeof(state.forceReload) != 'undefined') {
                  state.unLoad();
                  stateWasLoaded = false;
                }
                if (!presenter.instance)
                  presenter.loadContent(state.getModel());
                else {
                  presenter.changeModel(state.getModel());
                }
              } else {
                presenter.loadContent(state.getModel());
              }
            }
          }

          if (!stateWasLoaded) {
            state.loaded = true;
            console.log('State loaded:' + state.id);
          }
        };


        loadedPresenters = loadedPresenters || [];

        for (var i = 0; i < hierarchy.length; i++) {
          var state = hierarchy[i];
          var presenters = viewsOfState(contentPresenters, [state]);
          var childPresenters = viewsOfState(contentPresenters, hierarchy.slice(i + 1));
          visitState.call(this, state, presenters, childPresenters);
        }
      }

      Polymer({

        get parentState() {
          var node = this.parentNode;
          while(node) {
            if (node.tagName == "APP-STATE")
              return node;
            node = node.parentNode;
          }
        },

        getStateManager: function() {
          var node = this.parentNode;
          while (node) {
            if (node.tagName == 'STATE-MANAGER')
              return node;
            node = node.parentNode;
          }
        },

        created: function() {
          this.stateViewCache = document.createElement('view-registry').getCache('virtual-view:target');
        },

        ready: function() {
          this.stateManager = this.getStateManager();
          if (!this.stateManager)
            this.stateManager = document.querySelector('body /deep/ state-manager');
        },

        getModel: function() {
          var url = this.stateManager.parseUrl();
          var urlParams = this.$.helper.routeArguments(this.url, url.path, url.search, this.regex, this.stateManager.typecast === 'auto');

          var model = this.model, node = this.parentNode;

          while(!model && node && (node.tagName == 'APP-STATE' || node.tagName == 'STATE-MANAGER')) {
            model = node.model;
            node = node.parentNode;
          }

          if (model && model !== this.model) {
            var instance = Object.create(model);
            model = instance;
          }

          return {
            model: model,
            urlParams: urlParams,
            state: this
          };
        },

        load: function() {
          if (this.hasAttribute('redirect')) {
            this.stateManager.go(this.getAttribute('redirect'), {replace: true});
            return;
          }
          var hierarchy = [];
          unloadStatesAndGetHierarchy(this, hierarchy);
          loadState.call(this, hierarchy, this.stateViewCache.itemsArray());
        },

{'fraction_non_alphanumeric': 0.11816838995568685, 'fraction_numerical': 0.0014771048744460858, 'mean_word_length': 1.7349154253976269, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1146309', 'n_tokens_mistral': 2877, 'n_tokens_neox': 2812, 'n_words': 549}