move auto layout function into its own file
This commit is contained in:
parent
aace6796f5
commit
9515152315
3 changed files with 78 additions and 71 deletions
75
app/assets/javascripts/src/Metamaps.AutoLayout.js
Normal file
75
app/assets/javascripts/src/Metamaps.AutoLayout.js
Normal file
|
@ -0,0 +1,75 @@
|
|||
/* global Metamaps */
|
||||
|
||||
/*
|
||||
* Metmaaps.AutoLayout.js
|
||||
*
|
||||
* Dependencies: none!
|
||||
*/
|
||||
|
||||
Metamaps.AutoLayout = {
|
||||
nextX: 0,
|
||||
nextY: 0,
|
||||
sideLength: 1,
|
||||
turnCount: 0,
|
||||
nextXshift: 1,
|
||||
nextYshift: 0,
|
||||
timeToTurn: 0,
|
||||
|
||||
getNextCoord: function () {
|
||||
var self = Metamaps.AutoLayout
|
||||
var nextX = self.nextX
|
||||
var nextY = self.nextY
|
||||
|
||||
var DISTANCE_BETWEEN = 120
|
||||
|
||||
self.nextX = self.nextX + DISTANCE_BETWEEN * self.nextXshift
|
||||
self.nextY = self.nextY + DISTANCE_BETWEEN * self.nextYshift
|
||||
|
||||
self.timeToTurn += 1
|
||||
// if true, it's time to turn
|
||||
if (self.timeToTurn === self.sideLength) {
|
||||
self.turnCount += 1
|
||||
// if true, it's time to increase side length
|
||||
if (self.turnCount % 2 === 0) {
|
||||
self.sideLength += 1
|
||||
}
|
||||
self.timeToTurn = 0
|
||||
|
||||
// going right? turn down
|
||||
if (self.nextXshift == 1 && self.nextYshift == 0) {
|
||||
self.nextXshift = 0
|
||||
self.nextYshift = 1
|
||||
}
|
||||
// going down? turn left
|
||||
else if (self.nextXshift == 0 && self.nextYshift == 1) {
|
||||
self.nextXshift = -1
|
||||
self.nextYshift = 0
|
||||
}
|
||||
// going left? turn up
|
||||
else if (self.nextXshift == -1 && self.nextYshift == 0) {
|
||||
self.nextXshift = 0
|
||||
self.nextYshift = -1
|
||||
}
|
||||
// going up? turn right
|
||||
else if (self.nextXshift == 0 && self.nextYshift == -1) {
|
||||
self.nextXshift = 1
|
||||
self.nextYshift = 0
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
x: nextX,
|
||||
y: nextY
|
||||
}
|
||||
},
|
||||
resetSpiral: function () {
|
||||
var self = Metamaps.AutoLayout
|
||||
self.nextX = 0
|
||||
self.nextY = 0
|
||||
self.nextXshift = 1
|
||||
self.nextYshift = 0
|
||||
self.sideLength = 1
|
||||
self.timeToTurn = 0
|
||||
self.turnCount = 0
|
||||
}
|
||||
}
|
|
@ -4,6 +4,7 @@
|
|||
* Metamaps.Map.js.erb
|
||||
*
|
||||
* Dependencies:
|
||||
* - Metamaps.AutoLayout
|
||||
* - Metamaps.Create
|
||||
* - Metamaps.Erb
|
||||
* - Metamaps.Filter
|
||||
|
@ -34,13 +35,6 @@ Metamaps.Map = {
|
|||
events: {
|
||||
editedByActiveMapper: 'Metamaps:Map:events:editedByActiveMapper'
|
||||
},
|
||||
nextX: 0,
|
||||
nextY: 0,
|
||||
sideLength: 1,
|
||||
turnCount: 0,
|
||||
nextXshift: 1,
|
||||
nextYshift: 0,
|
||||
timeToTurn: 0,
|
||||
init: function () {
|
||||
var self = Metamaps.Map
|
||||
|
||||
|
@ -131,7 +125,7 @@ Metamaps.Map = {
|
|||
end: function () {
|
||||
if (Metamaps.Active.Map) {
|
||||
$('.wrapper').removeClass('canEditMap commonsMap')
|
||||
Metamaps.Map.resetSpiral()
|
||||
Metamaps.AutoLayout.resetSpiral()
|
||||
|
||||
$('.rightclickmenu').remove()
|
||||
Metamaps.TopicCard.hideCard()
|
||||
|
@ -242,68 +236,6 @@ Metamaps.Map = {
|
|||
Metamaps.Mappers.add(Metamaps.Active.Mapper)
|
||||
}
|
||||
},
|
||||
getNextCoord: function () {
|
||||
var self = Metamaps.Map
|
||||
var nextX = self.nextX
|
||||
var nextY = self.nextY
|
||||
|
||||
var DISTANCE_BETWEEN = 120
|
||||
|
||||
self.nextX = self.nextX + DISTANCE_BETWEEN * self.nextXshift
|
||||
self.nextY = self.nextY + DISTANCE_BETWEEN * self.nextYshift
|
||||
|
||||
self.timeToTurn += 1
|
||||
// if true, it's time to turn
|
||||
if (self.timeToTurn === self.sideLength) {
|
||||
self.turnCount += 1
|
||||
// if true, it's time to increase side length
|
||||
if (self.turnCount % 2 === 0) {
|
||||
self.sideLength += 1
|
||||
}
|
||||
self.timeToTurn = 0
|
||||
|
||||
// going right? turn down
|
||||
if (self.nextXshift == 1 && self.nextYshift == 0) {
|
||||
self.nextXshift = 0
|
||||
self.nextYshift = 1
|
||||
}
|
||||
// going down? turn left
|
||||
else if (self.nextXshift == 0 && self.nextYshift == 1) {
|
||||
self.nextXshift = -1
|
||||
self.nextYshift = 0
|
||||
}
|
||||
// going left? turn up
|
||||
else if (self.nextXshift == -1 && self.nextYshift == 0) {
|
||||
self.nextXshift = 0
|
||||
self.nextYshift = -1
|
||||
}
|
||||
// going up? turn right
|
||||
else if (self.nextXshift == 0 && self.nextYshift == -1) {
|
||||
self.nextXshift = 1
|
||||
self.nextYshift = 0
|
||||
}
|
||||
}
|
||||
|
||||
// this is so that if someone has relied on the auto-placement feature on this map,
|
||||
// it will at least start placing nodes at the first empty spot
|
||||
// this will only work up to the point in the spiral at which someone manually moved a node
|
||||
if (Metamaps.Mappings.findWhere({ xloc: nextX, yloc: nextY })) {
|
||||
return self.getNextCoord()
|
||||
}
|
||||
else return {
|
||||
x: nextX,
|
||||
y: nextY
|
||||
}
|
||||
},
|
||||
resetSpiral: function () {
|
||||
Metamaps.Map.nextX = 0
|
||||
Metamaps.Map.nextY = 0
|
||||
Metamaps.Map.nextXshift = 1
|
||||
Metamaps.Map.nextYshift = 0
|
||||
Metamaps.Map.sideLength = 1
|
||||
Metamaps.Map.timeToTurn = 0
|
||||
Metamaps.Map.turnCount = 0
|
||||
},
|
||||
exportImage: function () {
|
||||
var canvas = {}
|
||||
|
||||
|
|
|
@ -370,7 +370,7 @@ Metamaps.Topic = {
|
|||
|
||||
var topic = self.get(id)
|
||||
|
||||
var nextCoords = Metamaps.Map.getNextCoord()
|
||||
var nextCoords = Metamaps.AutoLayout.getNextCoord()
|
||||
var mapping = new Metamaps.Backbone.Mapping({
|
||||
xloc: nextCoords.x,
|
||||
yloc: nextCoords.y,
|
||||
|
|
Loading…
Reference in a new issue