Created
November 23, 2015 21:30
-
-
Save SirajGadhia/226d04fd0271c5eb3848 to your computer and use it in GitHub Desktop.
AngularJS Controller Module (for Courses Admin ) - www.Siraj360.com/ng/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//http://www.siraj360.com/ng/ :: A sample single page application (SPA) developed with AngularJS 1.4.5 and Bootstrap 3.3.5. | |
(function () { | |
//debugger; | |
"use strict;" | |
angular.module('FD360').controller('CoursesAdminController', | |
['$scope', '$compile', '$modal', '$log', 'DataFactory', CoursesAdminController] | |
); | |
function CoursesAdminController($scope, $compile, $modal, $log, DataFactory) { | |
var vm = this; | |
vm.Employees = []; | |
vm.Courses = []; | |
vm.total_pending = 0; | |
vm.total_completed = 0; | |
vm.addCourse = function () | |
{ | |
var newCourse = { | |
"$id": "0", | |
"CourseID": "0", | |
"Name": "", | |
"Code": "", | |
"Description": "", | |
"EmployeeCourses": [], | |
"isSelect": false, | |
"isDisable": false | |
}; | |
var modalAddCourse = $modal.open({ | |
animation: $scope.animationsEnabled, | |
templateUrl: 'modalAddCourse.html', | |
controller: 'ModalAddCourseController', | |
size: '', | |
resolve: { | |
new_course: function () { | |
return newCourse; | |
} | |
} | |
}); | |
modalAddCourse.result.then(function (newCourse) { | |
var maxId = vm.Courses.length; | |
for (var x = 0; x < vm.Courses.length; x++) { | |
if (vm.Courses[x].CourseID > maxId) | |
{ | |
maxId = vm.Courses[x].CourseID | |
} | |
} | |
newCourse.$id = maxId + 1; | |
newCourse.CourseID = maxId + 1; | |
newCourse.deleteCourse = deleteCourse; | |
DataFactory.addCourse(newCourse); | |
getCourses(); | |
}, function () { | |
$log.info('Modal dismissed at: ' + new Date()); | |
}); | |
} | |
init(); | |
function init() { | |
getEmployees(); | |
getCourses(); | |
} | |
function getEmployees() { | |
var employees = DataFactory.getEmployees(); | |
vm.Employees = employees; | |
} | |
function editCourse(event) { | |
event.stopPropagation(); | |
var editCourse = this; | |
var modalEditCourse = $modal.open({ | |
animation: $scope.animationsEnabled, | |
templateUrl: 'modalEditCourse.html', | |
controller: 'ModalEditCourseController', | |
size: '', | |
resolve: { | |
edit_course: function () { | |
return angular.copy(editCourse); //editCourse; | |
} | |
} | |
}); | |
modalEditCourse.result.then(function (editCourse) { | |
// debugger; | |
DataFactory.editCourse(editCourse); | |
getCourses(); | |
}, function () { | |
$log.info('Modal dismissed at: ' + new Date()); | |
}); | |
} | |
function deleteCourse(event) { | |
event.stopPropagation(); | |
var deleteCourse = this; | |
var modalDeleteCourse = $modal.open({ | |
animation: $scope.animationsEnabled, | |
templateUrl: 'modalDeleteCourse.html', | |
controller: 'ModalDeleteCourseController', | |
size: '', | |
resolve: { | |
delete_course: function () { | |
return deleteCourse; | |
} | |
} | |
}); | |
modalDeleteCourse.result.then(function (deleteCourse) { | |
DataFactory.deleteCourse(deleteCourse); | |
getCourses(); | |
}, function () { | |
$log.info('Modal dismissed at: ' + new Date()); | |
}); | |
} | |
function getCourses() { | |
var courses = DataFactory.getCourses(); | |
var employees = vm.Employees; | |
for (var x = 0; x < courses.length; x++) { | |
courses[x].Pending = 0; | |
courses[x].Completed = 0; | |
courses[x].Employees = []; | |
courses[x].isShowEmployee = false; | |
courses[x].order = 'Name'; | |
courses[x].reverse = ''; | |
courses[x].toggleShowEmployee = function() | |
{ | |
var isShow = this.isShowEmployee | |
for (var i = 0; i < vm.Courses.length; i++) | |
{ | |
vm.Courses[i].isShowEmployee = false; | |
} | |
this.isShowEmployee = !isShow; | |
} | |
var employee = {}; | |
for (var i = 0; i < employees.length; i++) { | |
for (var j = 0; j < employees[i].EmployeeCourses.length; j++) { | |
if (employees[i].EmployeeCourses[j].CourseID == courses[x].CourseID) | |
{ | |
employee = employees[i]; | |
employee.CourseID = courses[x].CourseID; | |
employee.TotalCourses = employees[i].EmployeeCourses.length; | |
employee.TotalCompleted = employees[i].EmployeeCourses.filter( | |
function (value) {return (value.isComplete == true); } | |
).length; | |
employee.isComplete = employees[i].EmployeeCourses[j].isComplete; | |
courses[x].Employees.push(employee); | |
if(employees[i].EmployeeCourses[j].isComplete) | |
{ courses[x].Completed++ | |
vm.total_completed++ | |
} else | |
{ | |
courses[x].Pending++ | |
vm.total_pending++ | |
} | |
courses[x].deleteCourse = deleteCourse; | |
courses[x].editCourse = editCourse; | |
} | |
} | |
} | |
} | |
vm.Courses = courses | |
} | |
}; | |
})(); | |
//Download of full application at https://github.com/SirajGadhia/FD360-V2-AngularJS |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment