github.com/brownsys/tracing-framework-go@v0.0.0-20161210174012-0542a62412fe/go/darwin_amd64/misc/tour/static/js/services.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  /* Services */
     8  
     9  angular.module('tour.services', []).
    10  
    11  // Google Analytics
    12  factory('analytics', ['$window',
    13      function(win) {
    14          var track = win.trackPageview || (function() {});
    15          return {
    16              trackView: track
    17          };
    18      }
    19  ]).
    20  
    21  // Internationalization
    22  factory('i18n', ['translation',
    23      function(translation) {
    24          return {
    25              l: function(key) {
    26                  if (translation[key]) return translation[key];
    27                  return '(no translation for ' + key + ')';
    28              }
    29          };
    30      }
    31  ]).
    32  
    33  // Running code
    34  factory('run', ['$window', 'editor',
    35      function(win, editor) {
    36          var writeInterceptor = function(writer, done) {
    37              return function(write) {
    38                  if (write.Kind == 'stderr') {
    39                      var lines = write.Body.split('\n');
    40                      for (var i in lines) {
    41                          var match = lines[i].match(/.*\.go:([0-9]+): ([^\n]*)/);
    42                          if (match !== null) {
    43                              editor.highlight(match[1], match[2]);
    44                          }
    45                      }
    46                  }
    47                  writer(write);
    48                  if (write.Kind == 'end') done();
    49              };
    50          };
    51          return function(code, output, options, done) {
    52              // PlaygroundOutput is defined in playground.js which is prepended
    53              // to the generated script.js in gotour/tour.go.
    54              // The next line removes the jshint warning.
    55              // global PlaygroundOutput
    56              return win.transport.Run(code, writeInterceptor(PlaygroundOutput(output), done), options);
    57          };
    58      }
    59  ]).
    60  
    61  // Formatting code
    62  factory('fmt', ['$http',
    63      function($http) {
    64          return function(body) {
    65              var params = $.param({
    66                  'body': body
    67              });
    68              var headers = {
    69                  'Content-Type': 'application/x-www-form-urlencoded'
    70              };
    71              return $http.post('/fmt', params, {
    72                  headers: headers
    73              });
    74          };
    75      }
    76  ]).
    77  
    78  // Local storage, persistent to page refreshing.
    79  factory('storage', ['$window',
    80      function(win) {
    81          try {
    82              // This will raise an exception if cookies are disabled.
    83              win.localStorage = win.localStorage;
    84              return {
    85                  get: function(key) {
    86                      return win.localStorage.getItem(key);
    87                  },
    88                  set: function(key, val) {
    89                      win.localStorage.setItem(key, val);
    90                  }
    91              };
    92          } catch (e) {
    93              return {
    94                  get: function() {
    95                      return null;
    96                  },
    97                  set: function() {}
    98              };
    99          }
   100      }
   101  ]).
   102  
   103  // Editor context service, kept through the whole app.
   104  factory('editor', ['$window', 'storage',
   105      function(win, storage) {
   106          var ctx = {
   107              syntax: storage.get('syntax') === 'true',
   108              toggleSyntax: function() {
   109                  ctx.syntax = !ctx.syntax;
   110                  storage.set('syntax', ctx.syntax);
   111                  ctx.paint();
   112              },
   113              paint: function() {
   114                  var mode = ctx.syntax && 'text/x-go' || 'text/x-go-comment';
   115                  // Wait for codemirror to start.
   116                  var set = function() {
   117                      if ($('.CodeMirror').length > 0) {
   118                          var cm = $('.CodeMirror')[0].CodeMirror;
   119                          if (cm.getOption('mode') == mode) {
   120                              cm.refresh();
   121                              return;
   122                          }
   123                          cm.setOption('mode', mode);
   124                      }
   125                      win.setTimeout(set, 10);
   126                  };
   127                  set();
   128              },
   129              highlight: function(line, message) {
   130                  $('.CodeMirror-code > div:nth-child(' + line + ')')
   131                      .addClass('line-error').attr('title', message);
   132              },
   133              onChange: function() {
   134                  $('.line-error').removeClass('line-error').attr('title', null);
   135              }
   136          };
   137          // Set in the window so the onChange function in the codemirror config
   138          // can call it.
   139          win.codeChanged = ctx.onChange;
   140          return ctx;
   141      }
   142  ]).
   143  
   144  // Table of contents management and navigation
   145  factory('toc', ['$http', '$q', '$log', 'tableOfContents', 'storage',
   146      function($http, $q, $log, tableOfContents, storage) {
   147          var modules = tableOfContents;
   148  
   149          var lessons = {};
   150  
   151          var prevLesson = function(id) {
   152              var mod = lessons[id].module;
   153              var idx = mod.lessons.indexOf(id);
   154              if (idx < 0) return '';
   155              if (idx > 0) return mod.lessons[idx - 1];
   156  
   157              idx = modules.indexOf(mod);
   158              if (idx <= 0) return '';
   159              mod = modules[idx - 1];
   160              return mod.lessons[mod.lessons.length - 1];
   161          };
   162  
   163          var nextLesson = function(id) {
   164              var mod = lessons[id].module;
   165              var idx = mod.lessons.indexOf(id);
   166              if (idx < 0) return '';
   167              if (idx + 1 < mod.lessons.length) return mod.lessons[idx + 1];
   168  
   169              idx = modules.indexOf(mod);
   170              if (idx < 0 || modules.length <= idx + 1) return '';
   171              mod = modules[idx + 1];
   172              return mod.lessons[0];
   173          };
   174  
   175          $http.get('/lesson/').then(
   176              function(data) {
   177                  lessons = data.data;
   178                  for (var m = 0; m < modules.length; m++) {
   179                      var module = modules[m];
   180                      module.lesson = {};
   181                      for (var l = 0; l < modules[m].lessons.length; l++) {
   182                          var lessonName = module.lessons[l];
   183                          var lesson = lessons[lessonName];
   184                          lesson.module = module;
   185                          module.lesson[lessonName] = lesson;
   186  
   187                          // replace file contents with locally stored copies.
   188                          for (var p = 0; p < lesson.Pages.length; p++) {
   189                              var page = lesson.Pages[p];
   190                              for (var f = 0; f < page.Files.length; f++) {
   191                                  page.Files[f].OrigContent = page.Files[f].Content;
   192                                  var val = storage.get(page.Files[f].Hash);
   193                                  if (val !== null) {
   194                                      page.Files[f].Content = val;
   195                                  }
   196                              }
   197                          }
   198                      }
   199                  }
   200                  moduleQ.resolve(modules);
   201                  lessonQ.resolve(lessons);
   202              },
   203              function(error) {
   204                  $log.error('error loading lessons : ', error);
   205                  moduleQ.reject(error);
   206                  lessonQ.reject(error);
   207              }
   208          );
   209  
   210          var moduleQ = $q.defer();
   211          var lessonQ = $q.defer();
   212  
   213          return {
   214              modules: moduleQ.promise,
   215              lessons: lessonQ.promise,
   216              prevLesson: prevLesson,
   217              nextLesson: nextLesson
   218          };
   219      }
   220  ]);