diff --git a/extensions/DijitRegistry.js b/extensions/DijitRegistry.js index 9e255328a..30499131b 100644 --- a/extensions/DijitRegistry.js +++ b/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, @@ -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) { @@ -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) { @@ -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); } }); }); diff --git a/test/intern/all.js b/test/intern/all.js index d0c8d6ccd..b7d479bd5 100644 --- a/test/intern/all.js +++ b/test/intern/all.js @@ -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', diff --git a/test/intern/extensions/DijitRegistry.js b/test/intern/extensions/DijitRegistry.js new file mode 100644 index 000000000..14202e717 --- /dev/null +++ b/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)); + }); + }); + }); +});