Skip to content

Commit

Permalink
apply patch ebondu#universal-support (edcarroll/pull/270)
Browse files Browse the repository at this point in the history
  • Loading branch information
Oliver Eivak committed Apr 7, 2018
1 parent 348d2e5 commit 77eaabf
Show file tree
Hide file tree
Showing 24 changed files with 101 additions and 59 deletions.
2 changes: 0 additions & 2 deletions demo/src/app/components/sidebar/sidebar.component.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { Component, HostBinding, Output, EventEmitter, HostListener, isDevMode } from "@angular/core";
// Polyfill for IE
import "element-closest";

interface IAugmentedElement extends Element {
closest(selector:string):IAugmentedElement;
Expand Down
5 changes: 0 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
"dependencies": {
"bowser": "^1.7.2",
"date-fns": "2.0.0-alpha.1",
"element-closest": "^2.0.2",
"extend": "^3.0.1",
"is-ua-webview": "^1.0.0",
"popper.js": "^1.14.0",
Expand Down
37 changes: 37 additions & 0 deletions src/misc/util/helpers/closest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// element-closest | CC0-1.0 | github.com/jonathantneal/closest

/** @this {Foo} */
if (typeof Element !== "undefined") {
if (typeof Element.prototype.matches !== "function") {
Element.prototype.matches = Element.prototype.msMatchesSelector ||
(Element as any).prototype["mozMatchesSelector"] ||
Element.prototype.webkitMatchesSelector ||
function matches(selector:any):boolean {
const element = this;
const elements = (element.document || element.ownerDocument).querySelectorAll(selector);
let index = 0;

while (elements[index] && elements[index] !== element) {
++index;
}

return Boolean(elements[index]);
};
}

if (typeof Element.prototype["closest"] !== "function") {
Element.prototype["closest"] = function closest(selector:any):any {
let element = this;

while (element && element.nodeType === 1) {
if (element.matches(selector)) {
return element;
}

element = element.parentNode;
}

return undefined;
};
}
}
2 changes: 1 addition & 1 deletion src/modules/checkbox/components/checkbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export class SuiCheckbox implements ICustomValueAccessorHost<boolean> {
}

@HostListener("mousedown", ["$event"])
public onMouseDown(e:MouseEvent):void {
public onMouseDown(e:any):void {
e.preventDefault();
}

Expand Down
2 changes: 1 addition & 1 deletion src/modules/checkbox/components/radio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export class SuiRadio<T> implements ICustomValueAccessorHost<T> {
}

@HostListener("mousedown", ["$event"])
public onMouseDown(e:MouseEvent):void {
public onMouseDown(e:any):void {
e.preventDefault();
}

Expand Down
2 changes: 1 addition & 1 deletion src/modules/datepicker/components/datepicker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class SuiDatepicker {
}

@HostListener("mousedown", ["$event"])
public onMouseDown(e:MouseEvent):void {
public onMouseDown(e:any):void {
e.preventDefault();
}
}
2 changes: 1 addition & 1 deletion src/modules/datepicker/directives/datepicker.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ export class SuiDatepickerDirective
}

@HostListener("keydown", ["$event"])
public onKeyDown(e:KeyboardEvent):void {
public onKeyDown(e:any):void {
if (e.keyCode === KeyCode.Escape) {
this.close();
}
Expand Down
4 changes: 2 additions & 2 deletions src/modules/datepicker/views/calendar-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export abstract class CalendarView implements AfterViewInit, OnDestroy {
this._type = viewType;
this.ranges = ranges;

this._documentKeyDownListener = renderer.listen("document", "keydown", (e:KeyboardEvent) => this.onDocumentKeyDown(e));
this._documentKeyDownListener = renderer.listen("document", "keydown", (e:any) => this.onDocumentKeyDown(e));
}

// Template Methods
Expand Down Expand Up @@ -111,7 +111,7 @@ export abstract class CalendarView implements AfterViewInit, OnDestroy {
}
}

private onDocumentKeyDown(e:KeyboardEvent):void {
private onDocumentKeyDown(e:any):void {
if (this._highlightedItem && e.keyCode === KeyCode.Enter) {
this.setDate(this._highlightedItem);
return;
Expand Down
8 changes: 4 additions & 4 deletions src/modules/dropdown/directives/dropdown-menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Transition, SuiTransition, TransitionController, TransitionDirection }
import { HandledEvent, IAugmentedElement, KeyCode } from "../../../misc/util/index";
import { DropdownService, DropdownAutoCloseType } from "../services/dropdown.service";
// Polyfill for IE
import "element-closest";
import "../../../misc/util/helpers/closest";

@Directive({
// We must attach to every '.item' as Angular doesn't support > selectors.
Expand Down Expand Up @@ -144,11 +144,11 @@ export class SuiDropdownMenu extends SuiTransition implements AfterContentInit,
this.menuAutoSelectFirst = false;
this.menuSelectedItemClass = "selected";

this._documentKeyDownListener = renderer.listen("document", "keydown", (e:KeyboardEvent) => this.onDocumentKeyDown(e));
this._documentKeyDownListener = renderer.listen("document", "keydown", (e:any) => this.onDocumentKeyDown(e));
}

@HostListener("click", ["$event"])
public onClick(e:HandledEvent & MouseEvent):void {
public onClick(e:HandledEvent & any):void {
if (!e.eventHandled) {
e.eventHandled = true;

Expand All @@ -162,7 +162,7 @@ export class SuiDropdownMenu extends SuiTransition implements AfterContentInit,
}
}

public onDocumentKeyDown(e:KeyboardEvent):void {
public onDocumentKeyDown(e:any):void {
// Only the root dropdown (i.e. not nested dropdowns) is responsible for keeping track of the currently selected item.
if (this._service.isOpen && !this._service.isNested) {
// Stop document events like scrolling while open.
Expand Down
2 changes: 1 addition & 1 deletion src/modules/dropdown/directives/dropdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export class SuiDropdown implements AfterContentInit {
}

@HostListener("keypress", ["$event"])
public onKeypress(e:HandledEvent & KeyboardEvent):void {
public onKeypress(e:HandledEvent & any):void {
// Block the keyboard event from being fired on parent dropdowns.
if (!e.eventHandled) {

Expand Down
28 changes: 15 additions & 13 deletions src/modules/modal/components/modal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import { ModalConfig, ModalSize } from "../classes/modal-config";
#modal>
<!-- Configurable close icon -->
<i class="close icon" *ngIf="isClosable" (click)="close()"></i>
<i class="close icon" [hidden]="!isClosable" (click)="close()"></i>
<!-- <ng-content> so that <sui-modal> can be used as a normal component. -->
<ng-content></ng-content>
<!-- @ViewChild reference so we can insert elements beside this div. -->
Expand Down Expand Up @@ -270,27 +270,29 @@ export class SuiModal<T, U> implements OnInit, AfterViewInit {

// Decides whether the modal needs to reposition to allow scrolling.
private updateScroll():void {
// Semantic UI modal margin is 3.5rem, which is relative to the global font size, so for compatibility:
const fontSize = parseFloat(window.getComputedStyle(document.documentElement).getPropertyValue("font-size"));
const margin = fontSize * 3.5;

// _mustAlwaysScroll works by stopping _mustScroll from being automatically updated, so it stays `true`.
if (!this._mustAlwaysScroll && this._modalElement) {
const element = this._modalElement.nativeElement as Element;

// The modal must scroll if the window height is smaller than the modal height + both margins.
this._mustScroll = window.innerHeight < element.clientHeight + margin * 2;
if (window) {
// Semantic UI modal margin is 3.5rem, which is relative to the global font size, so for compatibility:
const fontSize = parseFloat(window.getComputedStyle(document.documentElement).getPropertyValue("font-size"));
const margin = fontSize * 3.5;

// _mustAlwaysScroll works by stopping _mustScroll from being automatically updated, so it stays `true`.
if (!this._mustAlwaysScroll && this._modalElement) {
const element = this._modalElement.nativeElement as Element;

// The modal must scroll if the window height is smaller than the modal height + both margins.
this._mustScroll = window.innerHeight < element.clientHeight + margin * 2;
}
}
}

public onClick(e:MouseEvent):void {
public onClick(e:any):void {
// Makes sense here, as the modal shouldn't be attached to any DOM element.
e.stopPropagation();
}

// Document listener is fine here because nobody will enough modals open.
@HostListener("document:keyup", ["$event"])
public onDocumentKeyUp(e:KeyboardEvent):void {
public onDocumentKeyup(e:any):void {
if (e.keyCode === KeyCode.Escape) {
// Close automatically covers case of `!isClosable`, so check not needed.
this.close();
Expand Down
10 changes: 6 additions & 4 deletions src/modules/popup/classes/popup-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export abstract class SuiPopupController implements IPopup, OnDestroy {
// When the popup is closed (onClose fires on animation complete),
this.popup.onClose.subscribe(() => this.cleanup());

this._documentListener = renderer.listen("document", "click", (e:MouseEvent) => this.onDocumentClick(e));
this._documentListener = renderer.listen("document", "click", (e:any) => this.onDocumentClick(e));
}

public configure(config?:IPopupConfig):void {
Expand All @@ -53,8 +53,10 @@ export abstract class SuiPopupController implements IPopup, OnDestroy {
// Cancel the opening timer.
clearTimeout(this._openingTimeout);

// Start the popup opening after the specified delay interval.
this._openingTimeout = window.setTimeout(() => this.open(), this.popup.config.delay);
if (window) {
// Start the popup opening after the specified delay interval.
this._openingTimeout = window.setTimeout(() => this.open(), this.popup.config.delay);
}
}

public open():void {
Expand Down Expand Up @@ -141,7 +143,7 @@ export abstract class SuiPopupController implements IPopup, OnDestroy {
}
}

public onDocumentClick(e:MouseEvent):void {
public onDocumentClick(e:any):void {
// If the popup trigger is outside click,
if (this._componentRef && this.popup.config.trigger === PopupTrigger.OutsideClick) {
const target = e.target as Element;
Expand Down
4 changes: 2 additions & 2 deletions src/modules/popup/components/popup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ export class SuiPopup implements IPopup {

public close():void {
// Only attempt to close if currently open.
if (this.isOpen) {
if (this.isOpen && window) {
// Cancel all other transitions, and initiate the closing transition.
this.transitionController.stopAll();
this.transitionController.animate(
Expand All @@ -193,7 +193,7 @@ export class SuiPopup implements IPopup {
}

@HostListener("click", ["$event"])
public onClick(event:MouseEvent):void {
public onClick(event:any):void {
// Makes sense here, as the popup shouldn't be attached to any DOM element.
event.stopPropagation();
}
Expand Down
6 changes: 3 additions & 3 deletions src/modules/search/components/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ export class SuiSearch<T> implements AfterViewInit, OnDestroy {
this.transition = "scale";
this.transitionDuration = 200;

this._documentClickListener = renderer.listen("document", "click", (e:MouseEvent) => this.onDocumentClick(e));
this._documentClickListener = renderer.listen("document", "click", (e:any) => this.onDocumentClick(e));
}

public ngAfterViewInit():void {
Expand All @@ -226,7 +226,7 @@ export class SuiSearch<T> implements AfterViewInit, OnDestroy {
}
}

public onClick(e:MouseEvent):void {
public onClick(e:any):void {
this.open();
}

Expand All @@ -251,7 +251,7 @@ export class SuiSearch<T> implements AfterViewInit, OnDestroy {
}
}

public onDocumentClick(e:MouseEvent):void {
public onDocumentClick(e:any):void {
if (!this._element.nativeElement.contains(e.target)) {
this.dropdownService.setOpenState(false);
}
Expand Down
14 changes: 8 additions & 6 deletions src/modules/search/services/search.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,14 @@ export class SearchService<T, U> {
this._query = query;

clearTimeout(this._searchDelayTimeout);
this._searchDelayTimeout = window.setTimeout(
() => {
this.updateQuery(query, callback);
},
this.searchDelay
);
if (window) {
this._searchDelayTimeout = window.setTimeout(
() => {
this.updateQuery(query, callback);
},
this.searchDelay
);
}
}

// Updates the current search query.
Expand Down
8 changes: 4 additions & 4 deletions src/modules/select/classes/select-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ export abstract class SuiSelectBase<T, U> implements AfterContentInit, OnDestroy
this.transitionDuration = 200;

this.onTouched = new EventEmitter<void>();
this._documentKeyDownListener = renderer.listen("document", "keydown", (e:KeyboardEvent) => this.onDocumentKeyDown(e));
this._documentKeyDownListener = renderer.listen("document", "keydown", (e:any) => this.onDocumentKeyDown(e));

this._selectClasses = true;
}
Expand Down Expand Up @@ -378,15 +378,15 @@ export abstract class SuiSelectBase<T, U> implements AfterContentInit, OnDestroy
}

@HostListener("keypress", ["$event"])
public onKeyPress(e:KeyboardEvent):void {
public onKeyPress(e:any):void {
if (e.keyCode === KeyCode.Enter) {
// Enables support for focussing and opening with the keyboard alone.
// Using directly because Renderer2 doesn't have invokeElementMethod method anymore.
this._element.nativeElement.click();
}
}

public onDocumentKeyDown(e:KeyboardEvent):void {
public onDocumentKeyDown(e:any):void {
if (this._element.nativeElement.contains(e.target) &&
!this.dropdownService.isOpen &&
e.keyCode === KeyCode.Down) {
Expand All @@ -399,7 +399,7 @@ export abstract class SuiSelectBase<T, U> implements AfterContentInit, OnDestroy
}
}

public onQueryInputKeydown(event:KeyboardEvent):void {}
public onQueryInputKeydown(event:any):void {}

protected focus():void {
if (this.isSearchable && this.searchInput) {
Expand Down
2 changes: 1 addition & 1 deletion src/modules/select/components/multi-select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ export class SuiMultiSelect<T, U> extends SuiSelectBase<T, U> implements ICustom
}
}

public onQueryInputKeydown(event:KeyboardEvent):void {
public onQueryInputKeydown(event:any):void {
if (event.keyCode === KeyCode.Backspace && this.query === "" && this.selectedOptions.length > 0) {
// Deselect the rightmost option when the user presses backspace in the search input.
this.deselectOption(this.selectedOptions[this.selectedOptions.length - 1]);
Expand Down
6 changes: 3 additions & 3 deletions src/modules/select/directives/select-search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ export class SuiSelectSearch {
}

public onQueryUpdated:EventEmitter<string>;
public onQueryKeyDown:EventEmitter<KeyboardEvent>;
public onQueryKeyDown:EventEmitter<any>;

constructor(private _renderer:Renderer2, private _element:ElementRef) {
this.onQueryUpdated = new EventEmitter<string>();
this.onQueryKeyDown = new EventEmitter<KeyboardEvent>();
this.onQueryKeyDown = new EventEmitter<any>();

this._searchClass = true;
this._autoComplete = "off";
Expand All @@ -31,7 +31,7 @@ export class SuiSelectSearch {
}

@HostListener("keydown", ["$event"])
private onKeyDown(e:KeyboardEvent):void {
private onKeyDown(e:any):void {
this.onQueryKeyDown.emit(e);
}

Expand Down
2 changes: 1 addition & 1 deletion src/modules/sidebar/components/sidebar-sibling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export class SuiSidebarSibling {
}

@HostListener("click", ["$event"])
public onClick(event:MouseEvent):void {
public onClick(event:any):void {
if (this.service.isVisible && !this.service.wasJustOpened) {
this.service.setVisibleState(false);
}
Expand Down
4 changes: 3 additions & 1 deletion src/modules/sidebar/services/sidebar.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ export class SidebarService {

setTimeout(() => this.wasJustOpened = false);
clearTimeout(this._isAnimatingTimeout);
this._isAnimatingTimeout = window.setTimeout(() => this.isAnimating = false, 500);
if (window) {
this._isAnimatingTimeout = window.setTimeout(() => this.isAnimating = false, 500);
}
}
}

Expand Down

0 comments on commit 77eaabf

Please sign in to comment.