From 4b61ce66f4f28f7cc7eb6edde31a2e2bd43b65a5 Mon Sep 17 00:00:00 2001 From: Elisha Shapiro Date: Fri, 28 Feb 2014 10:45:55 +0200 Subject: [PATCH 1/4] Code autoformat. --- .gitignore | 1 + script/lvl-drag-drop.js | 160 ++++++++++++++++++++-------------------- script/lvl-uuid.js | 37 +++++----- 3 files changed, 100 insertions(+), 98 deletions(-) diff --git a/.gitignore b/.gitignore index 4b92424..628deb0 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ server/node_modules +.idea/ \ No newline at end of file diff --git a/script/lvl-drag-drop.js b/script/lvl-drag-drop.js index a3190e8..89fd37c 100644 --- a/script/lvl-drag-drop.js +++ b/script/lvl-drag-drop.js @@ -1,86 +1,86 @@ var module = angular.module("lvl.directives.dragdrop", ['lvl.services']); -module.directive('lvlDraggable', ['$rootScope', 'uuid', function($rootScope, uuid) { - return { - restrict: 'A', - link: function(scope, el, attrs, controller) { - angular.element(el).attr("draggable", "true"); - - var id = angular.element(el).attr("id"); - if (!id) { - id = uuid.new() - angular.element(el).attr("id", id); - } - - el.bind("dragstart", function(e) { - e.dataTransfer.setData('text', id); +module.directive('lvlDraggable', ['$rootScope', 'uuid', function ($rootScope, uuid) { + return { + restrict: 'A', + link: function (scope, el, attrs, controller) { + angular.element(el).attr("draggable", "true"); - $rootScope.$emit("LVL-DRAG-START"); - }); - - el.bind("dragend", function(e) { - $rootScope.$emit("LVL-DRAG-END"); - }); - } - } - }]); + var id = angular.element(el).attr("id"); + if (!id) { + id = uuid.new() + angular.element(el).attr("id", id); + } -module.directive('lvlDropTarget', ['$rootScope', 'uuid', function($rootScope, uuid) { - return { - restrict: 'A', - scope: { - onDrop: '&' - }, - link: function(scope, el, attrs, controller) { - var id = angular.element(el).attr("id"); - if (!id) { - id = uuid.new() - angular.element(el).attr("id", id); - } - - el.bind("dragover", function(e) { - if (e.preventDefault) { - e.preventDefault(); // Necessary. Allows us to drop. - } - - e.dataTransfer.dropEffect = 'move'; // See the section on the DataTransfer object. - return false; - }); - - el.bind("dragenter", function(e) { - // this / e.target is the current hover target. - angular.element(e.target).addClass('lvl-over'); - }); - - el.bind("dragleave", function(e) { - angular.element(e.target).removeClass('lvl-over'); // this / e.target is previous target element. - }); - - el.bind("drop", function(e) { - if (e.preventDefault) { - e.preventDefault(); // Necessary. Allows us to drop. - } + el.bind("dragstart", function (e) { + e.dataTransfer.setData('text', id); - if (e.stopPropagation) { - e.stopPropagation(); // Necessary. Allows us to drop. - } - var data = e.dataTransfer.getData("text"); - var dest = document.getElementById(id); - var src = document.getElementById(data); - - scope.onDrop({dragEl: src, dropEl: dest}); - }); + $rootScope.$emit("LVL-DRAG-START"); + }); - $rootScope.$on("LVL-DRAG-START", function() { - var el = document.getElementById(id); - angular.element(el).addClass("lvl-target"); - }); - - $rootScope.$on("LVL-DRAG-END", function() { - var el = document.getElementById(id); - angular.element(el).removeClass("lvl-target"); - angular.element(el).removeClass("lvl-over"); - }); - } - } - }]); + el.bind("dragend", function (e) { + $rootScope.$emit("LVL-DRAG-END"); + }); + } + } +}]); + +module.directive('lvlDropTarget', ['$rootScope', 'uuid', function ($rootScope, uuid) { + return { + restrict: 'A', + scope: { + onDrop: '&' + }, + link: function (scope, el, attrs, controller) { + var id = angular.element(el).attr("id"); + if (!id) { + id = uuid.new() + angular.element(el).attr("id", id); + } + + el.bind("dragover", function (e) { + if (e.preventDefault) { + e.preventDefault(); // Necessary. Allows us to drop. + } + + e.dataTransfer.dropEffect = 'move'; // See the section on the DataTransfer object. + return false; + }); + + el.bind("dragenter", function (e) { + // this / e.target is the current hover target. + angular.element(e.target).addClass('lvl-over'); + }); + + el.bind("dragleave", function (e) { + angular.element(e.target).removeClass('lvl-over'); // this / e.target is previous target element. + }); + + el.bind("drop", function (e) { + if (e.preventDefault) { + e.preventDefault(); // Necessary. Allows us to drop. + } + + if (e.stopPropagation) { + e.stopPropagation(); // Necessary. Allows us to drop. + } + var data = e.dataTransfer.getData("text"); + var dest = document.getElementById(id); + var src = document.getElementById(data); + + scope.onDrop({dragEl: src, dropEl: dest}); + }); + + $rootScope.$on("LVL-DRAG-START", function () { + var el = document.getElementById(id); + angular.element(el).addClass("lvl-target"); + }); + + $rootScope.$on("LVL-DRAG-END", function () { + var el = document.getElementById(id); + angular.element(el).removeClass("lvl-target"); + angular.element(el).removeClass("lvl-over"); + }); + } + } +}]); diff --git a/script/lvl-uuid.js b/script/lvl-uuid.js index 1074dcf..c87b85a 100644 --- a/script/lvl-uuid.js +++ b/script/lvl-uuid.js @@ -1,25 +1,26 @@ var module; try { - module = angular.module('lvl.services'); + module = angular.module('lvl.services'); } catch (e) { - module = angular.module('lvl.services', []); + module = angular.module('lvl.services', []); } -module.factory('uuid', function() { - var svc = { - new: function() { - function _p8(s) { - var p = (Math.random().toString(16)+"000000000").substr(2,8); - return s ? "-" + p.substr(0,4) + "-" + p.substr(4,4) : p ; - } - return _p8() + _p8(true) + _p8(true) + _p8(); - }, - - empty: function() { - return '00000000-0000-0000-0000-000000000000'; - } - }; - - return svc; +module.factory('uuid', function () { + var svc = { + new: function () { + function _p8(s) { + var p = (Math.random().toString(16) + "000000000").substr(2, 8); + return s ? "-" + p.substr(0, 4) + "-" + p.substr(4, 4) : p; + } + + return _p8() + _p8(true) + _p8(true) + _p8(); + }, + + empty: function () { + return '00000000-0000-0000-0000-000000000000'; + } + }; + + return svc; }); \ No newline at end of file From 34df48e21c0c3c7e31f06b91a697c6560d430998 Mon Sep 17 00:00:00 2001 From: Elisha Shapiro Date: Fri, 28 Feb 2014 10:46:54 +0200 Subject: [PATCH 2/4] added semicols; --- script/lvl-drag-drop.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/script/lvl-drag-drop.js b/script/lvl-drag-drop.js index 89fd37c..7f1456f 100644 --- a/script/lvl-drag-drop.js +++ b/script/lvl-drag-drop.js @@ -8,7 +8,7 @@ module.directive('lvlDraggable', ['$rootScope', 'uuid', function ($rootScope, uu var id = angular.element(el).attr("id"); if (!id) { - id = uuid.new() + id = uuid.new(); angular.element(el).attr("id", id); } @@ -34,7 +34,7 @@ module.directive('lvlDropTarget', ['$rootScope', 'uuid', function ($rootScope, u link: function (scope, el, attrs, controller) { var id = angular.element(el).attr("id"); if (!id) { - id = uuid.new() + id = uuid.new(); angular.element(el).attr("id", id); } From dd6d910e8a5ab58da807dedab94391c10a2e8724 Mon Sep 17 00:00:00 2001 From: Elisha Shapiro Date: Fri, 28 Feb 2014 10:47:49 +0200 Subject: [PATCH 3/4] removed duplicate code. --- script/lvl-drag-drop.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/script/lvl-drag-drop.js b/script/lvl-drag-drop.js index 7f1456f..be9669f 100644 --- a/script/lvl-drag-drop.js +++ b/script/lvl-drag-drop.js @@ -61,9 +61,6 @@ module.directive('lvlDropTarget', ['$rootScope', 'uuid', function ($rootScope, u e.preventDefault(); // Necessary. Allows us to drop. } - if (e.stopPropagation) { - e.stopPropagation(); // Necessary. Allows us to drop. - } var data = e.dataTransfer.getData("text"); var dest = document.getElementById(id); var src = document.getElementById(data); From f75959df1d90ab38cd65c2fa162587c7f6c8a6ac Mon Sep 17 00:00:00 2001 From: Elisha Shapiro Date: Fri, 28 Feb 2014 10:49:15 +0200 Subject: [PATCH 4/4] Refactored the uuid service, to be more efficient. --- script/lvl-uuid.js | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/script/lvl-uuid.js b/script/lvl-uuid.js index c87b85a..cfce055 100644 --- a/script/lvl-uuid.js +++ b/script/lvl-uuid.js @@ -7,19 +7,19 @@ try { } module.factory('uuid', function () { - var svc = { - new: function () { - function _p8(s) { - var p = (Math.random().toString(16) + "000000000").substr(2, 8); - return s ? "-" + p.substr(0, 4) + "-" + p.substr(4, 4) : p; - } + var svc = {}; - return _p8() + _p8(true) + _p8(true) + _p8(); - }, + function _p8(s) { + var p = (Math.random().toString(16) + "000000000").substr(2, 8); + return s ? "-" + p.substr(0, 4) + "-" + p.substr(4, 4) : p; + } - empty: function () { - return '00000000-0000-0000-0000-000000000000'; - } + svc.new = function () { + return _p8() + _p8(true) + _p8(true) + _p8(); + }; + + svc.empty = function () { + return '00000000-0000-0000-0000-000000000000'; }; return svc;