github.com/brownsys/tracing-framework-go@v0.0.0-20161210174012-0542a62412fe/go/darwin_amd64/misc/tour/static/js/controllers.js (about)

     1  /* Copyright 2012 The Go Authors.   All rights reserved.
     2   * Use of this source code is governed by a BSD-style
     3   * license that can be found in the LICENSE file.
     4   */
     5  'use strict';
     6  
     7  /* Controllers */
     8  
     9  
    10  angular.module('tour.controllers', []).
    11  
    12  // Navigation controller
    13  controller('EditorCtrl', ['$scope', '$routeParams', '$location', 'toc', 'i18n', 'run', 'fmt', 'editor', 'analytics', 'storage',
    14      function($scope, $routeParams, $location, toc, i18n, run, fmt, editor, analytics, storage) {
    15          var lessons = [];
    16          toc.lessons.then(function(v) {
    17              lessons = v;
    18              $scope.gotoPage($scope.curPage);
    19  
    20              // Store changes on the current file to local storage.
    21              $scope.$watch(function() {
    22                  var f = file();
    23                  return f && f.Content;
    24              }, function(val) {
    25                  storage.set(file().Hash, val);
    26              });
    27          });
    28  
    29          $scope.toc = toc;
    30          $scope.lessonId = $routeParams.lessonId;
    31          $scope.curPage = parseInt($routeParams.pageNumber);
    32          $scope.curFile = 0;
    33          $scope.job = null;
    34  
    35          $scope.nextPageClick = function(event) {
    36              event.preventDefault();
    37              $scope.nextPage();
    38          };
    39          $scope.prevPageClick = function(event) {
    40              event.preventDefault();
    41              $scope.prevPage();
    42          };
    43          $scope.nextPage = function() {
    44              $scope.gotoPage($scope.curPage + 1);
    45          };
    46          $scope.prevPage = function() {
    47              $scope.gotoPage($scope.curPage - 1);
    48          };
    49          $scope.gotoPage = function(page) {
    50              var l = $routeParams.lessonId;
    51              if (page >= 1 && page <= lessons[$scope.lessonId].Pages.length) {
    52                  $scope.curPage = page;
    53              } else {
    54                  l = (page < 1) ? toc.prevLesson(l) : toc.nextLesson(l);
    55                  if (l === '') { // If there's not previous or next
    56                      $location.path('/list');
    57                      return;
    58                  }
    59                  page = (page < 1) ? lessons[l].Pages.length : 1;
    60              }
    61              $location.path('/' + l + '/' + page);
    62              $scope.openFile($scope.curFile);
    63              analytics.trackView();
    64          };
    65          $scope.openFile = function(file) {
    66              $scope.curFile = file;
    67              editor.paint();
    68          };
    69  
    70          function log(mode, text) {
    71              $('.output.active').html('<pre class="' + mode + '">' + text + '</pre>');
    72          }
    73  
    74          function clearOutput() {
    75              $('.output.active').html('');
    76          }
    77  
    78          function file() {
    79              return lessons[$scope.lessonId].Pages[$scope.curPage - 1].Files[$scope.curFile];
    80          }
    81  
    82          $scope.run = function() {
    83              log('info', i18n.l('waiting'));
    84              var f = file();
    85              $scope.job = run(f.Content, $('.output.active > pre')[0], {
    86                  path: f.Name
    87              }, function() {
    88                  $scope.job = null;
    89                  $scope.$apply();
    90              });
    91          };
    92  
    93          $scope.kill = function() {
    94              if ($scope.job !== null) $scope.job.Kill();
    95          };
    96  
    97          $scope.format = function() {
    98              log('info', i18n.l('waiting'));
    99              fmt(file().Content).then(
   100                  function(data) {
   101                      if (data.data.Error !== '') {
   102                          log('stderr', data.data.Error);
   103                          return;
   104                      }
   105                      clearOutput();
   106                      file().Content = data.data.Body;
   107                  },
   108                  function(error) {
   109                      log('stderr', error);
   110                  });
   111          };
   112  
   113          $scope.reset = function() {
   114              file().Content = file().OrigContent;
   115          };
   116      }
   117  ]);