(function () { 'use strict'; angular .module('app', ['angular.drag.resize', 'ng-context-menu']) .controller('RoommapCtrl', RoommapCtrl); RoommapCtrl.$inject = ['$scope', 'smartRequest', 'Notification', '$http']; function RoommapCtrl($scope, smartRequest, Notification, $http) { $scope.places = []; $scope.tables = []; $scope.newTables = []; $scope.newTables2 = []; $scope.roommap = []; $scope.newElement = 0; $('#create_table').hide(); $('#create_place').show(); $('#places').show(); $('#background').hide(); $('#save_tables').hide(); $('#save_places').hide(); $('#back').hide(); $scope.getRoomMap = function () { smartRequest.get('v1/roommap?type=full', function (data) { $('#back').hide(); $('#save_tables').hide(); $('#save_places').hide(); $scope.roommap = data.roommap; Notification.success(data.status); }); }; $scope.getRoomMap(); $scope.alignTables = function () { $scope.tables.forEach(function (table) { var newArr = []; var goal = table.x; $scope.tables.forEach(function (el) { var middle = Math.abs(goal - el.x); if (middle <= 15) { newArr.push(el); } }); let xSum = 0; const len = newArr.length; let item = null; for (let i = 0; i < len; i++) { item = newArr[i]; xSum = item.x + xSum; } const averageX = Math.round(xSum / len); newArr.forEach(function (el) { $scope.tables.forEach((elem) => elem.id === el.id ? elem.x = averageX : elem.x = elem.x); }); }); $scope.tables.forEach(function (table) { var newArr = []; var goal = table.y; $scope.tables.forEach(function (el) { var middle = Math.abs(goal - el.y); if (middle <= 15) { newArr.push(el); } }); let ySum = 0; const len = newArr.length; let item = null; for (let i = 0; i < len; i++) { item = newArr[i]; ySum = item.y + ySum; } const averageY = Math.round(ySum / len); newArr.forEach(function (el) { $scope.tables.forEach((elem) => elem.id === el.id ? elem.y = averageY : elem.y = elem.y); }); }); $('#save_tables').show(); $('#back').show(); return $scope.tables; }; $scope.getTables = function (place_id) { $('#places').hide(); $('#background').show(); $('#create_table').show(); $('#create_place').hide(); $('#back').show(); $scope.maxWidth = 1024; $scope.windowHeight = window.innerHeight - (document.getElementById('view').clientHeight - document.getElementById('tables').clientHeight); $scope.screenHeight = screen.height; $scope.windowWidth = window.innerWidth; $scope.screenWidth = 69; smartRequest.get('v1/roommap?type=place&id=' + place_id, function (data) { $scope.place = data.place; $scope.place_id = data.place_id; Notification.success(data.status); $scope.left = document.getElementById('tables').getBoundingClientRect().left; $scope.top = parseInt(document.getElementById('view').clientHeight) - parseInt(document.getElementById('background_1').offsetHeight) - 2; $scope.percentWidth = document.getElementById('tables').clientWidth / 1024; $scope.percentHeight = document.getElementById('tables').clientHeight / 768; $scope.tables = data.tables; console.log($scope.tables); }); }; $scope.getCoords = function ($event, $index, table_id, place) { switch ($event.which) { case 1: var id = '#table_' + $index; $scope.element = $(id); $scope.parentOffset = $scope.element.offset(); var parentPos = document.getElementById('tables').getBoundingClientRect(), childrenPos = document.getElementById('table_' + $index).getBoundingClientRect(), relativePos = {}; relativePos.top = childrenPos.top - parentPos.top, relativePos.right = childrenPos.right - parentPos.right, relativePos.bottom = childrenPos.bottom - parentPos.bottom, relativePos.left = childrenPos.left - parentPos.left; $('#save_tables').show(); $scope.tables[$index].width = Math.round(document.getElementById('table_' + $index).offsetWidth / $scope.percentWidth), $scope.tables[$index].height = Math.round(document.getElementById('table_' + $index).offsetHeight / $scope.percentHeight), $scope.tables[$index].x = Math.round(relativePos.top / $scope.percentHeight), $scope.tables[$index].y = Math.round(relativePos.left / $scope.percentWidth); break; } }; $scope.deleteTable = function (table_id) { delete $scope.tables.splice(table_id, 1); $('#save_tables').show(); return $scope.tables; }; $scope.duplicateTable = function (index, place) { $scope.newTable = {}; $scope.lastEl = $scope.tables.length; $scope.newTable.id = $scope.tables[$scope.lastEl - 1].id + 1, $scope.newTable.place_id = $scope.tables[index].place_id, $scope.newTable.table_id = $scope.tables[$scope.lastEl - 1].table_id + 1, $scope.newTable.name = $scope.tables[index].name, $scope.newTable.width = $scope.tables[index].width, $scope.newTable.height = $scope.tables[index].height, $scope.newTable.x = $scope.tables[index].x + 50, $scope.newTable.y = $scope.tables[index].y + 50; $scope.tables.push($scope.newTable); $('#save_tables').show(); return $scope.tables; }; $scope.editTable = function ($index, table) { $('#table-edit').modal('show'); $scope.tableWidth = table.width; $scope.tableHeight = table.height; $scope.tableName = table.name; $scope.tableId = table.table_id; $scope.tableIndex = $index; $('#save_tables').show(); return $scope.tables; }; $scope.saveTable = function (index, id, name, width, height) { $('#table-edit').modal('toggle'); $scope.tables[index].width = width, $scope.tables[index].height = height, $scope.tables[index].name = name, $scope.tables[index].table_id = id; $('#save_tables').show(); return $scope.tables; }; $scope.back = function () { $('#places').show(); $('#background').hide(); $('#create_table').hide(); $('#create_place').show(); $('#save_tables').hide(); $('#back').hide(); $scope.tables = []; }; $scope.saveTables = function (place_id, tables) { console.log(tables); smartRequest.post('v1/roommap', { type: 'table', action: 'savetables', place_id: place_id, tables: JSON.stringify(tables), }, function (data) { $scope.tables = []; Notification.success(data.message); $scope.back(); }); }; $scope.deletePlace = function (index) { console.log(index); delete $scope.roommap.splice(index, 1); $('#save_places').show(); return $scope.roommap; }; $scope.editPlace = function (index) { }; $scope.savePlace = function (index) { }; $scope.savePlaces = function (places) { console.log(places); smartRequest.post('v1/roommap', { type: 'place', action: 'saveplaces', places: JSON.stringify(places), }, function (data) { Notification.success(data.message); /*$scope.getRoomMap();*/ }); }; } })();