github.com/brownsys/tracing-framework-go@v0.0.0-20161210174012-0542a62412fe/go/darwin_amd64/misc/tour/static/js/directives.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  /* Directives */
     8  
     9  angular.module('tour.directives', []).
    10  
    11  // onpageup executes the given expression when Page Up is released.
    12  directive('onpageup', function() {
    13      return function(scope, elm, attrs) {
    14          elm.attr('tabindex', 0);
    15          elm.keyup(function(evt) {
    16              var key = evt.which || evt.keyCode;
    17              if (key == 33 && !evt.ctrlKey) {
    18                  scope.$apply(attrs.onpageup);
    19                  evt.preventDefault();
    20              }
    21          });
    22      };
    23  }).
    24  
    25  // onpagedown executes the given expression when Page Down is released.
    26  directive('onpagedown', function() {
    27      return function(scope, elm, attrs) {
    28          elm.attr('tabindex', 0);
    29          elm.keyup(function(evt) {
    30              var key = evt.which || evt.keyCode;
    31              if (key == 34 && !evt.ctrlKey) {
    32                  scope.$apply(attrs.onpagedown);
    33                  evt.preventDefault();
    34              }
    35          });
    36      };
    37  }).
    38  
    39  // autofocus sets the focus on the given element when the condition is true.
    40  directive('autofocus', function() {
    41      return function(scope, elm, attrs) {
    42          elm.attr('tabindex', 0);
    43          scope.$watch(function() {
    44              return scope.$eval(attrs.autofocus);
    45          }, function(val) {
    46              if (val === true) $(elm).focus();
    47          });
    48      };
    49  }).
    50  
    51  // syntax-checkbox activates and deactivates
    52  directive('syntaxCheckbox', ['editor',
    53      function(editor) {
    54          return function(scope, elm) {
    55              elm.click(function() {
    56                  editor.toggleSyntax();
    57                  scope.$digest();
    58              });
    59              scope.editor = editor;
    60          };
    61      }
    62  ]).
    63  
    64  // verticalSlide creates a sliding separator between the left and right elements.
    65  // e.g.:
    66  // <div id="header">Some content</div>
    67  // <div vertical-slide top="#header" bottom="#footer"></div>
    68  // <div id="footer">Some footer</div>
    69  directive('verticalSlide', ['editor',
    70      function(editor) {
    71          return function(scope, elm, attrs) {
    72              var moveTo = function(x) {
    73                  if (x < 0) {
    74                      x = 0;
    75                  }
    76                  if (x > $(window).width()) {
    77                      x = $(window).width();
    78                  }
    79                  elm.css('left', x);
    80                  $(attrs.left).width(x);
    81                  $(attrs.right).offset({
    82                      left: x
    83                  });
    84                  editor.x = x;
    85              };
    86  
    87              elm.draggable({
    88                  axis: 'x',
    89                  drag: function(event) {
    90                      moveTo(event.clientX);
    91                      return true;
    92                  },
    93                  containment: 'parent',
    94              });
    95  
    96              if (editor.x !== undefined) {
    97                  moveTo(editor.x);
    98              }
    99          };
   100      }
   101  ]).
   102  
   103  // horizontalSlide creates a sliding separator between the top and bottom elements.
   104  // <div id="menu">Some menu</div>
   105  // <div vertical-slide left="#menu" bottom="#content"></div>
   106  // <div id="content">Some content</div>
   107  directive('horizontalSlide', ['editor',
   108      function(editor) {
   109          return function(scope, elm, attrs) {
   110              var moveTo = function(y) {
   111                  var top = $(attrs.top).offset().top;
   112                  if (y < top) {
   113                      y = top;
   114                  }
   115                  elm.css('top', y - top);
   116                  $(attrs.top).height(y - top);
   117                  $(attrs.bottom).offset({
   118                      top: y,
   119                      height: 0
   120                  });
   121                  editor.y = y;
   122              };
   123              elm.draggable({
   124                  axis: 'y',
   125                  drag: function(event) {
   126                      moveTo(event.clientY);
   127                      return true;
   128                  },
   129                  containment: 'parent',
   130              });
   131  
   132              if (editor.y !== undefined) {
   133                  moveTo(editor.y);
   134              }
   135          };
   136      }
   137  ]).
   138  
   139  directive('tableOfContentsButton', ['i18n', function(i18n) {
   140      var speed = 250;
   141      return {
   142          restrict: 'A',
   143          templateUrl: '/static/partials/toc-button.html',
   144          link: function(scope, elm, attrs) {
   145              scope.tocMessage = i18n.l('toc');
   146              elm.on('click', function() {
   147                  var toc = $(attrs.tableOfContentsButton);
   148                  // hide all non active lessons before displaying the toc.
   149                  var visible = toc.css('display') != 'none';
   150                  if (!visible) {
   151                      toc.find('.toc-lesson:not(.active) .toc-page').hide();
   152                      toc.find('.toc-lesson.active .toc-page').show();
   153                  }
   154                  toc.toggle('slide', {
   155                      direction: 'right'
   156                  }, speed);
   157  
   158                  // if fullscreen hide the rest of the content when showing the atoc.
   159                  var fullScreen = toc.width() == $(window).width();
   160                  if (fullScreen) $('#editor-container')[visible ? 'show' : 'hide']();
   161              });
   162          }
   163      };
   164  }]).
   165  
   166  // side bar with dynamic table of contents
   167  directive('tableOfContents', ['$routeParams', 'toc',
   168      function($routeParams, toc) {
   169          var speed = 250;
   170          return {
   171              restrict: 'A',
   172              templateUrl: '/static/partials/toc.html',
   173              link: function(scope, elm) {
   174                  scope.toc = toc;
   175                  scope.params = $routeParams;
   176  
   177                  scope.toggleLesson = function(id) {
   178                      var l = $('#toc-l-' + id + ' .toc-page');
   179                      l[l.css('display') == 'none' ? 'slideDown' : 'slideUp']();
   180                  };
   181  
   182                  scope.$watch(function() {
   183                      return scope.params.lessonId + scope.params.lessonId;
   184                  }, function() {
   185                      $('.toc-lesson:not(#toc-l-' + scope.params.lessonId + ') .toc-page').slideUp(speed);
   186                  });
   187  
   188                  scope.hideTOC = function(fullScreenOnly) {
   189                      var fullScreen = elm.find('.toc').width() == $(window).width();
   190                      if (fullScreenOnly && !fullScreen) {
   191                          return;
   192                      }
   193                      $('.toc').toggle('slide', {
   194                          direction: 'right'
   195                      }, speed);
   196                      $('#editor-container').show();
   197                  };
   198              }
   199          };
   200      }
   201  ]).
   202  
   203  directive('feedbackButton', ['i18n', function(i18n) {
   204      return {
   205          restrict: 'A',
   206          templateUrl: '/static/partials/feedback-button.html',
   207          link: function(scope, elm, attrs) {
   208              scope.feedbackMessage = i18n.l('submit-feedback');
   209  
   210              elm.on('click', function() {
   211                  var context = window.location.pathname === '/list'
   212                      ? '/list'
   213                      : '/' + scope.params.lessonId + '/' + scope.params.pageNumber;
   214  	        context = window.location.protocol + '//' + window.location.host + context;
   215                  var title = i18n.l('issue-title');
   216                  var body = i18n.l('context') + ': '+ context + '\n\n'+ i18n.l('issue-message');
   217                  var url = 'https://' + i18n.l('github-repo') + '/issues/new'
   218                      + '?title=' + encodeURIComponent(title)
   219                      + '&body=' + encodeURIComponent(body);
   220                  window.open(url);
   221              });
   222          }
   223      };
   224  }]);