move auto layout function into its own file

This commit is contained in:
Devin Howard 2016-09-13 21:01:36 +08:00
parent aace6796f5
commit 9515152315
3 changed files with 78 additions and 71 deletions

View 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
}
}

View file

@ -4,6 +4,7 @@
* Metamaps.Map.js.erb * Metamaps.Map.js.erb
* *
* Dependencies: * Dependencies:
* - Metamaps.AutoLayout
* - Metamaps.Create * - Metamaps.Create
* - Metamaps.Erb * - Metamaps.Erb
* - Metamaps.Filter * - Metamaps.Filter
@ -34,13 +35,6 @@ Metamaps.Map = {
events: { events: {
editedByActiveMapper: 'Metamaps:Map:events:editedByActiveMapper' editedByActiveMapper: 'Metamaps:Map:events:editedByActiveMapper'
}, },
nextX: 0,
nextY: 0,
sideLength: 1,
turnCount: 0,
nextXshift: 1,
nextYshift: 0,
timeToTurn: 0,
init: function () { init: function () {
var self = Metamaps.Map var self = Metamaps.Map
@ -131,7 +125,7 @@ Metamaps.Map = {
end: function () { end: function () {
if (Metamaps.Active.Map) { if (Metamaps.Active.Map) {
$('.wrapper').removeClass('canEditMap commonsMap') $('.wrapper').removeClass('canEditMap commonsMap')
Metamaps.Map.resetSpiral() Metamaps.AutoLayout.resetSpiral()
$('.rightclickmenu').remove() $('.rightclickmenu').remove()
Metamaps.TopicCard.hideCard() Metamaps.TopicCard.hideCard()
@ -242,68 +236,6 @@ Metamaps.Map = {
Metamaps.Mappers.add(Metamaps.Active.Mapper) 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 () { exportImage: function () {
var canvas = {} var canvas = {}

View file

@ -370,7 +370,7 @@ Metamaps.Topic = {
var topic = self.get(id) var topic = self.get(id)
var nextCoords = Metamaps.Map.getNextCoord() var nextCoords = Metamaps.AutoLayout.getNextCoord()
var mapping = new Metamaps.Backbone.Mapping({ var mapping = new Metamaps.Backbone.Mapping({
xloc: nextCoords.x, xloc: nextCoords.x,
yloc: nextCoords.y, yloc: nextCoords.y,