/
ModuleLayout.js
248 lines (223 loc) · 9.53 KB
/
ModuleLayout.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*
Copyright 2008-2009 University of Cambridge
Copyright 2008-2009 University of Toronto
Copyright 2010-2011 OCAD University
Copyright 2010 Lucendo Development Ltd.
Licensed under the Educational Community License (ECL), Version 2.0 or the New
BSD license. You may not use this file except in compliance with one these
Licenses.
You may obtain a copy of the ECL 2.0 License and BSD License at
https://source.fluidproject.org/svn/LICENSE.txt
*/
// Declare dependencies.
/*global jQuery, fluid, fluid_1_4:true*/
// JSLint options
/*jslint white: true, undef: true, newcap: true, nomen: true, regexp: true, bitwise: true, browser: true, forin: true, maxerr: 100, indent: 4 */
var fluid_1_4 = fluid_1_4 || {};
(function ($, fluid) {
fluid.registerNamespace("fluid.moduleLayout");
/**
* Calculate the location of the item and the column in which it resides.
* @return An object with column index and item index (within that column) properties.
* These indices are -1 if the item does not exist in the grid.
*/
// unsupported - NON-API function
fluid.moduleLayout.findColumnAndItemIndices = function (item, layout) {
return fluid.find(layout.columns,
function (column, colIndex) {
var index = $.inArray(item, column.elements);
return index === -1 ? undefined : {columnIndex: colIndex, itemIndex: index};
}, {columnIndex: -1, itemIndex: -1});
};
// unsupported - NON-API function
fluid.moduleLayout.findColIndex = function (item, layout) {
return fluid.find(layout.columns,
function (column, colIndex) {
return item === column.container ? colIndex : undefined;
}, -1);
};
/**
* Move an item within the layout object.
*/
// unsupported - NON-API function
fluid.moduleLayout.updateLayout = function (item, target, position, layout) {
item = fluid.unwrap(item);
target = fluid.unwrap(target);
var itemIndices = fluid.moduleLayout.findColumnAndItemIndices(item, layout);
layout.columns[itemIndices.columnIndex].elements.splice(itemIndices.itemIndex, 1);
var targetCol;
if (position === fluid.position.INSIDE) {
targetCol = layout.columns[fluid.moduleLayout.findColIndex(target, layout)].elements;
targetCol.splice(targetCol.length, 0, item);
} else {
var relativeItemIndices = fluid.moduleLayout.findColumnAndItemIndices(target, layout);
targetCol = layout.columns[relativeItemIndices.columnIndex].elements;
position = fluid.normalisePosition(position,
itemIndices.columnIndex === relativeItemIndices.columnIndex,
relativeItemIndices.itemIndex, itemIndices.itemIndex);
var relative = position === fluid.position.BEFORE ? 0 : 1;
targetCol.splice(relativeItemIndices.itemIndex + relative, 0, item);
}
};
/**
* Builds a layout object from a set of columns and modules.
* @param {jQuery} container
* @param {jQuery} columns
* @param {jQuery} portlets
*/
fluid.moduleLayout.layoutFromFlat = function (container, columns, portlets) {
var layout = {};
layout.container = container;
layout.columns = fluid.transform(columns,
function (column) {
return {
container: column,
elements: $.makeArray(portlets.filter(function () {
// is this a bug in filter? would have expected "this" to be 1st arg
return fluid.dom.isContainer(column, this);
}))
};
});
return layout;
};
/**
* Builds a layout object from a serialisable "layout" object consisting of id lists
*/
fluid.moduleLayout.layoutFromIds = function (idLayout) {
return {
container: fluid.byId(idLayout.id),
columns: fluid.transform(idLayout.columns, function (column) {
return {
container: fluid.byId(column.id),
elements: fluid.transform(column.children, fluid.byId)
};
})
};
};
/**
* Serializes the current layout into a structure of ids
*/
fluid.moduleLayout.layoutToIds = function (idLayout) {
return {
id: fluid.getId(idLayout.container),
columns: fluid.transform(idLayout.columns, function (column) {
return {
id: fluid.getId(column.container),
children: fluid.transform(column.elements, fluid.getId)
};
})
};
};
var defaultOnShowKeyboardDropWarning = function (item, dropWarning) {
if (dropWarning) {
var offset = $(item).offset();
dropWarning = $(dropWarning);
dropWarning.css("position", "absolute");
dropWarning.css("top", offset.top);
dropWarning.css("left", offset.left);
}
};
fluid.defaults(true, "fluid.moduleLayoutHandler",
{orientation: fluid.orientation.VERTICAL,
containerRole: fluid.reorderer.roles.REGIONS,
selectablesTabindex: -1,
sentinelize: true
});
/**
* Module Layout Handler for reordering content modules.
*
* General movement guidelines:
*
* - Arrowing sideways will always go to the top (moveable) module in the column
* - Moving sideways will always move to the top available drop target in the column
* - Wrapping is not necessary at this first pass, but is ok
*/
fluid.moduleLayoutHandler = function (container, options, dropManager, dom) {
var that = {};
function computeLayout() {
var togo;
if (options.selectors.modules) {
togo = fluid.moduleLayout.layoutFromFlat(container, dom.locate("columns"), dom.locate("modules"));
}
if (!togo) {
var idLayout = fluid.get(options, "moduleLayout.layout");
fluid.moduleLayout.layoutFromIds(idLayout);
}
return togo;
}
var layout = computeLayout();
that.layout = layout;
function isLocked(item) {
var lockedModules = options.selectors.lockedModules ? dom.fastLocate("lockedModules") : [];
return $.inArray(item, lockedModules) !== -1;
}
that.getRelativePosition =
fluid.reorderer.relativeInfoGetter(options.orientation,
fluid.reorderer.WRAP_LOCKED_STRATEGY, fluid.reorderer.GEOMETRIC_STRATEGY,
dropManager, dom, options.disableWrap);
that.getGeometricInfo = function () {
var extents = [];
var togo = {extents: extents,
sentinelize: options.sentinelize};
togo.elementMapper = function (element) {
return isLocked(element) ? "locked" : null;
};
togo.elementIndexer = function (element) {
var indices = fluid.moduleLayout.findColumnAndItemIndices(element, that.layout);
return {
index: indices.itemIndex,
length: layout.columns[indices.columnIndex].elements.length,
moduleIndex: indices.columnIndex,
moduleLength: layout.columns.length
};
};
for (var col = 0; col < layout.columns.length; col++) {
var column = layout.columns[col];
var thisEls = {
orientation: options.orientation,
elements: $.makeArray(column.elements),
parentElement: column.container
};
// fluid.log("Geometry col " + col + " elements " + fluid.dumpEl(thisEls.elements) + " isLocked [" +
// fluid.transform(thisEls.elements, togo.elementMapper).join(", ") + "]");
extents.push(thisEls);
}
return togo;
};
function computeModules(all) {
return function () {
var modules = fluid.accumulate(layout.columns, function (column, list) {
return list.concat(column.elements); // note that concat will not work on a jQuery
}, []);
if (!all) {
fluid.remove_if(modules, isLocked);
}
return modules;
};
}
that.returnedOptions = {
selectors: {
movables: computeModules(false),
dropTargets: computeModules(false),
selectables: computeModules(true)
},
listeners: {
onMove: {
priority: "last",
listener: function (item, requestedPosition) {
fluid.moduleLayout.updateLayout(item, requestedPosition.element, requestedPosition.position, layout);
}
},
onRefresh: function () {
layout = computeLayout();
that.layout = layout;
},
"onShowKeyboardDropWarning.setPosition": defaultOnShowKeyboardDropWarning
}
};
that.getModel = function () {
return fluid.moduleLayout.layoutToIds(layout);
};
return that;
};
})(jQuery, fluid_1_4);