Skip to content

Commit c20aa9b

Browse files
committed
grid scopes wrapping fix
1 parent 80656f0 commit c20aa9b

File tree

2 files changed

+63
-39
lines changed

2 files changed

+63
-39
lines changed

src/ui-scroll-grid.js

Lines changed: 51 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
angular.module('ui.scroll.grid', [])
2-
.directive('uiScrollTh', ['$log', '$timeout', function (console, $timeout) {
2+
.directive('uiScrollTh', ['$log', '$timeout', function(console, $timeout) {
33

44
function GridAdapter(controller) {
55

@@ -9,12 +9,12 @@ angular.module('ui.scroll.grid', [])
99

1010
this.columnFromPoint = (x, y) => controller.columnFromPoint(x, y);
1111

12-
Object.defineProperty(this, 'columns', {get: () => controller.getColumns()});
12+
Object.defineProperty(this, 'columns', { get: () => controller.getColumns() });
1313
}
1414

1515
function ColumnAdapter(controller, column) {
1616

17-
this.css = function (/* attr, value */) {
17+
this.css = function( /* attr, value */ ) {
1818
var attr = arguments[0];
1919
var value = arguments[1];
2020
if (arguments.length == 1) {
@@ -31,7 +31,7 @@ angular.module('ui.scroll.grid', [])
3131

3232
this.exchangeWith = (index) => controller.exchangeWith(column, index);
3333

34-
Object.defineProperty(this, 'columnId', {get: () => column.id});
34+
Object.defineProperty(this, 'columnId', { get: () => column.id });
3535
}
3636

3737
function ColumnController(controller, columns, header) {
@@ -43,13 +43,13 @@ angular.module('ui.scroll.grid', [])
4343

4444
// controller api methods
4545

46-
this.applyLayout = function (layout) {
46+
this.applyLayout = function(layout) {
4747
this.css = angular.extend({}, layout.css);
4848
this.mapTo = layout.mapTo;
4949
applyCss(this.header, this.css);
5050
};
5151

52-
this.moveBefore = function (target) {
52+
this.moveBefore = function(target) {
5353
if (target) {
5454
moveBefore(header, target.header);
5555
controller.forEachRow((row) => moveBefore(row[this.id], row[target.id]));
@@ -59,7 +59,7 @@ angular.module('ui.scroll.grid', [])
5959
}
6060
};
6161

62-
this.columnFromPoint = function (x, y) {
62+
this.columnFromPoint = function(x, y) {
6363
if (insidePoint(header, x, y)) {
6464
return this;
6565
}
@@ -70,7 +70,7 @@ angular.module('ui.scroll.grid', [])
7070
return result;
7171
};
7272

73-
this.applyCss = function (target) {
73+
this.applyCss = function(target) {
7474
applyCss(target, this.css);
7575
};
7676

@@ -116,11 +116,11 @@ angular.module('ui.scroll.grid', [])
116116
scrollViewport.adapter.transform = (scope, item) => transform(rowMap.get(scope), item);
117117
});
118118

119-
this.registerColumn = function (header) {
119+
this.registerColumn = function(header) {
120120
columns.push(new ColumnController(this, columns, header));
121121
};
122122

123-
this.registerCell = function (scope, cell) {
123+
this.registerCell = function(scope, cell) {
124124
let row = rowMap.get(scope);
125125
if (!row) {
126126
row = [];
@@ -133,7 +133,7 @@ angular.module('ui.scroll.grid', [])
133133
return true;
134134
};
135135

136-
this.unregisterCell = function (scope, cell) {
136+
this.unregisterCell = function(scope, cell) {
137137
let row = rowMap.get(scope);
138138
let i = row.indexOf(cell);
139139
row.splice(i, 1);
@@ -142,30 +142,29 @@ angular.module('ui.scroll.grid', [])
142142
}
143143
};
144144

145-
this.forEachRow = function (callback) {
145+
this.forEachRow = function(callback) {
146146
rowMap.forEach(callback);
147147
};
148148

149-
this.getColumns = function () {
149+
this.getColumns = function() {
150150
let result = [];
151151
columns.slice()
152152
.sort((a, b) => a.mapTo - b.mapTo)
153153
.forEach((column) => result.push(new ColumnAdapter(this, column)));
154154
return result;
155155
};
156156

157-
this.getLayout = function () {
157+
this.getLayout = function() {
158158
let result = [];
159159
columns.forEach((column, index) => result.push({
160-
index: index,
161-
css: angular.extend({}, column.css),
162-
mapTo: column.mapTo
163-
})
164-
);
160+
index: index,
161+
css: angular.extend({}, column.css),
162+
mapTo: column.mapTo
163+
}));
165164
return result;
166165
};
167166

168-
this.applyLayout = function (layouts) {
167+
this.applyLayout = function(layouts) {
169168
if (!layouts || layouts.length != columns.length) {
170169
throw new Error('Failed to apply layout - number of layouts should match number of columns');
171170
}
@@ -174,7 +173,7 @@ angular.module('ui.scroll.grid', [])
174173
rowMap.forEach((row) => transform(row));
175174
};
176175

177-
this.moveBefore = function (selected, target) {
176+
this.moveBefore = function(selected, target) {
178177
let index = target;
179178

180179
if (target % 1 !== 0) {
@@ -184,7 +183,8 @@ angular.module('ui.scroll.grid', [])
184183
return; // throw an error?
185184
}
186185

187-
let mapTo = selected.mapTo, next = null;
186+
let mapTo = selected.mapTo,
187+
next = null;
188188
index -= mapTo < index ? 1 : 0;
189189

190190
columns.forEach(c => {
@@ -197,15 +197,15 @@ angular.module('ui.scroll.grid', [])
197197
selected.moveBefore(next);
198198
};
199199

200-
this.exchangeWith = function (selected, index) {
200+
this.exchangeWith = function(selected, index) {
201201
if (index < 0 || index >= columns.length) {
202202
return;
203203
}
204204
columns.find(c => c.mapTo === index).mapTo = selected.mapTo;
205205
selected.mapTo = index;
206206
};
207207

208-
this.columnFromPoint = function (x, y) {
208+
this.columnFromPoint = function(x, y) {
209209
let column = columns.find(col => col.columnFromPoint(x, y));
210210
return column ? new ColumnAdapter(this, column) : undefined;
211211
};
@@ -237,17 +237,31 @@ angular.module('ui.scroll.grid', [])
237237
};
238238
}])
239239

240-
.directive('uiScrollTd', function () {
241-
return {
242-
require: ['?^^uiScrollViewport'],
243-
restrict: 'A',
244-
link: ($scope, element, $attr, controllers) => {
245-
if (controllers[0]) {
246-
let gridController = controllers[0].gridController;
247-
if (gridController.registerCell($scope, element)) {
248-
$scope.$on('$destroy', () => gridController.unregisterCell($scope, element));
249-
}
250-
}
240+
.directive('uiScrollTd', function() {
241+
return {
242+
require: ['?^^uiScrollViewport'],
243+
restrict: 'A',
244+
link: ($scope, element, $attr, controllers) => {
245+
if (!controllers[0]) {
246+
return;
251247
}
252-
};
253-
});
248+
let scope = $scope;
249+
let tdInitializer = $scope.uiScrollTdInitializer;
250+
if (!tdInitializer) {
251+
tdInitializer = $scope.uiScrollTdInitializer = {
252+
linking: true
253+
};
254+
}
255+
if (!tdInitializer.linking) {
256+
scope = tdInitializer.scope;
257+
}
258+
let gridController = controllers[0].gridController;
259+
if (gridController.registerCell(scope, element)) {
260+
$scope.$on('$destroy', () => gridController.unregisterCell(scope, element));
261+
}
262+
if (!tdInitializer.linking) {
263+
tdInitializer.onLink();
264+
}
265+
}
266+
};
267+
});

src/ui-scroll.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,18 @@ angular.module('ui.scroll', [])
230230
wrapper.scope = scope;
231231
scope[itemName] = wrapper.item;
232232
});
233-
if (adapter.transform)
234-
adapter.transform(wrapper.scope, wrapper.element);
233+
// ui-scroll-grid apply
234+
if (adapter.transform) {
235+
let tdInitializer = wrapper.scope.uiScrollTdInitializer;
236+
if (tdInitializer && tdInitializer.linking) {
237+
adapter.transform(wrapper.scope, wrapper.element);
238+
} else {
239+
wrapper.scope.uiScrollTdInitializer = {
240+
onLink: () => adapter.transform(wrapper.scope, wrapper.element),
241+
scope: wrapper.scope
242+
};
243+
}
244+
}
235245
return promises;
236246
}
237247

0 commit comments

Comments
 (0)