Skip to content

Commit

Permalink
DijitRegistry: Add placeAt (delegating to _WidgetBase's)
Browse files Browse the repository at this point in the history
Includes unit tests.

Resolves #1135.
  • Loading branch information
kfranqueiro authored and Kenneth G. Franqueiro committed Sep 25, 2015
1 parent be61cc2 commit 1767584
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 9 deletions.
28 changes: 19 additions & 9 deletions extensions/DijitRegistry.js
@@ -1,8 +1,11 @@
define([
'dojo/_base/declare',
'dojo/dom-geometry',
'dijit/_WidgetBase',
'dijit/registry'
], function (declare, domGeometry, registry) {
], function (declare, domGeometry, _WidgetBase, registry) {
var wbPrototype = _WidgetBase.prototype;

return declare(null, {
// summary:
// A dgrid extension which will add the grid to the dijit registry,
Expand All @@ -28,7 +31,7 @@ define([
}
this.inherited(arguments);

var widget = registry.getEnclosingWidget(this.domNode.parentNode);
var widget = this.getParent();
// If we have a parent layout container widget, it will handle resize,
// so remove the window resize listener added by List.
if (widget && widget.isLayoutContainer) {
Expand All @@ -51,11 +54,25 @@ define([
return [];
},

getParent: function () {
// summary:
// Analogous to _WidgetBase#getParent, for compatibility with e.g. dijit._KeyNavContainer.
return registry.getEnclosingWidget(this.domNode.parentNode);
},

isLeftToRight: function () {
// Implement method expected by Dijit layout widgets
return !this.isRTL;
},

placeAt: function (/*String|DomNode|DocumentFragment|dijit/_WidgetBase*/ reference, /*String|Int?*/ position) {
// summary:
// Analogous to dijit/_WidgetBase#placeAt;
// places this widget in the DOM based on domConstruct.place() conventions.

return wbPrototype.placeAt.call(this, reference, position);
},

resize: function (changeSize) {
// Honor changeSize parameter used by layout widgets, and resize grid
if (changeSize) {
Expand All @@ -76,13 +93,6 @@ define([
// summary:
// dgrid doesn't support watch; this is a no-op for compatibility with
// some Dijit layout widgets which assume its existence.
},

getParent: function () {
// summary:
// Analogue of _WidgetBase#getParent for compatibility with for example
// dijit._KeyNavContainer.
return registry.getEnclosingWidget(this.domNode.parentNode);
}
});
});
1 change: 1 addition & 0 deletions test/intern/all.js
Expand Up @@ -9,6 +9,7 @@ define([
'intern/node_modules/dojo/has!host-browser?./core/trackable',
'intern/node_modules/dojo/has!host-browser?./extensions/ColumnHider',
'intern/node_modules/dojo/has!host-browser?./extensions/CompoundColumns',
'intern/node_modules/dojo/has!host-browser?./extensions/DijitRegistry',
'intern/node_modules/dojo/has!host-browser?./extensions/Pagination',
'intern/node_modules/dojo/has!host-browser?./mixins/ColumnSet',
'intern/node_modules/dojo/has!host-browser?./mixins/Editor',
Expand Down
105 changes: 105 additions & 0 deletions test/intern/extensions/DijitRegistry.js
@@ -0,0 +1,105 @@
define([
'intern!tdd',
'intern/chai!assert',
'dojo/_base/array',
'dojo/_base/declare',
'dojo/aspect',
'dojo/dom-construct',
'dijit/registry',
'dijit/layout/BorderContainer',
'dijit/layout/StackContainer',
'dgrid/List',
'dgrid/extensions/DijitRegistry',
'dojo/domReady!'
], function (test, assert, arrayUtil, declare, aspect, domConstruct,
registry, BorderContainer, StackContainer, List, DijitRegistry) {

var list,
DijitList = declare([ List, DijitRegistry ]);

test.suite('DijitRegistry', function () {
test.afterEach(function () {
list.destroy();
});

test.test('Dijit registry population', function () {
var id = 'myId';
list = new DijitList({ id: id });
assert.strictEqual(registry.byId(id), list,
'dgrid instances with DijitRegistry mixin should appear in dijit/registry');
});

test.suite('#placeAt', function () {
test.beforeEach(function () {
list = new DijitList();
});

test.suite('DOM', function () {
var referenceNode;
test.before(function () {
referenceNode = domConstruct.create('div', null, document.body);
});

test.afterEach(function () {
domConstruct.empty(referenceNode);
});

test.after(function () {
domConstruct.destroy(referenceNode);
});

test.test('default placement (last child)', function () {
domConstruct.create('div', null, referenceNode);
list.placeAt(referenceNode);
assert.strictEqual(referenceNode.lastChild, list.domNode,
'placeAt with domNode should place dgrid instance as the last child by default');
});

test.test('specified placement', function () {
list.placeAt(referenceNode, 'after');
assert.strictEqual(referenceNode.nextSibling, list.domNode,
'placeAt with domNode and placement argument should operate like domConstruct.place');
});
});

test.suite('Dijit (also tests startup/destroy)', function () {
var containerWidget;

test.afterEach(function () {
containerWidget.destroyRecursive();
});

function createContainerTest(Ctor) {
return function () {
containerWidget = new Ctor().placeAt(document.body);
containerWidget.startup();

var startupCalled = 0;
var destroyCalled = 0;
aspect.before(list, 'startup', function () {
startupCalled++;
});
aspect.before(list, 'destroy', function () {
destroyCalled++;
});

list.region = 'center'; // For BorderContainer (irrelevant for others)
list.placeAt(containerWidget);

assert.strictEqual(arrayUtil.indexOf(containerWidget.getChildren(), list), 0,
'placeAt with layout widget should place dgrid instance inside layout widget');
assert.strictEqual(startupCalled, 1,
'dgrid instance should be started up when placed inside a started-up layout widget');

containerWidget.destroyRecursive();
assert.strictEqual(destroyCalled, 1,
'list should be destroyed when destroyRecursive is called on its containing widget');
};
}

test.test('BorderContainer', createContainerTest(BorderContainer));
test.test('StackContainer', createContainerTest(StackContainer));
});
});
});
});

0 comments on commit 1767584

Please sign in to comment.