Skip to content

Instantly share code, notes, and snippets.

@vistajess
Created March 26, 2019 19:52
Show Gist options
  • Save vistajess/9b4f970f3e68b4ba1061ad3c502ec398 to your computer and use it in GitHub Desktop.
Save vistajess/9b4f970f3e68b4ba1061ad3c502ec398 to your computer and use it in GitHub Desktop.
(function () {
'use strict';
angular.module('app').controller('plansController', plansController);
plansController.$inject = ['$rootScope', '$scope', '$http', '$q', '$document', 'appConstants', '$timeout', '$state', '$modal', '$stateParams', 'uiGridConstants', 'productionPlansService', 'localStorageService'];
function plansController($rootScope, $scope, $http, $q, $document, appConstants, $timeout, $state, $modal, $stateParams, uiGridConstants, productionPlansService, localStorageService) {
$document[0].title = $state.current.title;
var vm = this;
vm.appConstants = appConstants;
vm.showAllData = showAllData;
vm.onAddClick = onAddClick;
vm.onEditClick = onEditClick;
vm.onCopyClick = onCopyClick;
vm.onDeleteClick = onDeleteClick;
vm.onNew = onNew;
vm.data = [];
vm.lotNumberOpt = [];
vm.productTypesOpt = [];
vm.PartNumberOpt = [];
vm.RecipeOpt = [];
vm.StatusOpt = [];
vm.selectedProductType = [];
vm.selectedPartNumber = [];
vm.selectedRecipe = [];
vm.selectedStatus = [];
vm.page = 1;
vm.rowsPerPage = 60;
vm.getFirstPageData = getFirstPageData;
vm.getMoreData = getMoreData;
vm.gridOptions = {
rowHeight: 60,
headerRowHeight: 40,
infiniteScrollRowsFromEnd: 30,
infiniteScrollDown: true,
enableRowSelection: false,
enableVerticalScrollbar: 1,
enableHorizontalScrollbar: 0,
enableColumnResizing: true,
enableColumnMenus: false,
enableGridMenu: false,
onRegisterApi: function (gridApi) {
vm.gridApi = gridApi;
},
columnDefs: [
{
name: 'ProductionPlanName',
displayName: 'Name',
cellTemplate: '<a ng-click="grid.appScope.onEditClick(row.entity)" href=""><div class="text-green ui-grid-cell-contents">{{COL_FIELD}}</div></a>'
},
{
name: 'CreatedDateTime',
displayName: 'Created Date',
cellTemplate: '<a ng-click="grid.appScope.onEditClick(row.entity)" href=""><div class="text-green ui-grid-cell-contents"><span>{{COL_FIELD| date:\'MM/dd/yyyy h:mm a\'}}</span></div></a>'
},
{
name: 'FinalProductLotNumber',
displayName: 'Lot Number',
cellTemplate: '<a ng-click="grid.appScope.onEditClick(row.entity)" href=""><div class="ui-grid-cell-contents">{{COL_FIELD}}</div></a>'
},
{
name: 'Quantity',
displayName: 'Quantity',
cellTemplate: '<a ng-click="grid.appScope.onEditClick(row.entity)" href=""><div class="ui-grid-cell-contents"><span>{{COL_FIELD}}</span></div></a>'
},
{
name: 'ProductType',
displayName: 'Product Type',
cellTemplate: '<a ng-click="grid.appScope.onEditClick(row.entity)" href=""><div class="ui-grid-cell-contents">{{COL_FIELD}}</div></a>'
},
{
name: 'FinalProductPartNumber',
displayName: 'Part Number',
cellTemplate: '<a ng-click="grid.appScope.onEditClick(row.entity)" href=""><div class="ui-grid-cell-contents">{{COL_FIELD}}</div></a>'
},
{
name: 'Recipe',
displayName: 'Recipe',
cellTemplate: '<a ng-click="grid.appScope.onEditClick(row.entity)" href=""><div class="ui-grid-cell-contents">{{COL_FIELD}}</div></a>'
},
{
name: 'CompletedDateTime',
displayName: 'Status',
cellTemplate: '<a ng-click="grid.appScope.onEditClick(row.entity)" href=""><div class="ui-grid-cell-contents">{{COL_FIELD ? "Closed":"Open"}}</div></a>'
},
{
field: 'editDelete',
displayName: '',
visible: true,
width: 120,
enableSorting: false,
cellTemplate:
"<span class='editDeleteCell'>" +
"<a ng-click='grid.appScope.onEditClick(row.entity)'><i title='Edit Production Plan' icon-class-update-hover data-icon='ico-edit' class='hide-on-hover ico-edit'></i></a>" +
"</span>"
}
]
};
vm.gridOptions.appScopeProvider = vm;
vm.gridOptions.onRegisterApi = onGridRegisterApi;
function onNew() {
$state.go('user.plandetail');
}
function onAddClick(row) {
console.log("onAddClick" + row);
}
function onEditClick(row) {
console.log("onEditClick" + row);
localStorageService.set('planToEdit', row);
$state.go('user.plandetail', { ProductionPlanID: row.ProductionPlanID });
}
function onCopyClick(row) {
console.log("onEditClick" + row);
}
function onDeleteClick(row) {
console.log("onEditClick" + row);
}
function onGridRegisterApi(gridApi) {
vm.gridApi = gridApi;
angular.forEach(gridApi.grid.options.columnDefs, function (val) {
val.sortDirectionCycle = [uiGridConstants.ASC, uiGridConstants.DESC];
if (val.name === $stateParams.sort)
val.sort = { direction: $stateParams.order === "asc" ? uiGridConstants.ASC : uiGridConstants.DESC };
});
gridApi.infiniteScroll.on.needLoadMoreData($scope, vm.getMoreData);
gridApi.core.on.sortChanged($scope, function (grid, sortColumns) {
var sortName, orderDirection;
if (sortColumns && sortColumns.length >= 1) {
var column = sortColumns[0];
sortName = column.name;
orderDirection = column.sort.direction;
$stateParams.sort = sortName;
$stateParams.order = orderDirection;
// getFirstPageData();
$state.go($state.current.name, $stateParams, { notify: false });
}
});
}
vm.onChange = function () {
if (vm.searchText) {
$stateParams.q = vm.searchText;
} else {
$stateParams.q = '';
}
$timeout(function () {
applyFilter();
}, 100);
};
function applyFilter() {
var data = [];
angular.forEach(vm.data, function (val) {
var hasPartNumber = vm.selectedPartNumber.length === 0 || vm.selectedPartNumber.filter(e => e.id === val.FinalProductPartNumber).length > 0;
var hasProductType = vm.selectedProductType.length === 0 || vm.selectedProductType.filter(e => e.id === val.ProductType).length > 0;
var hasRecipe = vm.selectedRecipe.length === 0 || vm.selectedRecipe.filter(e => e.id === val.Recipe).length > 0;
var hasStatus = vm.selectedStatus.length === 0 || vm.selectedStatus.filter(e => e.id === (val.CompletedDateTime ? "Closed" : "Open")).length > 0;
var hasSearchText = _.isEmpty(vm.searchText) || vm.searchText &&
(_.contains(val.FinalProductLotNumber.toLowerCase(),vm.searchText.toLowerCase())||
_.contains(val.FinalProductPartNumber.toLowerCase(),vm.searchText.toLowerCase()) ||
_.contains(val.ProductionPlanName.toLowerCase(),vm.searchText.toLowerCase()));
if (hasPartNumber && hasProductType && hasSearchText && hasRecipe && hasStatus)
data.push(val);
});
vm.gridOptions.data = data;
}
function getProductionPlans(opts) {
if (opts) {
opts.page = vm.page;
opts.rows = vm.rowsPerPage;
vm.loading = true;
console.log("Loading Page: " + opts.page);
var _opts = angular.copy(opts);
$state.go($state.current.name, opts, { notify: false });
return new Promise(function (resolve, reject) {
productionPlansService.getProductionPlans(_opts, true).then(function (result) {
console.log("Page: " + _opts.page + " Loaded");
vm.loading = false;
vm.data = result;
angular.forEach(result, function (val) {
//ids are hardcoded for now since api does not return ids for this column
var productType = { id: val.ProductType , text: val.ProductType };
var lotNumber = { id: val.FinalProductLotNumber, text: val.FinalProductLotNumber };
var partNumber = { id: val.FinalProductPartNumber, text: val.FinalProductPartNumber };
var recipe = { id: val.Recipe, text: val.Recipe };
var calculatedStatus = val.CompletedDateTime ? "Closed" : "Open";
var status = { id: calculatedStatus, text: calculatedStatus };
if (vm.productTypesOpt.filter(e => e.id === productType.id).length <= 0) {
vm.productTypesOpt.push(productType);
} else {
// console.log("ignore product type" ,productType);
}
if (vm.lotNumberOpt.filter(e => e.id === lotNumber.id).length <= 0) {
vm.lotNumberOpt.push(lotNumber);
} else {
// console.log("ignore lot number" ,lotNumber);
}
if (vm.PartNumberOpt.filter(e => e.id === partNumber.id).length <= 0) {
vm.PartNumberOpt.push(partNumber);
} else {
// console.log("ignore part number", partNumber);
}
if (vm.RecipeOpt.filter(e => e.id === recipe.id).length <= 0) {
vm.RecipeOpt.push(recipe);
} else {
// console.log("ignore recipe", recipe);
}
if (vm.StatusOpt.filter(e => e.id === status.id).length <= 0) {
vm.StatusOpt.push(status);
} else {
// console.log("ignore status", status);
}
});
if (_opts.page === 1) {
vm.gridOptions.data = result;
} else {
vm.gridApi.infiniteScroll.saveScrollPercentage();
vm.gridOptions.data = result.concat(vm.gridOptions.data);
}
resolve(result);
return vm.gridApi.infiniteScroll.dataLoaded(false, result.length === vm.rowsPerPage);
}, function (error) {
console.log('Failed to getProductionPlans()', error);
reject(error);
return vm.gridApi.infiniteScroll.dataLoaded();
});
});
} else
return vm.gridApi.infiniteScroll.dataLoaded();
}
function getFirstPageData() {
vm.page = 1;
return new Promise(function (resolve, reject) {
console.log('getFirstPageData');
getProductionPlans($stateParams).then(function (result) {
console.log(result);
$timeout(function () {
//timeout needed to allow digest cycle to complete,and grid to finish ingesting the data
vm.gridApi.infiniteScroll.resetScroll(false, true);
});
resolve(result);
}, function(error) {
reject(error);
});
});
}
function getMoreData() {
//vm.page++;
//getTemplates($stateParams);
}
function showAllData() {
$stateParams.q = '';
vm.selectedProductType = [];
vm.selectedPartNumber = [];
vm.selectedRecipe = [];
vm.selectedStatus = [];
vm.searchText = "";
vm.getFirstPageData();
}
activate();
function activate() {
if ($stateParams.q) {
vm.searchText = $stateParams.q;
}
vm.selectedStatus = [{ "id": "Open", "text": "Open" }];
getFirstPageData().then(function(result) {
applyFilter();
});
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment