-
Notifications
You must be signed in to change notification settings - Fork 0
/
NetThings.Core.js
149 lines (118 loc) · 3.5 KB
/
NetThings.Core.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
var Mike;
Mike = Mike || {};
Mike.NetThings = Mike.NetThings || {};
Mike.NetThings.Core = function () {
var applyMovable = function(target) {
target.draggable({stop: dragStop});
};
var applyPortal = function(target, params) {
var destination = params.destination;
target.data('portal', function(object) { handlePortal(destination, object);});
portals.push(target);
};
var handlePortal = function(destination, object){
var objectId = object.attr('id');
var objectSpec = placeObjects[objectId];
delete placeObjects[objectId];
objectState[destination][objectId] = objectSpec;
object.hide();
saveState();
};
var behaviorHandlers = {
'movable' : applyMovable,
'portal' : applyPortal
};
// hash where key is the page key, value is the page's objects
var initialState = {
'chaos' : {
'greybox' : { name: '', top: 10, left: 50, height:30, width: 30, color: 'Grey', behaviors: [{type:'movable'}] },
'redbox' : { name: '', top: 100, left: 100, height:50, width: 50, color: 'Red', behaviors: [{type:'movable'}] },
'world1portal' : { name: '', top: 300, left: 600, height:100, width: 100, color: 'Black', behaviors: [{type:'portal', params : {destination:'world1'}}]}
},
'world1' : {}
};
var objectState;
var placeObjects;
var portals = [];
var dragStop = function(e, ui) {
// when someone finishes dragging an object, let's update its object state
// and save it into a cookie
var objectId = ui.helper.attr('id');
placeObjects[objectId].top = ui.position.top;
placeObjects[objectId].left = ui.position.left;
handlePortals(ui.helper, ui.position);
saveState();
}
var handlePortals = function(object, position) {
for(i=0;i<portals.length;i++)
{
var portal = portals[i];
var portalPosition = portal.offset();
var portalBottom = portalPosition.top + portal.height();
var portalRight = portalPosition.left + portal.width();
if (position.top > portalPosition.top && position.left > portalPosition.left
&& position.top < portalBottom && position.left < portalRight)
{
portal.data('portal')(object);
}
}
}
var clearSavedState = function() {
createCookie('thingsState', '', -1);
};
var saveState = function() {
objectState[inferPlace()] = placeObjects;
createCookie('thingsState', JSON.stringify(objectState));
};
var loadState = function() {
var stateJson = readCookie('thingsState');
if (stateJson)
{
objectState = JSON.parse(stateJson);
} else {
objectState = initialState;
}
loadPlace();
};
var getPlaceObjects = function(place) {
place = place || inferPlace();
return objectState[place];
};
var inferPlace = function() {
var pathParts = window.location.pathname.split('/');
var lastPart = pathParts[pathParts.length - 1];
var inferredPlace = lastPart.split('.')[0];
return inferredPlace;
};
var loadPlace = function() {
placeObjects = getPlaceObjects();
};
var render = function() {
for (var i in placeObjects)
{
var objectSpec = placeObjects[i];
var id = i;
var objectHtml = "<div id='" + id + "' class='object'>" + objectSpec.name + "</div>";
$('#objects').append(objectHtml);
$('#' + id).css(
{
'top' : objectSpec.top,
'left' : objectSpec.left,
'height' : objectSpec.height,
'width' : objectSpec.width,
'background-color' : objectSpec.color
});
var behaviors = objectSpec.behaviors;
for (j=0;j<behaviors.length;j++) {
var behavior = behaviors[j];
var handler = behaviorHandlers[behavior.type];
handler($('#' + id), behavior.params);
}
}
};
return {
render: render,
loadState: loadState,
clearSavedState: clearSavedState
};
}();