Advertisement
Guest User

Untitled

a guest
Sep 16th, 2015
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import Ember from "ember";
  2.  
  3. export default Ember.Component.extend({
  4.     tagName: '',
  5.     cy: undefined,
  6.     root_node: undefined,
  7.     cytoscape_data: function() {
  8.         var ret = {
  9.             nodes: [],
  10.             edges: []
  11.         };
  12.  
  13.         var first = true;
  14.         var self = this;
  15.         this.get("model").forEach(function(node) {
  16.             if(first) {
  17.                 first = false;
  18.                 self.set("root_node", node.get("id"));
  19.  
  20.                 ret["nodes"].push({
  21.                     data: {
  22.                         id: node.get("id"),
  23.                         name: node.get("title"),
  24.                         tooltip: node.get("intro"),
  25.                         node_type: node.get("category").get("type")
  26.                     },
  27.                     position: {
  28.                         //x: node.get("position").get("x"),
  29.                         y: 0
  30.                     }
  31.                 });
  32.             } else {
  33.                 ret["nodes"].push({
  34.                     data: {
  35.                         id: node.get("id"),
  36.                         name: node.get("title"),
  37.                         tooltip: node.get("intro"),
  38.                         node_type: node.get("category").get("type")
  39.                     }/*,
  40.                     position: {
  41.                         x: node.get("position").get("x"),
  42.                         y: node.get("position").get("y")
  43.                     }*/
  44.                 });
  45.             }
  46.             node.get("node_parent").forEach(function(parent) {
  47.                 ret["edges"].push({
  48.                     data: {
  49.                         source: parent.get("id"),
  50.                         target: node.get("id")
  51.                     }
  52.                 });
  53.             });
  54.         });
  55.  
  56.         return ret;
  57.     }.property("model"),
  58.     didInsertElement: function() {
  59.         this._super();
  60.         Ember.run.scheduleOnce('afterRender', this, function(){
  61.             var self = this;
  62.             Ember.$(window).on("window:resize", function() {
  63.                 self.reposition_graph();
  64.             });
  65.             this.reset_graph_panel();
  66.             this.set("cy", cytoscape({
  67.                 container: Ember.$('#cy')[0],
  68.                 elements: this.get("cytoscape_data"),
  69.                 zoom: 1,
  70.                 pan: { x: 0, y: 0 },
  71.                 zoomingEnabled: false,
  72.                 userZoomingEnabled: false,
  73.                 panningEnabled: true,
  74.                 userPanningEnabled: false,
  75.                 autoungrabify: false,
  76.                 ready: function() {
  77.                     console.log("Graph is ready!");
  78.                     self.reset_graph_panel();
  79.                     self.style_graph();
  80.                     self.reposition_graph();
  81.                     self.setup_graph_actions();
  82.                     self.get("cy").forceRender();
  83.                 }
  84.             }));
  85.         });
  86.     },
  87.     reset_graph_panel: function() {
  88.         var graphPanel = document.getElementById("cy");
  89.  
  90.         graphPanel.style.width = "100%";
  91.         graphPanel.style.height = 800 + "px";
  92.     },
  93.     loadGraph: function(file) {
  94.         var json = null;
  95.         Ember.$.ajax({
  96.             'async': false,
  97.             'global': false,
  98.             'url': file,
  99.             'dataType': 'json',
  100.             'success': function (data) {
  101.                 json = data;
  102.             }
  103.         });
  104.         return json;
  105.     },
  106.     reposition_graph: function() {
  107.         var self = this;
  108.         this.get("cy").autolock(false);
  109.         var width = Ember.$("#cy").width();
  110.         var height = 800;
  111.         var options = {
  112.             name: 'breadthfirst',
  113.             roots: [self.get("root_node")],
  114.             boundingBox: {
  115.                 x1: 0,
  116.                 y1: 0,
  117.                 w: width,
  118.                 h: height
  119.             },
  120.             padding: 5,
  121.             spacingFactor: 1
  122.  
  123.             /*fit: true, // whether to fit the viewport to the graph
  124.             directed: false, // whether the tree is directed downwards (or edges can point in any direction if false)
  125.             padding: 30, // padding on fit
  126.             circle: false, // put depths in concentric circles if true, put depths top down if false
  127.             spacingFactor: 1.75, // positive spacing factor, larger => more space between nodes (N.B. n/a if causes overlap)
  128.             boundingBox: undefined, // constrain layout bounds; { x1, y1, x2, y2 } or { x1, y1, w, h }
  129.             avoidOverlap: true, // prevents node overlap, may overflow boundingBox if not enough space
  130.             roots: undefined, // the roots of the trees
  131.             maximalAdjustments: 0, // how many times to try to position the nodes in a maximal way (i.e. no backtracking)
  132.             animate: false, // whether to transition the node positions
  133.             animationDuration: 500, // duration of animation in ms if enabled
  134.             ready: undefined, // callback on layoutready
  135.             stop: undefined // callback on layoutstop*/
  136.         };
  137.         this.get("cy").layout(options);
  138.         this.get("cy").center();
  139.         options.boundingBox.height = this.get("cy").height();
  140.         //this.get("cy").layout(options);
  141.         this.get("cy").autolock(true);
  142.         this.get("cy").forceRender();
  143.     },
  144.     style_graph: function()  {
  145.         this.get("cy").style()
  146.             .selector('node')
  147.               .css({
  148.                 'width': '100px',
  149.                 'height': '100px',
  150.                 'border-color': 'gray',
  151.                 'border-width': 3,
  152.                 'border-opacity': 0.5
  153.               })
  154.             .selector('node[node_type = "uvod"]')
  155.               .css({
  156.                 'background-image': 'img/nodes/node-start.svg',
  157.                 'background-width': '103px',
  158.                 'background-height': '103px'
  159.               })
  160.             .selector('node[node_type = "small_p"]')
  161.               .css({
  162.                 'background-image': 'img/nodes/node-jelen.png',
  163.                 'background-width': '103px',
  164.                 'background-height': '103px'
  165.                 //'background-color': '#3885C6'
  166.               })
  167.             .selector('node[node_type = "small_t"]')
  168.               .css({
  169.                 'background-image': 'img/nodes/node-kufor.png',
  170.                 'background-width': '103px',
  171.                 'background-height': '103px'
  172.                 //'background-color': '#81E877'
  173.               })
  174.             .selector('node[node_type = "big"]')
  175.               .css({
  176.                 'background-image': 'img/nodes/node-sova.png',
  177.                 'background-width': '103px',
  178.                 'background-height': '103px'
  179.                 //'background-color': '#FFCC52'
  180.               })
  181.             .selector('node[node_type = "bonus"]')
  182.               .css({
  183.                 'background-image': 'img/nodes/node-zem.png',
  184.                 'background-width': '103px',
  185.                 'background-height': '103px'
  186.                 //'background-color': '#7A80FF'
  187.               })
  188.             .selector('edge')
  189.               .css({
  190.                 'width': 6,
  191.                                 'border-color': '#39393a',
  192.                 'target-arrow-shape': 'triangle',
  193.                 'opacity': 1
  194.               })
  195.             .selector(':selected')
  196.               .css({
  197.                 'background-color': 'orange',
  198.                 'opacity': 1
  199.               })
  200.             .selector('.faded')
  201.               .css({
  202.                 'opacity': 0.0,
  203.                 'text-opacity': 0
  204.             }).update();
  205.         //this.get("cy").style(style);
  206.     },
  207.     setup_graph_actions: function() {
  208.         var self = this;
  209.         this.get("cy").cxtmenu({
  210.             selector: 'node',
  211.             commands: [
  212.                 {
  213.                     content: 'Odevzdání',
  214.                     select: function(){
  215.                         self.sendAction("sub", this.id());
  216.                     }
  217.                 },
  218.  
  219.                 {
  220.                     content: 'Zadání',
  221.                     select: function(){
  222.                         self.sendAction('assign', this.id());
  223.                     }
  224.                 },
  225.  
  226.                 {
  227.                     content: 'Statistika',
  228.                     select: function(){
  229.                         self.sendAction('stat', this.id());
  230.                     }
  231.                 },
  232.  
  233.                 {
  234.                     content: 'Diskuze',
  235.                     select: function(){
  236.                         self.sendAction('discuss', this.id());
  237.                     }
  238.                 },
  239.  
  240.                 {
  241.                     content: 'Řešení',
  242.                     select: function(){
  243.                         self.sendAction('solution', this.id());
  244.                     }
  245.                 }
  246.             ]
  247.         });
  248.         this.get("cy").on('mousedown','node', function(event){
  249.             var target = event.cyTarget;
  250.             var id = target.data("id");
  251.             self.sendAction('assign', id);
  252.         });
  253.  
  254.         this.get("cy").on('mouseover','node', function(event){
  255.             var target = event.cyTarget;
  256.             var id = target.data("id");
  257.             var name = target.data("name");
  258.             var text = target.data("tooltip") + " Pro detaily podrž pravé tlačítko."; //TODO formatovanie textu
  259.  
  260.             var x=event.cyPosition.x;
  261.             var y=event.cyPosition.y;
  262.             self.get("cy").$('#' + id).qtip({
  263.                 content: {
  264.                     title: name,
  265.                     text: text
  266.                 },
  267.                 show: {
  268.                     event: false,
  269.                     ready: true,
  270.                     effect:false
  271.                 },
  272.                 position: {
  273.                     my: 'bottom center',
  274.                     at: 'top center',
  275.                     target: [x+3, y+3],
  276.                     adjust: {x:7,y:7}
  277.                 },
  278.                 hide: {
  279.                     fixed: true,
  280.                     event: false,
  281.                     inactive: 1000
  282.                 },
  283.                 style: {
  284.                     classes: 'qtip-bootstrap',
  285.                     tip: {
  286.                         width: 16,
  287.                         height: 8
  288.                     }
  289.                 }
  290.             });
  291.         });
  292.     }
  293. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement