// Generated by CoffeeScript 1.10.0
(function() {
  angular.module('ajenti.dashboard', ['core']);

  angular.module('ajenti.dashboard').run(function(customization) {
    return customization.plugins.dashboard = {
      allowMove: true,
      allowRemove: true,
      allowConfigure: true,
      allowAdd: true,
      defaultConfig: {
        widgetsLeft: [
          {
            id: 'w1',
            typeId: 'hostname'
          }, {
            id: 'w2',
            typeId: 'cpu'
          }, {
            id: 'w3',
            typeId: 'loadavg'
          }
        ],
        widgetsRight: [
          {
            id: 'w4',
            typeId: 'uptime'
          }, {
            id: 'w5',
            typeId: 'memory'
          }
        ]
      }
    };
  });

}).call(this);

// Generated by CoffeeScript 1.10.0
(function() {
  angular.module('core').config(function($routeProvider) {
    return $routeProvider.when('/view/dashboard', {
      templateUrl: '/dashboard:resources/partial/index.html',
      controller: 'DashboardIndexController'
    });
  });

}).call(this);

// Generated by CoffeeScript 1.10.0
(function() {
  angular.module('ajenti.dashboard').controller('DashboardIndexController', function($scope, $interval, gettext, notify, pageTitle, customization, messagebox, dashboard, config) {
    pageTitle.set(gettext('Dashboard'));
    $scope.ready = false;
    $scope._ = {};
    dashboard.getAvailableWidgets().then(function(data) {
      var i, len, results, w;
      $scope.availableWidgets = data;
      $scope.widgetTypes = {};
      results = [];
      for (i = 0, len = data.length; i < len; i++) {
        w = data[i];
        results.push($scope.widgetTypes[w.id] = w);
      }
      return results;
    });
    $scope.addWidget = function(index, widget) {
      widget = {
        id: Math.floor(Math.random() * 0x10000000).toString(16),
        typeId: widget.id
      };
      $scope.userConfig.dashboard.tabs[index].widgetsLeft.push(widget);
      return $scope.save().then(function() {
        if (w.config_template) {
          $scope.configureWidget(widget);
        }
        return $scope.refresh();
      });
    };
    config.getUserConfig().then(function(userConfig) {
      var base, updateInterval;
      $scope.userConfig = userConfig;
      if ((base = $scope.userConfig).dashboard == null) {
        base.dashboard = customization.plugins.dashboard.defaultConfig;
      }
      if (!$scope.userConfig.dashboard.tabs) {
        $scope.userConfig.dashboard.tabs = [
          {
            name: 'Home',
            width: 2,
            widgetsLeft: $scope.userConfig.dashboard.widgetsLeft,
            widgetsRight: $scope.userConfig.dashboard.widgetsRight
          }
        ];
        delete $scope.userConfig.dashboard['widgetsLeft'];
        delete $scope.userConfig.dashboard['widgetsRight'];
      }
      updateInterval = $interval(function() {
        return $scope.refresh();
      }, 1000);
      return $scope.$on('$destroy', function() {
        return $interval.cancel(updateInterval);
      });
    });
    $scope.onSort = function() {
      return $scope.save();
    };
    $scope.refresh = function() {
      var i, j, len, len1, ref, ref1, rq, tab, widget;
      rq = [];
      ref = $scope.userConfig.dashboard.tabs;
      for (i = 0, len = ref.length; i < len; i++) {
        tab = ref[i];
        ref1 = tab.widgetsLeft.concat(tab.widgetsRight);
        for (j = 0, len1 = ref1.length; j < len1; j++) {
          widget = ref1[j];
          rq.push({
            id: widget.id,
            typeId: widget.typeId,
            config: widget.config || {}
          });
        }
      }
      return dashboard.getValues(rq).then(function(data) {
        var k, len2, resp, results;
        $scope.ready = true;
        results = [];
        for (k = 0, len2 = data.length; k < len2; k++) {
          resp = data[k];
          results.push($scope.$broadcast('widget-update', resp.id, resp.data));
        }
        return results;
      });
    };
    $scope.addTab = function(index) {
      return messagebox.prompt(gettext('New name')).then(function(msg) {
        if (!msg.value) {
          return;
        }
        $scope.userConfig.dashboard.tabs.push({
          widgetsLeft: [],
          widgetsRight: [],
          name: msg.value
        });
        return $scope.save();
      });
    };
    $scope.removeTab = function(index) {
      return $scope.userConfig.dashboard.tabs.splice(index, 1);
    };
    $scope.renameTab = function(index) {
      var tab;
      tab = $scope.userConfig.dashboard.tabs[index];
      return messagebox.prompt(gettext('New name'), tab.name).then(function(msg) {
        if (!msg.value) {
          return;
        }
        tab.name = msg.value;
        return $scope.save();
      });
    };
    $scope.configureWidget = function(widget) {
      if (widget.config == null) {
        widget.config = {};
      }
      return $scope.configuredWidget = widget;
    };
    $scope.saveWidgetConfig = function() {
      $scope.save().then(function() {
        return $scope.refresh();
      });
      return $scope.configuredWidget = null;
    };
    $scope.removeWidget = function(tab, widget) {
      tab.widgetsLeft.remove(widget);
      tab.widgetsRight.remove(widget);
      return $scope.save();
    };
    return $scope.save = function() {
      return config.setUserConfig($scope.userConfig);
    };
  });

}).call(this);

