(function () { 'use strict'; var app = angular.module('myforms'); // A controller for the myforms page app.controller('myAvailableFormsController', ['$scope', '$location', 'myFormsConfig', 'myFormsDataContext', 'myFormsService', 'userService', function ($scope, $location, config, myFormsDataContext, myFormsService, userService) { $scope.formIconBlank = config.formIconBlank; // Get the available forms when the scope is loaded if (!$scope.noUser) { getUser(); } else { getMyFormTemplates(); } // Get the forms available to the user function getUser() { userService.profile(function (user) { $scope.user = user; getMyFormTemplates(); }); } // Loaded data $scope.data = { loaded: false, formTemplates: [] } // Options for the forms $scope.options = { filters: { query: '' }, orderOptions: [ { name: 'Publish date', value: { predicate: 'lastPublishedOn', reverse: true }, }, { name: 'Form title', value: { predicate: 'title', reverse: false }, }, { name: 'Status', value: { predicate: 'inProgress', reverse: false }, } ] }; // Set the default options $scope.options.order = $scope.options.orderOptions[0]; // My forms functions $scope.fns = { // User selected a form template to complete completeForm: function (simpleTemplate) { var promise = simpleTemplate.inProgress ? myFormsDataContext.getForm(simpleTemplate.inProgressFormId) : myFormsDataContext.getFormTemplate(simpleTemplate.id).then(function (formTemplate) { if ($scope.user) { formTemplate.externalId = $scope.user.id; } return myFormsService.createFormFromTemplate(formTemplate); }); return promise.then(function (form) { myFormsService.setCurrentForm(form); $location.path('/completeform'); }); } } // Get all the form templates available to the user function getMyFormTemplates() { var promise = $scope.publicOnly ? myFormsDataContext.getPublicFormTemplates() : ($scope.user ? myFormsDataContext.getAvailableTemplates($scope.user.id) : myFormsDataContext.getAllAvailableTemplates() ); promise.then(function (formTemplates) { $scope.data.formTemplates = formTemplates; $scope.data.loaded = true; }); } //function link(scope, elem, attrs) { // scope.loadedyet = false; // scope.myAvailibleFormsLoaded = false; // scope.formIconBlank = config.formIconBlank; // scope.orderByPredicate = '-lastPublishedOn'; // scope.orderByChoice = 'Order by date'; // function getUser() { // user.getProfile().then(function (user) { // scope.user = user; // //get all forms available to the user // getMyFormTemplates(); // }); // } // if (typeof mobile != 'undefined') { // scope.isMobile = mobile; // } // if (!scope.noUser) { // getUser(); // } else { // getMyFormTemplates(); // } // //user selected a template to complete // scope.completeForm = function (simpleFormTemplate) { // var selectedForm; // if (simpleFormTemplate.inProgress) { // myFormsDataContext.getForm(simpleFormTemplate.inProgressFormId).then(function (theForm) { // // handleChoiceQuestionsOnLoading(theForm); // selectedForm = theForm; // scope.onFormSelected()(selectedForm); // }); // } else { // myFormsDataContext.getFormTemplate(simpleFormTemplate.id).then(function (formTemplate) { // if (scope.user) { // formTemplate.externalId = scope.user.id; // } // selectedForm = myFormsService.createFormFromTemplate(formTemplate); // //$location.path('/completeform/' + selectedForm.formTemplateId); // scope.onFormSelected()(selectedForm); // }); // } // } // //get all the form templates available to the user // function getMyFormTemplates() { // //if we are showing only public forms call the appropriate api // if (scope.publicOnly) { // myFormsDataContext.getPublicFormTemplates().then(function (formTemplates) { // scope.formTemplates = formTemplates; // }); // } // //we need to get all the forms available to the organisations that the user is a member of // else { // if (scope.user) { // myFormsDataContext.getAvailableTemplates(scope.user.id).then(function (formTemplates) { // scope.formTemplates = formTemplates; // scope.myAvailibleFormsLoaded = true; // }); // } else { // myFormsDataContext.getAllAvailableTemplates().then(function (formTemplates) { // scope.formTemplates = formTemplates; // scope.myAvailibleFormsLoaded = true; // }); // } // } // } //} }]); })();