Sha256: ab9dc17b5d7d5882d86bb10b07739e418894ea613c3180a456f07ac0f439c182

Contents?: true

Size: 1.76 KB

Versions: 1

Compression:

Stored size: 1.76 KB

Contents

robeaux.factory('Widgets', function() {
  var service = {};

  var defaults = [
    {
      name: 'mindwave',
      custom: false,
      template_url: '/js/widgets/mindwave.html',
      script_url: '/js/widgets/mindwave.js',
      attrs: ['robot', 'device', 'event']
    },
    {
      name: 'attitude',
      custom: false,
      template_url: '/js/widgets/attitude.html',
      script_url: '/js/widgets/attitude.js',
      attrs: ['robot', 'device', 'event']
    }
  ];

  service.list = [];

  service.load = function() {
    if (localStorage['widgets']) {
      this.list = angular.fromJson(localStorage['widgets']);
    } else {
      this.list = defaults;
    }

    if (localStorage['activeWidgets']) {
      this.activeWidgets = angular.fromJson(localStorage['activeWidgets']);
    } else {
      this.activeWidgets = {};
    }
  };

  service.find = function(name) {
    var match;

    for (var i = 0; i < this.list.length; i++) {
      if (this.list[i].name === name) {
        match = this.list[i];
      }
    }

    return match;
  };

  service.save = function() {
    localStorage.setItem('widgets', angular.toJson(this.list));
    localStorage.setItem('activeWidgets', angular.toJson(this.activeWidgets));
  };

  service.add = function(name) {
    var widget = {
      name: name,
      template: "",
      script: "",
      attrs: [],
      custom: true
    };

    this.list.push(widget);
    this.save();
    return this.list[this.list.length - 1];
  };

  service.remove = function(name) {
    for (var i = 0; i < this.list.length; i++) {
      if (this.list[i].name === name) {
        this.list.splice(i, 1);
      }
    }

    this.save();
  };

  service.reset = function() {
    localStorage.removeItem('widgets');
    this.load();
  }

  service.load();

  return service;
});

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
robeaux-0.3.0 js/services/widgets.js