// Generated by CoffeeScript 1.10.0
(function() {
  angular.module('ajenti.dashboard').controller('CPUWidgetController', function($scope) {
    return $scope.$on('widget-update', function($event, id, data) {
      var i, len, x;
      if (id !== $scope.widget.id) {
        return;
      }
      $scope.avg = 0;
      $scope.cores = 0;
      for (i = 0, len = data.length; i < len; i++) {
        x = data[i];
        $scope.avg += x / data.length;
        if (x > 0) {
          $scope.cores += 1;
        }
      }
      $scope.avgPercent = Math.floor($scope.avg * 100);
      return $scope.values = data;
    });
  });

}).call(this);

// Generated by CoffeeScript 1.10.0
(function() {
  angular.module('ajenti.dashboard').controller('HostnameWidgetController', function($scope) {
    return $scope.$on('widget-update', function($event, id, data) {
      if (id !== $scope.widget.id) {
        return;
      }
      return $scope.hostname = data;
    });
  });

}).call(this);

// Generated by CoffeeScript 1.10.0
(function() {
  angular.module('ajenti.dashboard').controller('LoadAverageWidgetController', function($scope) {
    return $scope.$on('widget-update', function($event, id, data) {
      if (id !== $scope.widget.id) {
        return;
      }
      return $scope.load = data;
    });
  });

}).call(this);

// Generated by CoffeeScript 1.10.0
(function() {
  angular.module('ajenti.dashboard').controller('MemoryWidgetController', function($scope) {
    return $scope.$on('widget-update', function($event, id, data) {
      if (id !== $scope.widget.id) {
        return;
      }
      $scope.used = data.used;
      $scope.total = data.total;
      return $scope.usage = Math.floor(100 * $scope.used / $scope.total);
    });
  });

}).call(this);

// Generated by CoffeeScript 1.10.0
(function() {
  angular.module('ajenti.dashboard').controller('UptimeWidgetController', function($scope) {
    return $scope.$on('widget-update', function($event, id, data) {
      if (id !== $scope.widget.id) {
        return;
      }
      return $scope.uptime = data;
    });
  });

}).call(this);

// Generated by CoffeeScript 1.10.0
(function() {
  angular.module('ajenti.dashboard').service('dashboard', function($http, $q) {
    this.getAvailableWidgets = function() {
      var q;
      q = $q.defer();
      $http.get("/api/dashboard/widgets").success(function(data) {
        return q.resolve(data);
      }).error(function(err) {
        return q.reject(err);
      });
      return q.promise;
    };
    this.getValues = function(data) {
      var q;
      q = $q.defer();
      $http.post("/api/dashboard/get-values", data, {
        ignoreLoadingBar: true
      }).success(function(data) {
        return q.resolve(data);
      }).error(function(err) {
        return q.reject(err);
      });
      return q.promise;
    };
    return this;
  });

}).call(this);

