github.com/kaisawind/go-swagger@v0.19.0/docs/presentations/Swagger-golangsf_files/slides.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  var PERMANENT_URL_PREFIX = '/static/';
     6  
     7  var SLIDE_CLASSES = ['far-past', 'past', 'current', 'next', 'far-next'];
     8  
     9  var PM_TOUCH_SENSITIVITY = 15;
    10  
    11  var curSlide;
    12  
    13  /* ---------------------------------------------------------------------- */
    14  /* classList polyfill by Eli Grey
    15   * (http://purl.eligrey.com/github/classList.js/blob/master/classList.js) */
    16  
    17  if (typeof document !== "undefined" && !("classList" in document.createElement("a"))) {
    18  
    19  (function (view) {
    20  
    21  var
    22      classListProp = "classList"
    23    , protoProp = "prototype"
    24    , elemCtrProto = (view.HTMLElement || view.Element)[protoProp]
    25    , objCtr = Object
    26      strTrim = String[protoProp].trim || function () {
    27      return this.replace(/^\s+|\s+$/g, "");
    28    }
    29    , arrIndexOf = Array[protoProp].indexOf || function (item) {
    30      for (var i = 0, len = this.length; i < len; i++) {
    31        if (i in this && this[i] === item) {
    32          return i;
    33        }
    34      }
    35      return -1;
    36    }
    37    // Vendors: please allow content code to instantiate DOMExceptions
    38    , DOMEx = function (type, message) {
    39      this.name = type;
    40      this.code = DOMException[type];
    41      this.message = message;
    42    }
    43    , checkTokenAndGetIndex = function (classList, token) {
    44      if (token === "") {
    45        throw new DOMEx(
    46            "SYNTAX_ERR"
    47          , "An invalid or illegal string was specified"
    48        );
    49      }
    50      if (/\s/.test(token)) {
    51        throw new DOMEx(
    52            "INVALID_CHARACTER_ERR"
    53          , "String contains an invalid character"
    54        );
    55      }
    56      return arrIndexOf.call(classList, token);
    57    }
    58    , ClassList = function (elem) {
    59      var
    60          trimmedClasses = strTrim.call(elem.className)
    61        , classes = trimmedClasses ? trimmedClasses.split(/\s+/) : []
    62      ;
    63      for (var i = 0, len = classes.length; i < len; i++) {
    64        this.push(classes[i]);
    65      }
    66      this._updateClassName = function () {
    67        elem.className = this.toString();
    68      };
    69    }
    70    , classListProto = ClassList[protoProp] = []
    71    , classListGetter = function () {
    72      return new ClassList(this);
    73    }
    74  ;
    75  // Most DOMException implementations don't allow calling DOMException's toString()
    76  // on non-DOMExceptions. Error's toString() is sufficient here.
    77  DOMEx[protoProp] = Error[protoProp];
    78  classListProto.item = function (i) {
    79    return this[i] || null;
    80  };
    81  classListProto.contains = function (token) {
    82    token += "";
    83    return checkTokenAndGetIndex(this, token) !== -1;
    84  };
    85  classListProto.add = function (token) {
    86    token += "";
    87    if (checkTokenAndGetIndex(this, token) === -1) {
    88      this.push(token);
    89      this._updateClassName();
    90    }
    91  };
    92  classListProto.remove = function (token) {
    93    token += "";
    94    var index = checkTokenAndGetIndex(this, token);
    95    if (index !== -1) {
    96      this.splice(index, 1);
    97      this._updateClassName();
    98    }
    99  };
   100  classListProto.toggle = function (token) {
   101    token += "";
   102    if (checkTokenAndGetIndex(this, token) === -1) {
   103      this.add(token);
   104    } else {
   105      this.remove(token);
   106    }
   107  };
   108  classListProto.toString = function () {
   109    return this.join(" ");
   110  };
   111  
   112  if (objCtr.defineProperty) {
   113    var classListPropDesc = {
   114        get: classListGetter
   115      , enumerable: true
   116      , configurable: true
   117    };
   118    try {
   119      objCtr.defineProperty(elemCtrProto, classListProp, classListPropDesc);
   120    } catch (ex) { // IE 8 doesn't support enumerable:true
   121      if (ex.number === -0x7FF5EC54) {
   122        classListPropDesc.enumerable = false;
   123        objCtr.defineProperty(elemCtrProto, classListProp, classListPropDesc);
   124      }
   125    }
   126  } else if (objCtr[protoProp].__defineGetter__) {
   127    elemCtrProto.__defineGetter__(classListProp, classListGetter);
   128  }
   129  
   130  }(self));
   131  
   132  }
   133  /* ---------------------------------------------------------------------- */
   134  
   135  /* Slide movement */
   136  
   137  function hideHelpText() {
   138    $('#help').hide();
   139  };
   140  
   141  function getSlideEl(no) {
   142    if ((no < 0) || (no >= slideEls.length)) {
   143      return null;
   144    } else {
   145      return slideEls[no];
   146    }
   147  };
   148  
   149  function updateSlideClass(slideNo, className) {
   150    var el = getSlideEl(slideNo);
   151  
   152    if (!el) {
   153      return;
   154    }
   155  
   156    if (className) {
   157      el.classList.add(className);
   158    }
   159  
   160    for (var i in SLIDE_CLASSES) {
   161      if (className != SLIDE_CLASSES[i]) {
   162        el.classList.remove(SLIDE_CLASSES[i]);
   163      }
   164    }
   165  };
   166  
   167  function updateSlides() {
   168    if (window.trackPageview) window.trackPageview();
   169  
   170    for (var i = 0; i < slideEls.length; i++) {
   171      switch (i) {
   172        case curSlide - 2:
   173          updateSlideClass(i, 'far-past');
   174          break;
   175        case curSlide - 1:
   176          updateSlideClass(i, 'past');
   177          break;
   178        case curSlide:
   179          updateSlideClass(i, 'current');
   180          break;
   181        case curSlide + 1:
   182          updateSlideClass(i, 'next');
   183          break;
   184        case curSlide + 2:
   185          updateSlideClass(i, 'far-next');
   186          break;
   187        default:
   188          updateSlideClass(i);
   189          break;
   190      }
   191    }
   192  
   193    triggerLeaveEvent(curSlide - 1);
   194    triggerEnterEvent(curSlide);
   195  
   196    window.setTimeout(function() {
   197      // Hide after the slide
   198      disableSlideFrames(curSlide - 2);
   199    }, 301);
   200  
   201    enableSlideFrames(curSlide - 1);
   202    enableSlideFrames(curSlide + 2);
   203  
   204    updateHash();
   205  };
   206  
   207  function prevSlide() {
   208    hideHelpText();
   209    if (curSlide > 0) {
   210      curSlide--;
   211  
   212      updateSlides();
   213    }
   214  };
   215  
   216  function nextSlide() {
   217    hideHelpText();
   218    if (curSlide < slideEls.length - 1) {
   219      curSlide++;
   220  
   221      updateSlides();
   222    }
   223  };
   224  
   225  /* Slide events */
   226  
   227  function triggerEnterEvent(no) {
   228    var el = getSlideEl(no);
   229    if (!el) {
   230      return;
   231    }
   232  
   233    var onEnter = el.getAttribute('onslideenter');
   234    if (onEnter) {
   235      new Function(onEnter).call(el);
   236    }
   237  
   238    var evt = document.createEvent('Event');
   239    evt.initEvent('slideenter', true, true);
   240    evt.slideNumber = no + 1; // Make it readable
   241  
   242    el.dispatchEvent(evt);
   243  };
   244  
   245  function triggerLeaveEvent(no) {
   246    var el = getSlideEl(no);
   247    if (!el) {
   248      return;
   249    }
   250  
   251    var onLeave = el.getAttribute('onslideleave');
   252    if (onLeave) {
   253      new Function(onLeave).call(el);
   254    }
   255  
   256    var evt = document.createEvent('Event');
   257    evt.initEvent('slideleave', true, true);
   258    evt.slideNumber = no + 1; // Make it readable
   259  
   260    el.dispatchEvent(evt);
   261  };
   262  
   263  /* Touch events */
   264  
   265  function handleTouchStart(event) {
   266    if (event.touches.length == 1) {
   267      touchDX = 0;
   268      touchDY = 0;
   269  
   270      touchStartX = event.touches[0].pageX;
   271      touchStartY = event.touches[0].pageY;
   272  
   273      document.body.addEventListener('touchmove', handleTouchMove, true);
   274      document.body.addEventListener('touchend', handleTouchEnd, true);
   275    }
   276  };
   277  
   278  function handleTouchMove(event) {
   279    if (event.touches.length > 1) {
   280      cancelTouch();
   281    } else {
   282      touchDX = event.touches[0].pageX - touchStartX;
   283      touchDY = event.touches[0].pageY - touchStartY;
   284      event.preventDefault();
   285    }
   286  };
   287  
   288  function handleTouchEnd(event) {
   289    var dx = Math.abs(touchDX);
   290    var dy = Math.abs(touchDY);
   291  
   292    if ((dx > PM_TOUCH_SENSITIVITY) && (dy < (dx * 2 / 3))) {
   293      if (touchDX > 0) {
   294        prevSlide();
   295      } else {
   296        nextSlide();
   297      }
   298    }
   299  
   300    cancelTouch();
   301  };
   302  
   303  function cancelTouch() {
   304    document.body.removeEventListener('touchmove', handleTouchMove, true);
   305    document.body.removeEventListener('touchend', handleTouchEnd, true);
   306  };
   307  
   308  /* Preloading frames */
   309  
   310  function disableSlideFrames(no) {
   311    var el = getSlideEl(no);
   312    if (!el) {
   313      return;
   314    }
   315  
   316    var frames = el.getElementsByTagName('iframe');
   317    for (var i = 0, frame; frame = frames[i]; i++) {
   318      disableFrame(frame);
   319    }
   320  };
   321  
   322  function enableSlideFrames(no) {
   323    var el = getSlideEl(no);
   324    if (!el) {
   325      return;
   326    }
   327  
   328    var frames = el.getElementsByTagName('iframe');
   329    for (var i = 0, frame; frame = frames[i]; i++) {
   330      enableFrame(frame);
   331    }
   332  };
   333  
   334  function disableFrame(frame) {
   335    frame.src = 'about:blank';
   336  };
   337  
   338  function enableFrame(frame) {
   339    var src = frame._src;
   340  
   341    if (frame.src != src && src != 'about:blank') {
   342      frame.src = src;
   343    }
   344  };
   345  
   346  function setupFrames() {
   347    var frames = document.querySelectorAll('iframe');
   348    for (var i = 0, frame; frame = frames[i]; i++) {
   349      frame._src = frame.src;
   350      disableFrame(frame);
   351    }
   352  
   353    enableSlideFrames(curSlide);
   354    enableSlideFrames(curSlide + 1);
   355    enableSlideFrames(curSlide + 2);
   356  };
   357  
   358  function setupInteraction() {
   359    /* Clicking and tapping */
   360  
   361    var el = document.createElement('div');
   362    el.className = 'slide-area';
   363    el.id = 'prev-slide-area';
   364    el.addEventListener('click', prevSlide, false);
   365    document.querySelector('section.slides').appendChild(el);
   366  
   367    var el = document.createElement('div');
   368    el.className = 'slide-area';
   369    el.id = 'next-slide-area';
   370    el.addEventListener('click', nextSlide, false);
   371    document.querySelector('section.slides').appendChild(el);
   372  
   373    /* Swiping */
   374  
   375    document.body.addEventListener('touchstart', handleTouchStart, false);
   376  }
   377  
   378  /* Hash functions */
   379  
   380  function getCurSlideFromHash() {
   381    var slideNo = parseInt(location.hash.substr(1));
   382  
   383    if (slideNo) {
   384      curSlide = slideNo - 1;
   385    } else {
   386      curSlide = 0;
   387    }
   388  };
   389  
   390  function updateHash() {
   391    location.replace('#' + (curSlide + 1));
   392  };
   393  
   394  /* Event listeners */
   395  
   396  function handleBodyKeyDown(event) {
   397    // If we're in a code element, only handle pgup/down.
   398    var inCode = event.target.classList.contains("code");
   399  
   400    switch (event.keyCode) {
   401      case 72: // 'H' hides the help text
   402      case 27: // escape key
   403        if (!inCode) hideHelpText();
   404        break;
   405  
   406      case 39: // right arrow
   407      case 13: // Enter
   408      case 32: // space
   409        if (inCode) break;
   410      case 34: // PgDn
   411        nextSlide();
   412        event.preventDefault();
   413        break;
   414  
   415      case 37: // left arrow
   416      case 8: // Backspace
   417        if (inCode) break;
   418      case 33: // PgUp
   419        prevSlide();
   420        event.preventDefault();
   421        break;
   422  
   423      case 40: // down arrow
   424        if (inCode) break;
   425        nextSlide();
   426        event.preventDefault();
   427        break;
   428  
   429      case 38: // up arrow
   430        if (inCode) break;
   431        prevSlide();
   432        event.preventDefault();
   433        break;
   434    }
   435  };
   436  
   437  function addEventListeners() {
   438    document.addEventListener('keydown', handleBodyKeyDown, false);
   439  };
   440  
   441  /* Initialization */
   442  
   443  function addFontStyle() {
   444    var el = document.createElement('link');
   445    el.rel = 'stylesheet';
   446    el.type = 'text/css';
   447    el.href = '//fonts.googleapis.com/css?family=' +
   448              'Open+Sans:regular,semibold,italic,italicsemibold|Droid+Sans+Mono';
   449  
   450    document.body.appendChild(el);
   451  };
   452  
   453  function addGeneralStyle() {
   454    var el = document.createElement('link');
   455    el.rel = 'stylesheet';
   456    el.type = 'text/css';
   457    el.href = PERMANENT_URL_PREFIX + 'styles.css';
   458    document.body.appendChild(el);
   459  
   460    var el = document.createElement('meta');
   461    el.name = 'viewport';
   462    el.content = 'width=1100,height=750';
   463    document.querySelector('head').appendChild(el);
   464  
   465    var el = document.createElement('meta');
   466    el.name = 'apple-mobile-web-app-capable';
   467    el.content = 'yes';
   468    document.querySelector('head').appendChild(el);
   469  };
   470  
   471  function showHelpText() {
   472  };
   473  
   474  function handleDomLoaded() {
   475    slideEls = document.querySelectorAll('section.slides > article');
   476  
   477    setupFrames();
   478  
   479    addFontStyle();
   480    addGeneralStyle();
   481    addEventListeners();
   482  
   483    updateSlides();
   484  
   485    setupInteraction();
   486  
   487    if (window.location.hostname == "localhost" || window.location.hostname == "127.0.0.1" || window.location.hostname == "::1") {
   488      hideHelpText();
   489    }
   490  
   491    document.body.classList.add('loaded');
   492  };
   493  
   494  function initialize() {
   495    getCurSlideFromHash();
   496  
   497    if (window['_DEBUG']) {
   498      PERMANENT_URL_PREFIX = '../';
   499    }
   500  
   501    if (window['_DCL']) {
   502      handleDomLoaded();
   503    } else {
   504      document.addEventListener('DOMContentLoaded', handleDomLoaded, false);
   505    }
   506  }
   507  
   508  // If ?debug exists then load the script relative instead of absolute
   509  if (!window['_DEBUG'] && document.location.href.indexOf('?debug') !== -1) {
   510    document.addEventListener('DOMContentLoaded', function() {
   511      // Avoid missing the DomContentLoaded event
   512      window['_DCL'] = true
   513    }, false);
   514  
   515    window['_DEBUG'] = true;
   516    var script = document.createElement('script');
   517    script.type = 'text/javascript';
   518    script.src = '../slides.js';
   519    var s = document.getElementsByTagName('script')[0];
   520    s.parentNode.insertBefore(script, s);
   521  
   522    // Remove this script
   523    s.parentNode.removeChild(s);
   524  } else {
   525    initialize();
   526  }