<commit_before>app.controller('mainController',
           ['$scope','$stateParams','$location','$window','$rootScope','$filter','$modal','hotkeys','toaster','cfpLoadingBar','GitHub',
    function($scope,  $stateParams,  $location,  $window,  $rootScope,  $filter,  $modal,  hotkeys,  toaster,  cfpLoadingBar,  GitHub){
    
    //show loading text on startup
    $scope.markdown='#Loading...';
       
    /*****************************************
        Set up the markdown editor
    ******************************************/
    $scope.edit = false;                        // set edit to false on startup (hide editor!)
    $scope.editorOptions = {                    // set codemirror options
        mode: 'text/x-markdown',
        theme:'default',
        lineNumbers: true,
        matchBrackets: true,
        lineWrapping: true,
        indentWithTabs: true,
        extraKeys: {
          "Ctrl-S": function (instance) {       // add save shortcut to codemirror editor
             $scope.savePost();
             return false;
          },
          "Cmd-S": function (instance) {       // add save shortcut to codemirror editor (mac)
             $scope.savePost();
             return false;
          },
          "Ctrl-E": function (instance) {       // add edit shortcut to codemirror editor
             $scope.edit=!$scope.edit;
             //TODO: create a focus directive (use tabIndex="-1")
             window.document.getElementsByClassName('rightPanel')[0].focus();
             return false;
          },
          "Cmd-E": function (instance) {       // add edit shortcut to codemirror editor
             $scope.edit=!$scope.edit;
             //TODO: create a focus directive (use tabIndex="-1")
             window.document.getElementsByClassName('rightPanel')[0].focus();
             return false;
          },
          "Ctrl-M": function (instance) {       // add new post shortcut to codemirror editor
             $scope.newPost();
             return false;
          },
          "Cmd-M": function (instance) {       // add new post shortcut to codemirror editor
             $scope.newPost();
             return false;
          },
        },
        onLoad: function(_editor){              // When the editor loads it NEEDS to have text/markdown
            $scope.editor = _editor;
            $scope.html = $window.marked($scope.markdown);
            
            $scope.editor.on("keyup", function(e) { // as we type/move... scroll html view to the new content
              // TODO: Change to key UP/DOWN only ?   
              $scope.editorCurPos = e.getCursor().line +1;
              $scope.editorHeight = e.doc.height;
              //$scope.editorNumLines= e.doc.size;
              //$scope.scrollFactor = $scope.editorCurPos / $scope.editorNumLines;
              
              $scope.$apply(); // needs to be updated

              // TODO: look at using $uiViewScroll !!!
              //window.scrollTo(0,($scope.editorHeight/15)*$scope.editorCurPos);
            });
        },
    };


    /*****************************************
        Set up ChiefFancyPants hotkeys
    ******************************************/
    hotkeys.bindTo($scope)
      .add({
        combo: 'mod+e',
        description: 'Open editor',
        callback: function(event) {
            event.preventDefault();
            if( $location.path().indexOf('/post' ) != -1 ) { $scope.edit=!$scope.edit; }
            else { toaster.pop("warning", "Oops", "Can't edit archive!"); }
            //TODO: create a directive to set editor focus
            setTimeout(function(){$scope.editor.focus();$scope.editor.refresh();}, 120);
        }
    }).add({
        combo: 'mod+s',
        description: 'Save changes',
        callback: function(event) {
            event.preventDefault();
            if( $location.path().indexOf('/post' ) != -1 ) { $scope.savePost(); }
            else { $scope.modalPrompt();  }
        }
    }).add({
        combo: 'mod+m',
        description: 'New post',
        callback: function(event) {
            event.preventDefault();
            $scope.newPost();
        }
    }).add({
        combo: 'mod+d',
        description: 'Delete post',
        callback: function(event) {
            event.preventDefault();
            $scope.deletePost();
        }
    }).add({
        combo: 'return',
        description: 'Select post',
        callback: function(event) {
            event.preventDefault();
            // save in archive to upload to github
            if(typeof modalForm !== 'undefined' && modalForm !== null){
                document.querySelector('#modalForm').submit();
            }
            // save anywhere else just saves to posts array
            else if( $location.path() == '/') {
                $scope.updateFiteredList();
                // select the post based on the position of the filtered list
                $scope.selectPost($scope.filteredItems[$scope.position].title);
            }
        }
    }).add({
        combo: 'backspace',
        description: 'Blog archive',
        callback: function(event) {
            event.preventDefault();
            $location.path('/');
            $scope.edit = false;
        }
    }).add({
        combo: 'up',
        description: 'Prev post',
        callback: function(event) {
            if( $location.path() == '/' ) {
                event.preventDefault();
                //if at top of visible list AND NOT at start of list
                if($scope.position === 0 && $scope.start>0){
                    $scope.start--;
                }
                if($scope.position!==0){
                        $scope.position--;
                }
                $scope.updateFiteredList();
            }
        }
    }).add({
        combo: 'down',
        description: 'Next post',
        callback: function(event) {
            if( $location.path() == '/' ) {
                event.preventDefault();
                //TODO: How long is a piece of ng-repeat...
                // what if we are at bottom of visible list but NOT at end of list...?
                if($scope.position == $scope.numPosts-1 && $scope.start<($scope.archive.length-$scope.numPosts)){
                    $scope.start++;
                }
                if($scope.position!=$scope.numPosts-1 ){
                        $scope.position++;
                }
                $scope.updateFiteredList();
            }
        }
    });



    /*****************************************
        Helper functions
    ******************************************/

    // get username from window.location
    $scope.username = $window.location.hostname.contains('github') ? $window.location.hostname.split('.')[0]: 'endz';
    // get blog settings & posts
    GitHub.getData($scope.username).then(function(data){<commit_msg>initial commit<commit_after>app.controller('mainController',
           ['$scope','$stateParams','$location','$window','$rootScope','$filter','$modal','hotkeys','toaster','cfpLoadingBar','GitHub',
    function($scope,  $stateParams,  $location,  $window,  $rootScope,  $filter,  $modal,  hotkeys,  toaster,  cfpLoadingBar,  GitHub){
    
    //show loading text on startup
    $scope.markdown='#Loading...';
       
    /*****************************************
        Set up the markdown editor
    ******************************************/
    $scope.edit = false;                        // set edit to false on startup (hide editor!)
    $scope.editorOptions = {                    // set codemirror options
        mode: 'text/x-markdown',
        theme:'default',
        lineNumbers: true,
        matchBrackets: true,
        lineWrapping: true,
        indentWithTabs: true,
        extraKeys: {
          "Ctrl-S": function (instance) {       // add save shortcut to codemirror editor
             $scope.savePost();
             return false;
          },
          "Cmd-S": function (instance) {       // add save shortcut to codemirror editor (mac)
             $scope.savePost();
             return false;
          },
          "Ctrl-E": function (instance) {       // add edit shortcut to codemirror editor
             $scope.edit=!$scope.edit;
             //TODO: create a focus directive (use tabIndex="-1")
             window.document.getElementsByClassName('rightPanel')[0].focus();
             return false;
          },
          "Cmd-E": function (instance) {       // add edit shortcut to codemirror editor
             $scope.edit=!$scope.edit;
             //TODO: create a focus directive (use tabIndex="-1")
             window.document.getElementsByClassName('rightPanel')[0].focus();
             return false;
          },
          "Ctrl-M": function (instance) {       // add new post shortcut to codemirror editor
             $scope.newPost();
             return false;
          },
          "Cmd-M": function (instance) {       // add new post shortcut to codemirror editor
             $scope.newPost();
             return false;
          },
        },
        onLoad: function(_editor){              // When the editor loads it NEEDS to have text/markdown
            $scope.editor = _editor;
            $scope.html = $window.marked($scope.markdown);
            
            $scope.editor.on("keyup", function(e) { // as we type/move... scroll html view to the new content
              // TODO: Change to key UP/DOWN only ?   
              $scope.editorCurPos = e.getCursor().line +1;
              $scope.editorHeight = e.doc.height;
              //$scope.editorNumLines= e.doc.size;
              //$scope.scrollFactor = $scope.editorCurPos / $scope.editorNumLines;
              
              $scope.$apply(); // needs to be updated

              // TODO: look at using $uiViewScroll !!!
              //window.scrollTo(0,($scope.editorHeight/15)*$scope.editorCurPos);
            });
        },
    };


    /*****************************************
        Set up ChiefFancyPants hotkeys
    ******************************************/
    hotkeys.bindTo($scope)
      .add({
        combo: 'mod+e',
        description: 'Open editor',
        callback: function(event) {
            event.preventDefault();
            if( $location.path().indexOf('/post' ) != -1 ) { $scope.edit=!$scope.edit; }
            else { toaster.pop("warning", "Oops", "Can't edit archive!"); }
            //TODO: create a directive to set editor focus
            setTimeout(function(){$scope.editor.focus();$scope.editor.refresh();}, 120);
        }
    }).add({
        combo: 'mod+s',
        description: 'Save changes',
        callback: function(event) {
            event.preventDefault();
            if( $location.path().indexOf('/post' ) != -1 ) { $scope.savePost(); }
            else { $scope.modalPrompt();  }
        }
    }).add({
        combo: 'mod+m',
        description: 'New post',
        callback: function(event) {
            event.preventDefault();
            $scope.newPost();
        }
    }).add({
        combo: 'mod+d',
        description: 'Delete post',
        callback: function(event) {
            event.preventDefault();
            $scope.deletePost();
        }
    }).add({
        combo: 'return',
        description: 'Select post',
        callback: function(event) {
            event.preventDefault();
            // save in archive to upload to github
            if(typeof modalForm !== 'undefined' && modalForm !== null){
                document.querySelector('#modalForm').submit();
            }
            // save anywhere else just saves to posts array
            else if( $location.path() == '/') {
                $scope.updateFiteredList();
                // select the post based on the position of the filtered list
                $scope.selectPost($scope.filteredItems[$scope.position].title);
            }
        }
    }).add({
        combo: 'backspace',
        description: 'Blog archive',
        callback: function(event) {
            event.preventDefault();
            $location.path('/');
            $scope.edit = false;
        }
    }).add({
        combo: 'up',
        description: 'Prev post',
        callback: function(event) {
            if( $location.path() == '/' ) {
                event.preventDefault();
                //if at top of visible list AND NOT at start of list
                if($scope.position === 0 && $scope.start>0){
                    $scope.start--;
                }
                if($scope.position!==0){
                        $scope.position--;
                }
                $scope.updateFiteredList();
            }
        }
    }).add({
        combo: 'down',
        description: 'Next post',
        callback: function(event) {
            if( $location.path() == '/' ) {
                event.preventDefault();
                //TODO: How long is a piece of ng-repeat...
                // what if we are at bottom of visible list but NOT at end of list...?
                if($scope.position == $scope.numPosts-1 && $scope.start<($scope.archive.length-$scope.numPosts)){
                    $scope.start++;
                }
                if($scope.position!=$scope.numPosts-1 ){
                        $scope.position++;
                }
                $scope.updateFiteredList();
            }
        }
    });



    /*****************************************
        Helper functions
    ******************************************/

    // get username from window.location
    $scope.username = $window.location.hostname.indexOf('github')!=-1 ? $window.location.hostname.split('.')[0]: 'endz';
    // get blog settings & posts
    GitHub.getData($scope.username).then(function(data){
{'fraction_non_alphanumeric': 0.170186425866589, 'fraction_numerical': 0.002840081561316633, 'mean_word_length': 1.6775199844024176, 'pattern_counts': {'":': 12, '<': 5, '<?xml version=': 0, '>': 5, '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': '2177171', 'n_tokens_mistral': 3607, 'n_tokens_neox': 3502, 'n_words': 816}