Advertisement
maygapixel

Untitled

Apr 25th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. extends layout
  2.  
  3. block content
  4.     link(href='assets/css/style.css', rel='stylesheet')
  5.     script(src='assets/js/crosshair-min.js')
  6.     div.page-content
  7.         div.content-wrapper
  8.             div
  9.                 div.col-md-4
  10.                     div.panel.panel-flat
  11.                         div.panel-heading
  12.                             h6.panel-title Plants
  13.                             div.heading-elements
  14.                                 div.heading-btn.btn-group
  15.                                     //-button.btn.btn-xs.btn-primary(type="button") Add
  16.                         div.container-fluid
  17.                             div.col-md-10
  18.                                 form.form-horizontal
  19.                                     div.form-group
  20.                                         label.control-label.col-lg-3 Tool :
  21.                                         div.col-lg-9
  22.                                             select.select.select2-hidden-accessible-.form-control(name="tool", tabindex="-1", aria-hidden="true")
  23.                                                 option(value=0) Seed
  24.                                     //- dynamic form
  25.                                     div
  26.                                         div.plant-row
  27.                                             div.form-group
  28.                                                 label.control-label.col-lg-3 Plant :
  29.                                                 div.col-lg-9
  30.                                                     select.select.plant_type.form-control(name="plant[]", id="plant[]", aria-hidden="true")
  31.                                                         option(value=0) Kale
  32.                                                         option(value=1) Napa cabbage
  33.                                                         option(value=2) Red Oak Lettuce
  34.                                             div.form-group
  35.                                                 label.control-label.col-lg-3 Position:
  36.                                                 div.col-lg-9
  37.                                                     input.form-control.position(type="text", name='position[]', id='position[]')
  38.                                             hr
  39.                                     //- \dynamic form
  40.  
  41.                                     div.form-group
  42.                                         div.col-lg-3
  43.                                         div.col-lg-9
  44.                                             button.btn.btn-default(type='button', id='add_plant')
  45.                                                 span Add Plant
  46.                                             button.btn.btn-primary(type='button', id='execute')
  47.                                                 span Execute
  48.                                     div.form-group
  49.                                         div.col-lg-3
  50.                                         div.col-lg-9
  51.                                            
  52.  
  53.                 div.col-md-8
  54.                     div.panel.panel-flat
  55.                         div.panel-heading
  56.                             h6.panel-title Raise Bed
  57.                         div.container-fluid
  58.                             div.col-md-12
  59.                                 div.row
  60.                                     div.col-md-12(id="status")
  61.                                 div.crosshair
  62.                                     img(id="demo-img" src="./assets/images/bed-plane.png" alt="")
  63.  
  64.     script.
  65.         var socket = io();
  66.         socket.on('timer', function (data) {  
  67.             $('#timer').html(data.current);
  68.         });
  69.         function plantVisual() {
  70.             $('.position').each(function(position) {
  71.                 var plants = $(this).val();
  72.                 if(!$.isNumeric(plants.substr(plants.length - 1))) {
  73.                     console.log(plants.substr(plants.length - 1));
  74.                     return;
  75.                 }
  76.                 var positions = [[0, 0],
  77.                                                  [170, 500], [170, 370], [170, 240], [170, 110],
  78.                                                  [320, 110], [320, 240], [320, 370], [320, 500],
  79.                                                  [470, 500], [470, 370], [470, 240], [470, 110],
  80.                                                  [620, 110], [620, 240], [620, 370], [620, 500]
  81.                                                  ];
  82.                 var plant_array = plants.split(',');
  83.                 var plant_type = $(this).parent().parent().parent().find('.plant_type');
  84.                 //console.log('type: ' + plant_type.val());
  85.                 $.each(plant_array, function( index, value ) {
  86.                     if($('#plant_'+value).data('type') != plant_type.val()) {
  87.                         $('img').remove('img[id=plant_'+value+']');
  88.                     }
  89.  
  90.                     if(value != '' && $('#plant_'+value).length == 0) {
  91.                         switch(parseInt(plant_type.val())) {
  92.                             case 0:
  93.                                 $('.crosshair').append('<img class="plants" data-id="'+value+'" data-type="0" id="plant_'+value+'" name="plant" src="./assets/images/spinach-512.png" style="left: '+positions[value][0]+'px; top: '+positions[value][1]+'px;">');
  94.                                 break;
  95.  
  96.                             case 1:
  97.                                 $('.crosshair').append('<img class="plants" data-id="'+value+'" data-type="1" id="plant_'+value+'" name="plant" src="./assets/images/cabbage.png" style="left: '+(positions[value][0]-5)+'px; top: '+(positions[value][1]-5)+'px;">');
  98.                                 break;
  99.  
  100.                             case 2:
  101.                                 $('.crosshair').append('<img class="plants" data-id="'+value+'" data-type="2" id="plant_'+value+'" name="plant" src="./assets/images/redoak.png" style="left: '+(positions[value][0]-5)+'px; top: '+(positions[value][1]-5)+'px;">');
  102.                                 break;
  103.                         }
  104.                     }
  105.                 });
  106.             });
  107.             //console.log(plant_array);
  108.             var plant_arr_global = $('.position').serializeArray();
  109.  
  110.             $('.plants').each(function(plant) {
  111.                 var current_id = $(this).data('id');
  112.                 var remove_permit = 0;
  113.                 //console.log('plant: ' + plant);
  114.                 $.each(plant_arr_global, function(key, value) {
  115.                     //console.log('global_arr : ' + value.value);
  116.                     var ArrayOfInts = value.value.split(',').map(Number);
  117.                     //console.log(ArrayOfInts);
  118.                     if(jQuery.inArray(current_id, ArrayOfInts) >= 0) {
  119.                     //if(value.value.indexOf(current_id.toString()) >= 0) {
  120.                         remove_permit += 1;
  121.                         //console.log('found: ' + current_id + ' - ' + remove_permit);
  122.                     }
  123.                 });
  124.  
  125.                 if(remove_permit == 0) {
  126.                     $('img').remove('#plant_'+current_id);
  127.                 }
  128.                 remove_permit = 0;     
  129.             });
  130.         }
  131.         $(document).on('keyup', '.position', function() {
  132.         //$('.position').keyup(function() {
  133.             plantVisual();
  134.         });
  135.  
  136.         $('#demo-img').crosshair({
  137.             callback: function(crosshair) {
  138.                 var pos_x = ((crosshair.coords.x-63)/1009)*800;
  139.                 var pos_y = ((crosshair.coords.y-63)/756)*600;
  140.                 pos_x = Math.min(Math.max(parseInt(pos_x), 0), 600);
  141.                 pos_y = Math.min(Math.max(parseInt(pos_y), 0), 600);
  142.                 pos_y = Math.min(Math.max(parseInt(500-pos_y), 0), 300);
  143.                 $('#status').html(pos_x+','+pos_y);
  144.                 console.log("Pixel coordinates: ("+(pos_x)+", "+(pos_y)+")");
  145.                 console.log("Relative position: X: "+crosshair.pct.x+"%, Y: "+crosshair.pct.y+"%");
  146.                 // 1008, 756
  147.                 //console.log(pos_x/10 + ', ' + pos_y/10);
  148.                 execute(pos_x/10, pos_y/10);
  149.             }
  150.         });
  151.  
  152.         $('img[name=plant]').css({'left': '126.201px', 'top': '91.9792px'});
  153.  
  154.         $('#add_plant').click(function(e) {
  155.             console.log('adding...');
  156.             var divNomeMail = $('.plant-row');
  157.             var sDivNomeMailHtml = divNomeMail.html();
  158.             var sInputGroupClasses = divNomeMail.attr('class');
  159.  
  160.             console.log(divNomeMail.next().length);
  161.  
  162.             //Gambiarra pra nao ficar criando mil inputs
  163.             //if(divNomeMail.next().length >= 1) return;
  164.  
  165.             divNomeMail.parent().append('<div class="plant-row"><div class="form-group"><label class="control-label col-lg-3">Plant :</label><div class="col-lg-9"><select aria-hidden="true" class="select form-control plant_type" name="plant[]" id="plant[]" tabindex="-1"><option value=0> Kale</option><option value=1> Napa cabbage</option><option value=2> Red Oak Lettuce</option></select></div></div><div class="form-group"><label class="control-label col-lg-3">Position:</label><div class="col-lg-9"><input class="form-control position" id="position[]" name="position[]" type="text"/></div></div><hr/></div>');
  166.             $('select').select2();
  167.         });
  168.  
  169.         $('#execute').click(function(e) {
  170.             //- var position = $('#position').val();
  171.             var positions = [];
  172.             var plants = $('select[name="plant[]"] option:selected');
  173.            
  174.             $('input[name="position[]"]').each(function(key, value) {
  175.                 var aValue = $(this).val();
  176.                 positions[key] = [plants[key]['value'], aValue];
  177.             });
  178.             console.log(JSON.stringify(positions));
  179.             console.log('--------------------');
  180.             //- return;
  181.             //- if(number > 18) {
  182.             //-     swal({
  183.             //-         title: 'Warning',
  184.             //-         text: number + ' is exceed number limit (18) ?',
  185.             //-         type: 'error',
  186.             //-         showConfirmButton: true,
  187.             //-         timer: 3000
  188.             //-     })
  189.             //-     return;
  190.             //- }
  191.             swal({
  192.                 title: 'Warning',
  193.                 text: 'Execute tree(s) ?',
  194.                 type: 'warning',
  195.                 showCancelButton: true,
  196.                 confirmButtonColor: '#3085d6',
  197.                 cancelButtonColor: '#d33',
  198.                 //confirmButtonText: 'Yes, execute it!'
  199.             }).then((result) => {
  200.                 if (result.value) {
  201.                     $.ajax({
  202.                         url: '/api/execute/plants',
  203.                         method: 'post',
  204.                         dataType: "json",
  205.                         data: {positions: JSON.stringify(positions)}
  206.                     }).done(function(res) {
  207.                         console.log('res: '+res.msg);
  208.                     });
  209.                     /*
  210.                     swal(
  211.                         'Deleted!',
  212.                         'Your file has been deleted.',
  213.                         'success'
  214.                     )
  215.                     */
  216.                 }
  217.             })
  218.         });
  219.  
  220.         function plant_execute() {
  221.             console.log('plainting ...');
  222.         }
  223.  
  224.         $(document).on("keypress", "form", function(event) {
  225.             if(event.keyCode === 13) {
  226.                 $('#execute').click();
  227.             }
  228.             return event.keyCode != 13;
  229.         });
  230.  
  231.         function execute(pos_x, pos_y) {
  232.             console.log(pos_x+', '+pos_y);
  233.             $.ajax({
  234.                 url: '/api/controls/coordinate',
  235.                 method: 'post',
  236.                 data: {pos_x: pos_x, pos_y: pos_y},
  237.             }).done(function(res) {
  238.                 console.log(res.msg);
  239.             });
  240.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement