Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
angular-gobox / src / services / box_repo.js
Size: Mime:
(function() {
    'use strict';

    angular.module('angularGobox').service('BoxRepo', BoxRepo);

    BoxRepo.$inject = ["$rootScope", "API", "CustomerboxStatus"];

    function BoxRepo($rootScope, API, CustomerboxStatus) {
        var service = this;
        service.$scope = $rootScope.$new();

        service.update = update;
        service.findById = findById;
        service.findByBoxName = findByBoxName;

        service.$scope.$watch(function() { return service.allBoxes; }, function() {
            sortBoxes();
        });

        // public

        function update() {
            return API.get('/customerboxes/').success(function(boxes) {
                service.allBoxes = boxes;
            });
        }

        function findById(id) {
            return _.find(service.allBoxes, { id: parseInt(id, 10) });
        }

        function findByBoxName(box_name) {
            return _.find(service.allBoxes, { box_name: box_name });
        }

        // private

        function sortBoxes() {
            resetCollections();

            _.each(service.allBoxes, function(box) {
                if (box.status === CustomerboxStatus.inStorage) {
                    service.inStorage.push(box);
                } else if (box.status === CustomerboxStatus.atCustomer) {
                    if (box.registered) service.atCustomer.registered.push(box);
                    else service.atCustomer.unregistered.push(box);
                } else if (box.status === CustomerboxStatus.pendingDropOff) {
                    service.pendingDropOff.push(box);
                } else if (box.status === CustomerboxStatus.pendingPickUp) {
                    service.pendingPickUp.push(box);
                }
            });
        }

        function resetCollections() {
            service.inStorage = [];
            service.atCustomer = {
                registered: [],
                unregistered: []
            };
            service.pendingDropOff = [];
            service.pendingPickUp = [];
        }

        function initialize() {
            service.allBoxes = [];
            resetCollections();
        }

        initialize();
    }
})();