Skip to content

Commit 2b15adc

Browse files
negherbonfaceleg
authored andcommitted
Setting the width through a property js (likeastore#460)
* Setting the width through a property js * update readme, implement other metrics support, and unit tests * update readme * refactoring, multi-line conditional
1 parent 95b79bb commit 2b15adc

File tree

5 files changed

+73
-1
lines changed

5 files changed

+73
-1
lines changed

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,30 @@ Specifies the CSS selector for the element to be referenced by the ``aria-descri
372372

373373
If specified, the first matching element is used. See [Accessibility](#Accessibility) for more information.
374374

375+
##### ``width {Number | String}``
376+
377+
This option allows you to control the dialog's width. Default value is `null` (unspecified)
378+
379+
If you provide a Number, 'px' will be appended. To use a custom metric, use a String, e.g. `'40%'`.
380+
381+
For example, the following will add `width: 400px;` to the dialog when opened:
382+
383+
```
384+
ngDialog.open({
385+
template: 'template.html',
386+
width: 400
387+
});
388+
```
389+
390+
In another example, the following will add `width: 40%;`:
391+
392+
```
393+
ngDialog.open({
394+
template: 'template.html',
395+
width: '40%'
396+
});
397+
```
398+
375399
#### Returns:
376400

377401
The ``open()`` method returns an object with some useful properties.

example/index.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
<a href="" ng-click="openConfirmWithPreCloseCallbackOnScope()">Open confirm modal with pre-close callback on scope</a>
7171
<a href="" ng-click="openConfirmWithPreCloseCallbackInlinedWithNestedConfirm()">Open confirm modal with pre-close inlined with nested confirm.</a>
7272
<a href="" ng-click="openWithoutOverlay()">Open without overlay</a>
73+
<a href="" ng-click="openWithJSSpecificWidth()" id="js-width">Open with js specific width</a>
7374

7475
<!-- Templates -->
7576

@@ -387,6 +388,15 @@ <h3>ngDialog modal example</h3>
387388
});
388389
};
389390

391+
$scope.openWithJSSpecificWidth = function () {
392+
ngDialog.open({
393+
template: '<h2>Notice that style inline set specific width!</h2>',
394+
className: 'ngdialog-theme-default',
395+
width: 650,
396+
plain: true
397+
});
398+
};
399+
390400
$rootScope.$on('ngDialog.opened', function (e, $dialog) {
391401
console.log('ngDialog opened: ' + $dialog.attr('id'));
392402
});

js/ngDialog.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@
6161
ariaLabelledBySelector: null,
6262
ariaDescribedById: null,
6363
ariaDescribedBySelector: null,
64-
bodyClassName: 'ngdialog-open'
64+
bodyClassName: 'ngdialog-open',
65+
width: null
6566
};
6667

6768
this.setForceHtmlReload = function (_useIt) {
@@ -542,6 +543,15 @@
542543
$dialog.addClass(options.appendClassName);
543544
}
544545

546+
if (options.width) {
547+
var $dialogContent = $dialog[0].querySelector('.ngdialog-content');
548+
if (angular.isString(options.width)) {
549+
$dialogContent.style.width = options.width;
550+
} else {
551+
$dialogContent.style.width = options.width + 'px';
552+
}
553+
}
554+
545555
if (options.disableAnimation) {
546556
$dialog.addClass(disabledAnimationClass);
547557
}

tests/protractor/open.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,9 @@ describe('ngDialog open', function() {
1414
expect(text).toBe('Data passed through: from a service');
1515
});
1616
});
17+
18+
it('should define specific width through a property js', function() {
19+
element(by.css('#js-width')).click();
20+
expect(element(by.css('.ngdialog-content')).getCssValue('width')).toBe('650px');
21+
});
1722
});

tests/unit/ngDialog.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,4 +267,27 @@ describe('ngDialog', function () {
267267
});
268268
});
269269

270+
describe('with an width', function () {
271+
var elm, open;
272+
beforeEach(inject(function (ngDialog, $timeout, $document) {
273+
open = function(width) {
274+
var id = ngDialog.open({
275+
width: width
276+
}).id;
277+
$timeout.flush();
278+
elm = $document[0].getElementById(id).querySelector('.ngdialog-content');
279+
}
280+
}));
281+
282+
it('should transform number to px', function () {
283+
open(400);
284+
expect(elm.style.cssText).toBe('width: 400px; ');
285+
});
286+
287+
it('should set other width metrics', function () {
288+
open('40%');
289+
expect(elm.style.cssText).toBe('width: 40%; ');
290+
});
291+
});
292+
270293
});

0 commit comments

Comments
 (0)