Skip to content

Commit 01aa1de

Browse files
authored
Merge pull request #716 from schmidtk/fix-replace-jquery-resize
fix(resize): replace jquery.resize.js with css-element-queries
2 parents a8b4cba + e9959de commit 01aa1de

18 files changed

+157
-211
lines changed

externs/jquery-plugins.js

-6
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,6 @@ jQuery.prototype.modal;
4141
*/
4242
jQuery.prototype.popover;
4343

44-
/**
45-
* @param {function()} listener
46-
* @return {!jQuery}
47-
*/
48-
jQuery.prototype.removeResize;
49-
5044
/**
5145
* @param {(string|Object)=} opt_options
5246
* @param {string=} opt_key

externs/resizesensor.externs.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @externs
3+
* @see https://github.com/marcj/css-element-queries
4+
*/
5+
6+
/**
7+
* Element resize sensor.
8+
* @param {Element|jQuery|angular.JQLite} element The element(s).
9+
* @param {Function} callback The callback.
10+
* @constructor
11+
*/
12+
var ResizeSensor = function(element, callback) {};
13+
14+
/**
15+
* Detach a resize sensor.
16+
* @param {Element|jQuery|angular.JQLite} element The element(s).
17+
* @param {Function} callback The callback.
18+
*/
19+
ResizeSensor.detach = function(element, callback) {};
20+
21+
/**
22+
* Reset a resize sensor.
23+
* @param {Element|jQuery|angular.JQLite} element The element(s).
24+
* @param {Function} callback The callback.
25+
*/
26+
ResizeSensor.reset = function(element, callback) {};

