inputbox.js

angular.module('homepage').
directive('inputbox', function() {
    return {
        controller: 'BoxController',
        restrict: 'E',
        replace: true,
        scope: {
            isDemo: '='
        },
        transclude: true,
        templateUrl: 'components/content-components/inputbox/inputbox.html'
    };
}).
controller('BoxController', function($localStorage, $scope, $state, $timeout, DataStoreSrvc, DateUtility, InputboxDataSrvc, GlobalDataService, MobileType, Server, TemplatePreprocessor, TemplateService) {
    function userdata() {
        // '_' object contains info used only on UI side
        return InputboxDataSrvc.moduleDefaultData();
    }

    // Keep only data properties that are having 'isHavingData' as true
    function filterData(data) {
        for (var key in data) {
            if (data.hasOwnProperty(key) && data[key]._.isHavingData) {
                // merge tag data within data fields
                data[key] = mergeTagData(data[key]);
                return data[key];
            }
        }
        return {};
    }

    function mergeTagData(fields) {
        for(var key in fields) {
            if (fields.hasOwnProperty(key)) {
                var field = fields[key];
                if(field.taglist !== undefined && field.text !== undefined) {
                    if(field.text !== '') {
                        field.taglist.push(field.text);
                    }

                    if(field.taglist.length > 0) {
                        var tagstext = field.taglist[0];
                        for(var i=1; i<field.taglist.length; i++) {
                            tagstext += ', ' + field.taglist[i];
                        }
                        field.tagstext = tagstext;
                    }
                }
            }
        }
        return fields;
    }

    function reset() {
        $scope.data = new userdata();
        $scope.currenttype = '';
    }

    function preprocessor(details) {
        if(details.type === 'CLDR' || details.type === 'HABT') {
            details.isLatest = true;
        }
        if(details.type === 'HLTH') {
            TemplatePreprocessor.removeEmptyWorkoutAndSet(details);
        }

        return details;
    }

    $scope.currenttype = '';
    $scope.data = new userdata();
    $scope.list = InputboxDataSrvc.templates;
    $scope.isMobile = MobileType.any();

    /**
     * Object to hold date for the date and time picker
     * @type {Object} selected Object containing 'date' attribute
     */
    $scope.selected = {};
    $scope.selected.date = new Date();

    $scope.select = function(type) {
        if(type === 'CLDR') return;

        switch (type) {
            case 'NOTE':
            $scope.data.note._.isHavingData = true;
            break;
            case 'LIST':
            $scope.data.list._.isHavingData = true;
            break;
            case 'ACTY':
            $scope.data.activity._.isHavingData = true;
            break;
            case 'MOOD':
            $scope.data.mood._.isHavingData = true;
            break;
            case 'HABT':
            $scope.data.habit._.isHavingData = true;
            break;
            case 'HLTH':
            $scope.data.fitness._.isHavingData = true;
            break;
            case 'FOOD':
            $scope.data.food._.isHavingData = true;
            break;
            case 'MNEY':
            $scope.data.finance._.isHavingData = true;
            break;
            case 'PLCS':
            $scope.data.place._.isHavingData = true;
            break;
            case 'MDIA':
            $scope.data.media._.isHavingData = true;
            break;
            case 'PEPL':
            $scope.data.people._.isHavingData = true;
            break;
            case 'CLDR':
            $scope.data.calendar._.isHavingData = true;
            break;
            default:
            $scope.currenttype = '';
        }
        $scope.currenttype = type;
        // Reset date maintaining the date reference
        $scope.selected.date = DateUtility.newDateSameRef($scope.selected.date);
    };

    $scope.open = function(page) {
        $state.transitionTo(page);
    }

    $scope.submit = function() {
        if(!$scope.isDemo) {
            var fieldsHavingData = filterData($scope.data);
            delete fieldsHavingData._;
            var details = {};
            details.type = fieldsHavingData.type;
            details.timestamp = new Date().getTime();
            details.latertime = new Date($scope.selected.date).getTime();
            details.data = fieldsHavingData;
            delete details.data.type;

            preprocessor(details);

            console.log(details);

            DataStoreSrvc.add(details);

            GlobalDataService.setSelectedDate(new Date());
            TemplateService.getTemplates();
            DataStoreSrvc.updateAll();

            reset();

            var data = {
                templates: [ details ]
            };
            Server.ibsubmit(data)
            .success(function (result) {
                console.log(result);
            })
            .error(function (message, status) {
                console.log(message);
                console.log(status);
                $localStorage.templates.push(details);
            });
       }
       else {
            reset();
       }
    };

    $scope.reset = function() {
        reset();
    };
}).
factory('InputboxDataSrvc', function () {
    var choices =
    [
        { index: 1, id: 'ABC1', label: 'One' },
        { index: 2, id: 'ABC2', label: 'Two' },
        { index: 3, id: 'ABC3', label: 'Three' },
        { index: 4, id: 'ABC4', label: 'Four' },
        { index: 5, id: 'ABC5', label: 'Five' },
        { index: 6, id: 'ABC6', label: 'Six' },
        { index: 7, id: 'ABC7', label: 'Seven' },
        { index: 8, id: 'ABC8', label: 'Eight' },
        { index: 9, id: 'ABC9', label: 'Nine' },
        { index: 10, id: 'ABC10', label: 'Ten' },
    ];

    function moduleDefaultData() {
        return {
            activity: {
                type: 'ACTY',
                _ : {
                    isHavingData: false,
                    isVisible: {},
                    names: choices
                },
                whom: {
                    text: '',
                    taglist: []
                }
            },
            calendar: {
                type: 'CLDR',
                _ : {
                    isHavingData: false,
                    isVisible: {},
                    places: choices,
                    attendees: choices
                },
                dates: {
                    START: {date: new Date(), isDateSet: false},
                    END: {date: new Date(), isDateSet: false}
                },
                times: {
                    START: {date: new Date(), isDateSet: false},
                    END: {date: new Date(), isDateSet: false}
                },
                attendees: {
                    text: '',
                    taglist: []
                },
                isChecked: false
            },
            finance: {
                type: 'MNEY',
                _ : {
                    isHavingData: false,
                    isVisible: {},
                    attendees: choices
                }
            },
            fitness: {
                type: 'HLTH',
                _ : {
                    isHavingData: false,
                    isVisible: {},
                    workouts: choices
                }
            },
            food: {
                type: 'FOOD',
                _ : {
                    isHavingData: false,
                    isVisible: {},
                    foodlist: choices
                }
            },
            list: {
                type: 'LIST',
                _ : {
                    list: [],
                    isHavingData: false,
                    isVisible: {}
                } ,
                tags: {
                    text: '',
                    taglist: []
                }
            },
            habit: {
                type: 'HABT',
                _ : {
                    isHavingData: false,
                    isVisible: {}
                },
                isChecked: false
            },
            mood: {
                type: 'MOOD',
                level: 999,
                _ : {
                    isHavingData: false,
                    isVisible: {}
                }
            },
            note: {
                type: 'NOTE',
                _ : {
                    isHavingData: false,
                    isVisible: {},
                    tags: choices
                },
                tags: {
                    text: '',
                    taglist: []
                }
            },
            place: {
                type: 'PLCS',
                _ : {
                    isHavingData: false,
                    isVisible: {},
                    places: choices
                },
                whom: {
                    text: '',
                    taglist: []
                }
            }
        };
    }

    // Templates without blank for flex based layouts
    var templates = [
        {
            name: 'note',
            type: 'NOTE',
            page: 'notes',
            src: 'img/icons/note.png',
            icon: 'file-text-o'
        }, 
        /*{
            name: 'list',
            type: 'LIST',
            src: 'img/icons/list.png',
            icon: 'list'
        }, 
        {
            name: 'mood',
            type: 'MOOD',
            src: 'img/icons/mood.png',
            icon: 'meh-o'
        }, 
        {
            name: 'activity',
            type: 'ACTY',
            src: 'img/icons/activity.png',
            icon: 'gamepad'
        },*/
        {
            name: 'food',
            type: 'FOOD',
            page: 'food',
            src: 'img/icons/food.png',
            icon: 'cutlery'
        }, 
        {
            name: 'workout',
            type: 'HLTH',
            page: 'health',
            src: 'img/icons/health.png',
            icon: 'heartbeat'
        }, 
        {
            name: 'money',
            type: 'MNEY',
            page: 'finance',
            src: 'img/icons/finance.png',
            icon: 'money'
        }, 
        {
            name: 'place',
            type: 'PLCS',
            page: 'places',
            src: 'img/icons/places.png',
            icon: 'map-marker'
        }, 
        /*{
            name: 'habit',
            type: 'HABT',
            src: 'img/icons/habit.png',
            icon: 'refresh'
        }, 
        {
            name: 'calendar',
            type: 'CLDR',
            src: 'img/icons/calendar.png',
            icon: 'calendar'
        }*/
    ];

    return {
        moduleDefaultData: moduleDefaultData,
        templates: templates
    };
});