bucket-edit.js 2.28 KB
Newer Older
nabil el mahiri committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
angular.module('bucket-edit', ['ui.bootstrap','ui.ace'])
.controller('BucketEditCtrl', function($scope, $http) {
 
  var copy = $scope.copy;




  var sortIcons = {
    's1':'fa-sort-amount-desc',
    's-1':'fa-sort-amount-asc'
  };


  if (!copy.projection) {
    copy.projection = [];
  }
  if (!copy.sort) {
    copy.sort = [];
  }
  if (!copy.params) {
    copy.params = [];
  }

  var displays = { 
    'block':{
      icon:'fa-cube',
      label:'Block',
      create:function(copy) {
        return {
          'type':'block',
          'overrides':[]
        }
      }
    }
  }

  function addField(field) {
    if (field.name && copy.projection.indexOf(field.name) === -1) {
      copy.projection.push(field.name);
    }
    field.name = null;
  }

  function addParam(param) {
    if (param.name && copy.params.indexOf(param.name) === -1) {
      copy.params.push(param.name);
    }
    param.name = null;
  }

  function addSort(sort) {
    copy.sort.push(sort.data);
    sort.data = null;
  }

	function addDisplay(type) {
    if (!copy.displays) {
      copy.displays = [];
    }
    copy.displays.push(displays[type].create(copy));
    console.log(copy.displays);
  }
 


  function doPreview() {
//    q = JSON.parse(copy.query);
//    console.log("query", q);
 
    var params = {q:copy.query};

    if (copy.projection.length > 0) {
      params.f = copy.projection.join(",");
    }

    $http.get("/" + copy.collection.name, {params:params})
      .success(function(data, status) {
        console.log(data);
        $scope.preview = {data: data, status: status};
      })
      .error(function() {
        console.error(arguments)
      }
    );
  }
 

  angular.extend($scope, {
    addDisplay:addDisplay,
    doPreview:doPreview,
    addParam:addParam,
    addField:addField,
    addSort:addSort,
    sortIcons:sortIcons,
    displays:displays
  });

  $scope.commands.preview = {
    label:"Preview",
    icon:'fa-eye',
    command:doPreview
  }

})
.directive('ngEnter', function () {
    return function (scope, element, attrs) {
        element.bind("keydown keypress", function (event) {
            if(event.which === 13) {
                scope.$apply(function (){
                    scope.$eval(attrs.ngEnter);
                });

                event.preventDefault();
            }
        });
    };
});