index.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,12 @@ const sharedResources = [
9696
{
9797
source: 'vendor/jquery',
9898
target: 'vendor/jquery',
99-
scripts: ['jquery.event.drag-2.3.0.js', 'jquery.resize.js']
99+
scripts: ['jquery.event.drag-2.3.0.js']
100+
},
101+
{
102+
source: resolver.resolveModulePath('css-element-queries/src', __dirname),
103+
target: 'vendor/css-element-queries',
104+
scripts: ['ResizeSensor.js']
100105
},
101106
{
102107
source: 'vendor/jquery-ui',

karma.conf.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ module.exports = function(config) {
4444
{pattern: 'vendor/geomag/WMM.COF', watched: false, included: false, served: true},
4545
{pattern: 'vendor/geomag/cof2Obj.js', watched: false, included: true, served: true},
4646
{pattern: 'vendor/geomag/geomag.js', watched: false, included: true, served: true},
47-
{pattern: 'vendor/jquery/jquery.resize.js', watched: false, included: true, served: true},
47+
{pattern: resolver.resolveModulePath('css-element-queries/src/ResizeSensor.js'), watched: false, included: true, served: true},
4848
{pattern: resolver.resolveModulePath('jschardet/dist/jschardet.min.js'), watched: false, included: true, served: true},
4949
{pattern: resolver.resolveModulePath('oboe/dist/oboe-browser.min.js'), watched: false, included: true, served: true},
5050
{pattern: resolver.resolveModulePath('lolex/lolex.js', __dirname), watched: false, included: true, served: true},

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@
287287
"cesium": "1.49.0",
288288
"compass-mixins": "^0.12.10",
289289
"crossfilter2": "^1.4.5",
290+
"css-element-queries": "^1.2.1",
290291
"d3": "^3.5.17",
291292
"d3-tip": "=0.6.8",
292293
"file-saver": "^1.3.8",

src/os/ui/nav/navbarctrl.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ goog.provide('os.ui.NavBarCtrl');
22

33
goog.require('goog.Disposable');
44
goog.require('goog.events.Event');
5+
goog.require('os.ui');
56
goog.require('os.ui.list');
67
goog.require('os.ui.nav.EventType');
78

@@ -39,7 +40,7 @@ os.ui.NavBarCtrl = function($scope, $element) {
3940
* @private
4041
*/
4142
this.resizeFn_ = this.onResize_.bind(this);
42-
$element.resize(this.resizeFn_);
43+
os.ui.resize(this.element, this.resizeFn_);
4344

4445
$scope.$on('$destroy', this.dispose.bind(this));
4546
};
@@ -52,6 +53,10 @@ goog.inherits(os.ui.NavBarCtrl, goog.Disposable);
5253
os.ui.NavBarCtrl.prototype.disposeInternal = function() {
5354
os.ui.NavBarCtrl.base(this, 'disposeInternal');
5455

56+
if (this.element && this.resizeFn_) {
57+
os.ui.removeResize(this.element, this.resizeFn_);
58+
}
59+
5560
this.scope = null;
5661
this.element = null;
5762
};

src/os/ui/slick/slickgrid.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ goog.require('ol.array');
1717
goog.require('os.data.ColumnDefinition');
1818
goog.require('os.events');
1919
goog.require('os.string');
20+
goog.require('os.ui');
2021
goog.require('os.ui.Module');
2122
goog.require('os.ui.column.columnManagerDirective');
2223
goog.require('os.ui.globalMenuDirective');
@@ -297,11 +298,10 @@ os.ui.slick.SlickGridCtrl = function($scope, $element, $compile) {
297298

298299
if ($scope['resizeWith']) {
299300
this.container_ = $element.parents($scope['resizeWith']);
300-
this.container_.resize(this.resizeFn);
301+
os.ui.resize(this.container_, this.resizeFn);
301302
} else {
302303
// call the resize handler when the root element resizes
303-
// note: this requires the jquery.resize.js vendor library to work properly
304-
this.element.resize(this.resizeFn);
304+
os.ui.resize(this.element, this.resizeFn);
305305
}
306306

307307
/**
@@ -404,10 +404,10 @@ os.ui.slick.SlickGridCtrl.prototype.disposeInternal = function() {
404404
goog.dispose(this.resizeDelay);
405405

406406
if (this.container_) {
407-
this.container_.off('resize');
407+
os.ui.removeResize(this.container_, this.resizeFn);
408408
this.container_ = null;
409409
} else {
410-
this.element.off('resize');
410+
os.ui.removeResize(this.element, this.resizeFn);
411411
}
412412

413413
if (this.selectionModel_) {

src/os/ui/timeline/timeline.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ goog.require('os.time.TimeInstant');
2020
goog.require('os.time.TimelineController');
2121
goog.require('os.time.TimelineEventType');
2222
goog.require('os.time.timeline');
23+
goog.require('os.ui');
2324
goog.require('os.ui.Module');
2425
goog.require('os.ui.hist.IHistogramChart');
2526
goog.require('os.ui.timeline.Brush');
@@ -460,7 +461,7 @@ os.ui.timeline.TimelineCtrl = function($scope, $element, $timeout) {
460461
* @private
461462
*/
462463
this.resizeFn_ = this.updateSize_.bind(this);
463-
this.element_.resize(this.resizeFn_);
464+
os.ui.resize(this.element_, this.resizeFn_);
464465

465466
$scope.$on('$destroy', this.destroy_.bind(this));
466467

@@ -545,7 +546,7 @@ os.ui.timeline.TimelineCtrl.prototype.destroyBrushCollection_ = function(brushCo
545546
*/
546547
os.ui.timeline.TimelineCtrl.prototype.destroy_ = function() {
547548
if (this.element_ && this.resizeFn_) {
548-
this.element_.removeResize(this.resizeFn_);
549+
os.ui.removeResize(this.element_, this.resizeFn_);
549550
this.resizeFn_ = null;
550551
}
551552

src/os/ui/ui.js

+29
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,35 @@ os.ui.sortDirectives_ = function(a, b) {
257257
};
258258

259259

260+
/**
261+
* Listen to an element for size changes.
262+
* @param {?(angular.JQLite|jQuery)} el The element.
263+
* @param {?Function} fn The callback to remove.
264+
*/
265+
os.ui.resize = function(el, fn) {
266+
if (el && fn) {
267+
if (window.ResizeSensor) {
268+
new ResizeSensor(el, fn);
269+
} else {
270+
goog.asserts.fail('The css-element-queries ResizeSensor library is not loaded. Element resize detection will ' +
271+
'not work.');
272+
}
273+
}
274+
};
275+
276+
277+
/**
278+
* Remove resize listener from an element.
279+
* @param {?(angular.JQLite|jQuery)} el The element.
280+
* @param {?Function} fn The callback to remove.
281+
*/
282+
os.ui.removeResize = function(el, fn) {
283+
if (el && fn && window.ResizeSensor != null) {
284+
ResizeSensor.detach(el, fn);
285+
}
286+
};
287+
288+
260289
/**
261290
* Replace a directive already registered with Angular. The directive name and module should be identical to the
262291
* original.

src/os/ui/util/autoheight.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,12 @@ os.ui.util.AutoHeightCtrl = function($scope, $element, $injector) {
9797

9898
if (this.parent_) {
9999
// listen for parent size changes
100-
this.parent_.resize(this.resizeFn_);
100+
os.ui.resize(this.parent_, this.resizeFn_);
101101

102102
// listen for sibling resize changes
103103
var siblings = /** @type {string} */ ($scope['siblings']);
104104
if (siblings) {
105-
this.parent_.find(siblings).resize(this.resizeFn_);
105+
os.ui.resize(this.parent_.find(siblings), this.resizeFn_);
106106
}
107107

108108
// there are some situations where resize won't fire on creation, particularly when using IE or when swapping DOM
@@ -121,13 +121,11 @@ os.ui.util.AutoHeightCtrl = function($scope, $element, $injector) {
121121
*/
122122
os.ui.util.AutoHeightCtrl.prototype.onDestroy_ = function() {
123123
if (this.parent_) {
124-
this.parent_.removeResize(this.resizeFn_);
124+
os.ui.removeResize(this.parent_, this.resizeFn_);
125125

126126
var siblings = /** @type {string} */ (this.scope_['siblings']);
127127
if (siblings) {
128-
try {
129-
this.parent_.find(siblings).removeResize(this.resizeFn_);
130-
} catch (e) {}
128+
os.ui.removeResize(this.parent_.find(siblings), this.resizeFn_);
131129
}
132130

133131
this.parent_ = null;

src/os/ui/util/autovheight.js

+5-7
Original file line numberDiff line numberDiff line change
@@ -200,12 +200,12 @@ os.ui.util.AutoVHeightCtrl.prototype.addResizeListeners_ = function() {
200200

201201
// add resize to common elements
202202
goog.object.getValues(os.ui.windowCommonElements).forEach(function(sibling) {
203-
$(/** @type {string} */ (sibling)).resize(this.resizeFn_);
203+
os.ui.resize($(/** @type {string} */ (sibling)), this.resizeFn_);
204204
}.bind(this));
205205

206206
var siblings = /** @type {string} */ (this.scope_['siblings']);
207207
if (siblings) {
208-
$(siblings).resize(this.resizeFn_);
208+
os.ui.resize($(siblings), this.resizeFn_);
209209
}
210210
}
211211
};
@@ -224,16 +224,14 @@ os.ui.util.AutoVHeightCtrl.prototype.removeResizeListeners_ = function() {
224224
// add resize to common elements
225225
goog.object.getValues(os.ui.windowCommonElements).forEach(function(sibling) {
226226
if (this.resizeFn_) {
227-
try {
228-
$(/** @type {string} */ (sibling)).off(goog.events.EventType.RESIZE, this.resizeFn_);
229-
} catch (e) {}
227+
os.ui.removeResize($(/** @type {string} */ (sibling)), this.resizeFn_);
230228
}
231-
}.bind(this));
229+
}, this);
232230

233231
var siblings = /** @type {string} */ (this.scope_['siblings']);
234232
if (siblings && this.resizeFn_) {
235233
try {
236-
$(siblings).off(goog.events.EventType.RESIZE, this.resizeFn_);
234+
os.ui.removeResize($(siblings), this.resizeFn_);
237235
} catch (e) {}
238236
}
239237
}

src/os/ui/util/offsetmargin.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
goog.provide('os.ui.util.OffsetMarginCtrl');
22
goog.provide('os.ui.util.offsetMarginDirective');
3+
34
goog.require('goog.Throttle');
45
goog.require('os.config.ThemeSettingsChangeEvent');
56
goog.require('os.ui');
@@ -100,12 +101,12 @@ os.ui.util.OffsetMarginCtrl.prototype.onDestroy_ = function() {
100101
}
101102

102103
if (this.bufferTopElement_) {
103-
this.bufferTopElement_.removeResize(this.resizeFn_);
104+
os.ui.removeResize(this.bufferTopElement_, this.resizeFn_);
104105
this.bufferTopElement_ = null;
105106
}
106107

107108
if (this.bufferBotElement_) {
108-
this.bufferBotElement_.removeResize(this.resizeFn_);
109+
os.ui.removeResize(this.bufferBotElement_, this.resizeFn_);
109110
this.bufferBotElement_ = null;
110111
}
111112

@@ -123,8 +124,8 @@ os.ui.util.OffsetMarginCtrl.prototype.setWatchEl_ = function() {
123124
this.bufferTopElement_ = $(this.scope_['offsetTopEl']);
124125
this.bufferBotElement_ = $(this.scope_['offsetBotEl']);
125126
if (this.bufferTopElement_[0] && this.bufferBotElement_[0]) {
126-
this.bufferTopElement_.resize(this.resizeFn_);
127-
this.bufferBotElement_.resize(this.resizeFn_);
127+
os.ui.resize(this.bufferTopElement_, this.resizeFn_);
128+
os.ui.resize(this.bufferBotElement_, this.resizeFn_);
128129
this.onThrottleResize_();
129130
} else {
130131
this.bufferTopElement_ = null;

src/os/ui/util/punyparent.js

+9-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
goog.provide('os.ui.util.PunyParentCtrl');
22
goog.provide('os.ui.util.punyParentDirective');
3+
34
goog.require('goog.Throttle');
5+
goog.require('goog.dom.ViewportSizeMonitor');
6+
goog.require('os.ui');
47
goog.require('os.ui.Module');
58

69

@@ -50,20 +53,16 @@ os.ui.util.PunyParentCtrl = function($scope, $element) {
5053
*/
5154
this.throttle_ = new goog.Throttle(this.onThrottleResize_, 200, this);
5255

53-
/**
54-
* @type {Function}
55-
* @private
56-
*/
57-
this.resizeFn_ = this.onResize_.bind(this);
58-
5956
/**
6057
* Keep track of our maximum child size. This prevents saying we have enough space after a resize already occured
6158
* @type {number}
6259
* @private
6360
*/
6461
this.fullSize = 0;
6562

66-
$element.resize(this.resizeFn_);
63+
var vsm = goog.dom.ViewportSizeMonitor.getInstanceForWindow();
64+
vsm.listen(goog.events.EventType.RESIZE, this.onResize_, false, this);
65+
6766
$scope.$on('$destroy', this.destroy_.bind(this));
6867
};
6968

@@ -72,7 +71,9 @@ os.ui.util.PunyParentCtrl = function($scope, $element) {
7271
* @private
7372
*/
7473
os.ui.util.PunyParentCtrl.prototype.destroy_ = function() {
75-
this.element_.removeResize(this.resizeFn_);
74+
var vsm = goog.dom.ViewportSizeMonitor.getInstanceForWindow();
75+
vsm.unlisten(goog.events.EventType.RESIZE, this.onResize_, false, this);
76+
7677
if (this.throttle_) {
7778
this.throttle_.dispose();
7879
this.throttle_ = null;

src/os/ui/window.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ goog.require('goog.events.KeyHandler');
1616
goog.require('goog.log');
1717
goog.require('ol.array');
1818
goog.require('os.array');
19+
goog.require('os.ui');
1920
goog.require('os.ui.Module');
2021
goog.require('os.ui.events.UIEvent');
2122
goog.require('os.ui.onboarding.contextOnboardingDirective');
@@ -824,7 +825,7 @@ os.ui.WindowCtrl = function($scope, $element, $timeout) {
824825

825826
if (this.element && !this.resizeFn_) {
826827
this.resizeFn_ = this.onWindowResize_.bind(this);
827-
this.element.resize(this.resizeFn_);
828+
os.ui.resize(this.element, this.resizeFn_);
828829
}
829830
}.bind(this));
830831
};
@@ -863,7 +864,7 @@ os.ui.WindowCtrl.prototype.disposeInternal = function() {
863864
}
864865

865866
if (this.element && this.resizeFn_) {
866-
this.element.removeResize(this.resizeFn_);
867+
os.ui.removeResize(this.element, this.resizeFn_);
867868
this.resizeFn_ = null;
868869
}
869870

src/os/ui/wiz/wizardpreview.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ goog.provide('os.ui.wiz.wizardPreviewDirective');
33

44
goog.require('os.data.ColumnDefinition');
55
goog.require('os.im.mapping');
6+
goog.require('os.ui');
67
goog.require('os.ui.Module');
78
goog.require('os.ui.slick.slickGridDirective');
89
goog.require('os.ui.slick.slickHeaderButtonDirective');
@@ -91,7 +92,7 @@ os.ui.wiz.WizardPreviewCtrl = function($scope, $element, $timeout) {
9192
if ($scope['resizeWith']) {
9293
this.resizeFn_ = this.resizePreview_.bind(this);
9394
this.container_ = $element.parents($scope['resizeWith']);
94-
this.container_.resize(this.resizeFn_);
95+
os.ui.resize(this.container_, this.resizeFn_);
9596
this.resizePreview_(500);
9697
}
9798

@@ -108,7 +109,7 @@ os.ui.wiz.WizardPreviewCtrl = function($scope, $element, $timeout) {
108109
os.ui.wiz.WizardPreviewCtrl.prototype.destroy_ = function() {
109110
if (this.container_) {
110111
if (this.resizeFn_) {
111-
this.container_.removeResize(this.resizeFn_);
112+
os.ui.removeResize(this.container_, this.resizeFn_);
112113
this.resizeFn_ = null;
113114
}
114115

0 commit comments

Comments
 (0)