Skip to content

Instantly share code, notes, and snippets.

@NikitaObukhov
Created December 25, 2015 10:21
Show Gist options
  • Save NikitaObukhov/d22071cc2c48856bc753 to your computer and use it in GitHub Desktop.
Save NikitaObukhov/d22071cc2c48856bc753 to your computer and use it in GitHub Desktop.
jQuery(function ($) {
var app = angular.module('Journal', ['med_rest', 'med_stdlib', 'ui.date',
'med_component_norms', 'med_component_odin_s', 'anguFixedHeaderTable', 'ngDialog']);
app.controller('JournalCtrl', function ($scope, $http, $q, RestAPI, Restangular, ngDialog) {
var stash = [];
// DATA
$scope.months = [];
$scope.journal = {};
$scope.employees = [];
$scope.preparats = [];
$scope.selectedMonth = null;
$scope.selectedEmployees = [];
$scope.selectedPreparats = [];
$scope.Math = Math;
$scope.show_settings = 0;
$scope.selected_employee = null;
$scope.selected_day_id = null;
$scope.getWorks = function(journal, employee_id, prep_index, day) {
var prep_id = journal.preparats[prep_index].id;
var works =
employee_id in journal.works
&& prep_id in journal.works[employee_id]
&& day in journal.works[employee_id][prep_id]
? journal.works[employee_id][prep_id][day]
: null;
if (null === works) {
journal[works][employee_id][prep_id][day] = 0;
}
return works === null ? null : _.reduce(works, function(result, work) {
return work.amount + result;
}, 0);
};
$scope.save = function() {
$('.journal-save').attr('disabled', 'disabled');
$http.post(Drupal.settings.basePath +'c/norms/naryad/saveWorks', stash)
.success(function (data) {
stash = [];
$('.journal-save').removeAttr('disabled');
});
};
$scope.stashChange = function(employee_id, preparat_id, day) {
stash.push($scope.journal.works[employee_id][preparat_id][day]);
stash = _.uniq(stash);
};
$scope.onKeyDown = function(e) {
var target = e.target;
var col = $(target).parents('td').index();
var row = $(target).parents('tr').index()+1;
switch(e.which) {
case 37: // left
col--;
var target2 = $('tr:eq('+row+') td:eq('+col+') input');
break;
case 38: // up
row--;
var target2 = $('tr:eq('+row+') td:eq('+col+') input');
break;
case 39: // right
col++;
var target2 = $('tr:eq('+row+') td:eq('+col+') input');
break;
case 40: // down
row++;
var target2 = $('tr:eq('+row+') td:eq('+col+') input');
break;
default: return; // exit this handler for other keys
}
if (target2.length) {
target2.focus();
}
};
$scope.onFocus = function(e, $child) {
var day_id = $child.day_id;
var employee = $child.$parent.employee;
$scope.selected_employee = employee;
$scope.selected_day_id = day_id;
};
$scope.openSettings = function() {
var scope = $scope.$new();
scope.months = $scope.months;
scope.employees = $scope.employees;
scope.preparats = $scope.preparats;
scope.selectedMonth = $scope.selectedMonth;
scope.selectedEmployees = $scope.selectedEmployees;
scope.selectedPreparats = $scope.selectedPreparats;
scope.selectedPreparat = $scope.journal.preparats.length ? $scope.journal.preparats[0] : null;
ngDialog.open({
template: 'journal-settings',
controller: 'JournalSettingsCtrl',
scope: scope
});
};
// INIT
(function () {
$scope.loading = true;
var promises = [];
promises.push(RestAPI.rest('norms').getList()
.then(function (norms) {
_.each(norms, function (norm) {
norm.preparat.id = parseInt(norm.preparat.id);
if (!_.findWhere($scope.preparats, {id: norm.preparat.id})) {
$scope.preparats.push(norm.preparat);
}
});
_.forEach($scope.journal.preparats, function(p) {
_.forEach($scope.preparats, function(p2) {
if (p2.id == p.id) {
$scope.selectedPreparats.push(p2);
return false;
}
});
});
})
);
promises.push(RestAPI.rest('odin_s_users').getList({department: 46})
.then(function (users) {
$scope.employees = users;
_.map($scope.employees, function (user) {
user.tabel_records = [];
});
_.forEach($scope.journal.employees, function(e) {
_.forEach($scope.employees, function(e2) {
if (e2.id == e.id) {
$scope.selectedEmployees.push(e2);
return false;
}
});
});
})
);
$q.all(promises).then(function () {
$scope.loading = false;
$scope.selectedMonth = _.find($scope.months, function(m) {
return m.spec == $scope.journal.month.spec;
});
/*
$('table#naryadsJournal').once('tableheader', function () {
$(this).data("drupal-tableheader", new Drupal.tableHeader(this));
});*/
});
})();
});
app.controller('JournalSettingsCtrl', function ($scope, $http, $q, RestAPI, Restangular, ngDialog) {
$scope.selectJournal = function() {
$http.post(Drupal.settings.basePath +'c/norms/naryad/selectJournal', {
month: $scope.selectedMonth.spec,
employees: _.map($scope.selectedEmployees, function(e) {
return e.id;
}),
preparats: [$scope.selectedPreparat.id]
})
.success(function (data) {
window.location.reload();
});
};
});
angular.bootstrap($('#naryadsJournalApp'), ['Journal']);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment