github.com/cayleygraph/cayley@v0.7.7/static/js/cayley_main.js (about)

     1  // Copyright 2014 The Cayley Authors. All rights reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  $(function() {
    16    s = null;
    17    group = null;
    18    Snap.load("/static/cayley.svg", function(d, err) {
    19      //Snap(105,65).append(d);
    20      s = Snap("#logo").append(d);
    21      svg = Snap("svg");
    22      var c = svg.selectAll("*")
    23      group = svg.group()
    24      group.add(c)
    25      scale = new Snap.Matrix();
    26      scale.scale(0.5);
    27      group.transform(scale)
    28    })
    29  
    30  	neutralColor = "#999999"
    31  	green = "#0F9D58"
    32  
    33  	stopAndReset = function () {
    34  		for (var i = 0; i < 19; i++) {
    35  			group[i].stop()
    36  		}
    37  		for (var i = 0; i < currentTimeouts.length; i++) {
    38  			clearTimeout(currentTimeouts[i])
    39  		}
    40  		currentTimeouts = []
    41  		group[17].attr({fill: neutralColor})
    42  		group[18].attr({fill: neutralColor})
    43  		for (var i = 1; i < 11; i++) {
    44  			group[i].attr({stroke: neutralColor})
    45  		}
    46  
    47  	}
    48  
    49  	currentTimeouts = []
    50  
    51  	flash = function(elem, start) {
    52  		currentTimeouts.push(
    53  			setTimeout(function() { elem.animate({stroke: green}, 1000)}, start * 1000))
    54  		currentTimeouts.push(
    55  			setTimeout(function() { elem.animate({stroke: neutralColor}, 1200)}, (start + 1) * 1000))
    56  	}
    57  
    58  	animate = function() {
    59  		group[17].animate({fill: green}, 1000)
    60  		currentTimeouts.push(
    61  			setTimeout(function() {group[17].animate({fill: neutralColor}, 1200)}, 1000))
    62  		flash(group[1], 0.5)
    63  		flash(group[8], 1.0)
    64  		flash(group[4], 1.5)
    65  		flash(group[10], 1.0)
    66  		flash(group[6], 1.5)
    67  		flash(group[2], 2.0)
    68  		currentTimeouts.push(
    69  			setTimeout(function() {group[18].animate({fill: green}, 1000)}, 2500))
    70  		currentTimeouts.push(
    71  			setTimeout(function() {group[18].animate({fill: neutralColor}, 1200)}, 3500))
    72  		currentTimeouts.push(
    73  			setTimeout(function() {group[17].animate({fill: green}, 1000)}, 3500))
    74  		currentTimeouts.push(
    75  			setTimeout(function() {group[17].animate({fill: neutralColor}, 1200)}, 4500))
    76  		flash(group[1], 4.0)
    77  		flash(group[7], 4.5)
    78  		flash(group[5], 5.0)
    79  		flash(group[9], 4.5)
    80  		flash(group[3], 5.0)
    81  		flash(group[2], 5.5)
    82  		currentTimeouts.push(
    83  			setTimeout(function() {group[18].animate({fill: green}, 1000)}, 6000))
    84  		currentTimeouts.push(
    85  			setTimeout(function() {group[18].animate({fill: neutralColor}, 1200, function() {
    86  				stopAndReset();
    87  				animate();
    88  		})}, 7000))
    89  	}
    90  
    91    if ($("#code").length != 0) {
    92      editor = CodeMirror.fromTextArea(document.getElementById("code"), {
    93        lineNumbers: true,
    94        matchBrackets: true,
    95        continueComments: "Enter",
    96        //        extraKeys: {"Ctrl-Q": "toggleComment"}
    97      });
    98    } else{
    99      editor = null;
   100    }
   101  
   102    var defaultQueryStrings = {
   103      "gizmo": "g.Emit('Hello World')",
   104      "mql": "[{\n  \"id\": \"Hello World\"\n}]",
   105      "graphql": "{\n  nodes(first: 10){\n    id\n  }\n}"
   106    }
   107  
   108    var getLastQueryStringFor = function(type) {
   109      if (typeof(Storage) !== "undefined") {
   110        return localStorage.getItem("cayleySavedQueries" + type)
   111      } else {
   112        return defaultQueryStrings[type]
   113      }
   114    }
   115  
   116    var switchTo = function(type) {
   117      if (type === "gizmo") { switchToGizmo()}
   118      if (type === "mql") { switchToMQL()}
   119      if (type === "graphql") { switchToGraphQL()}
   120      if (typeof(Storage) !== "undefined") {
   121        localStorage.setItem("cayleyQueryLang", type);
   122      }
   123      if (editor) {
   124        editor.setValue(getLastQueryStringFor(type))
   125      }
   126    }
   127  
   128    var switchToGizmo = function () {
   129      $("#selected-query-lang").html("Gizmo " + caretSpan)
   130      selectedQueryLanguage = "gizmo"
   131    }
   132  
   133    var switchToMQL = function() {
   134      $("#selected-query-lang").html("MQL" + caretSpan)
   135      selectedQueryLanguage = "mql"
   136    }
   137  
   138    var switchToGraphQL = function() {
   139      $("#selected-query-lang").html("GraphQL" + caretSpan)
   140      selectedQueryLanguage = "graphql"
   141    }
   142  
   143    selectedQueryLanguage = "gizmo"
   144    var caretSpan = " &nbsp <span class='caret'></span>"
   145  
   146    if (typeof(Storage) !== "undefined") {
   147      savedQueries = localStorage.getItem("cayleySavedQueriesgraphql");
   148      if (savedQueries === null) {
   149        for (var key in defaultQueryStrings) {
   150          localStorage.setItem("cayleySavedQueries" + key, defaultQueryStrings[key])
   151        }
   152      }
   153      lang = localStorage.getItem("cayleyQueryLang");
   154      if (lang !== null) {
   155        switchTo(lang)
   156      } else {
   157        switchTo("gizmo")
   158      }
   159    } else {
   160      switchTo("gizmo")
   161    }
   162  
   163  
   164    $("#gizmo-dropdown").click(function() {
   165      switchTo("gizmo")
   166    })
   167  
   168    $("#mql-dropdown").click(function() {
   169      switchTo("mql")
   170    })
   171  
   172    $("#graphql-dropdown").click(function() {
   173      switchTo("graphql")
   174    })
   175  });
   176  
   177