Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A lilbit improvement (I hope) #8

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
server/node_modules
.idea/
157 changes: 77 additions & 80 deletions script/lvl-drag-drop.js
Original file line number Diff line number Diff line change
@@ -1,86 +1,83 @@
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.
}

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");
});
}
}
}]);
37 changes: 19 additions & 18 deletions script/lvl-uuid.js
Original file line number Diff line number Diff line change
@@ -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 = {};

function _p8(s) {
var p = (Math.random().toString(16) + "000000000").substr(2, 8);
return s ? "-" + p.substr(0, 4) + "-" + p.substr(4, 4) : p;
}

svc.new = function () {
return _p8() + _p8(true) + _p8(true) + _p8();
};

svc.empty = function () {
return '00000000-0000-0000-0000-000000000000';
};

return svc;
});