Skip to content

Commit f41c02c

Browse files
committed
TS: Converted app file and animations service
Extracted functions out of app file during changes to clean up. Altered animation function to use normal css prop names instead of JS CSS prop names.
1 parent 2e8d6ce commit f41c02c

File tree

11 files changed

+108
-84
lines changed

11 files changed

+108
-84
lines changed

resources/js/app.js

Lines changed: 0 additions & 33 deletions
This file was deleted.

resources/js/app.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import {EventManager} from './services/events';
2+
import {HttpManager} from './services/http';
3+
import {Translator} from './services/translations';
4+
import * as componentMap from './components/index';
5+
import {ComponentStore} from './services/components';
6+
import {baseUrl, importVersioned} from "./services/util";
7+
8+
// eslint-disable-next-line no-underscore-dangle
9+
window.__DEV__ = false;
10+
11+
// Make common important util functions global
12+
window.baseUrl = baseUrl;
13+
window.importVersioned = importVersioned;
14+
15+
// Setup events, http & translation services
16+
window.$http = new HttpManager();
17+
window.$events = new EventManager();
18+
window.$trans = new Translator();
19+
20+
// Load & initialise components
21+
window.$components = new ComponentStore();
22+
window.$components.register(componentMap);
23+
window.$components.init();

resources/js/components/chapter-contents.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {slideUp, slideDown} from '../services/animations';
1+
import {slideUp, slideDown} from '../services/animations.ts';
22
import {Component} from './component';
33

44
export class ChapterContents extends Component {

resources/js/components/collapsible.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {slideDown, slideUp} from '../services/animations';
1+
import {slideDown, slideUp} from '../services/animations.ts';
22
import {Component} from './component';
33

44
/**

resources/js/components/dropdown-search.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {debounce} from '../services/util.ts';
2-
import {transitionHeight} from '../services/animations';
2+
import {transitionHeight} from '../services/animations.ts';
33
import {Component} from './component';
44

55
export class DropdownSearch extends Component {

resources/js/components/expand-toggle.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {slideUp, slideDown} from '../services/animations';
1+
import {slideUp, slideDown} from '../services/animations.ts';
22
import {Component} from './component';
33

44
export class ExpandToggle extends Component {
File renamed without changes.

resources/js/components/popup.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {fadeIn, fadeOut} from '../services/animations';
1+
import {fadeIn, fadeOut} from '../services/animations.ts';
22
import {onSelect} from '../services/dom';
33
import {Component} from './component';
44

resources/js/global.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ declare global {
77
const __DEV__: boolean;
88

99
interface Window {
10+
__DEV__: boolean;
1011
$components: ComponentStore;
1112
$events: EventManager;
1213
$trans: Translator;
1314
$http: HttpManager;
1415
baseUrl: (path: string) => string;
16+
importVersioned: (module: string) => Promise<object>;
1517
}
1618
}

resources/js/services/animations.js renamed to resources/js/services/animations.ts

Lines changed: 32 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
11
/**
22
* Used in the function below to store references of clean-up functions.
33
* Used to ensure only one transitionend function exists at any time.
4-
* @type {WeakMap<object, any>}
54
*/
6-
const animateStylesCleanupMap = new WeakMap();
5+
const animateStylesCleanupMap: WeakMap<object, any> = new WeakMap();
76

87
/**
98
* Animate the css styles of an element using FLIP animation techniques.
10-
* Styles must be an object where the keys are style properties, camelcase, and the values
9+
* Styles must be an object where the keys are style rule names and the values
1110
* are an array of two items in the format [initialValue, finalValue]
12-
* @param {Element} element
13-
* @param {Object} styles
14-
* @param {Number} animTime
15-
* @param {Function} onComplete
1611
*/
17-
function animateStyles(element, styles, animTime = 400, onComplete = null) {
12+
function animateStyles(
13+
element: HTMLElement,
14+
styles: Record<string, string[]>,
15+
animTime: number = 400,
16+
onComplete: Function | null = null
17+
): void {
1818
const styleNames = Object.keys(styles);
1919
for (const style of styleNames) {
20-
element.style[style] = styles[style][0];
20+
element.style.setProperty(style, styles[style][0]);
2121
}
2222

2323
const cleanup = () => {
2424
for (const style of styleNames) {
25-
element.style[style] = null;
25+
element.style.removeProperty(style);
2626
}
27-
element.style.transition = null;
27+
element.style.removeProperty('transition');
2828
element.removeEventListener('transitionend', cleanup);
2929
animateStylesCleanupMap.delete(element);
3030
if (onComplete) onComplete();
@@ -33,7 +33,7 @@ function animateStyles(element, styles, animTime = 400, onComplete = null) {
3333
setTimeout(() => {
3434
element.style.transition = `all ease-in-out ${animTime}ms`;
3535
for (const style of styleNames) {
36-
element.style[style] = styles[style][1];
36+
element.style.setProperty(style, styles[style][1]);
3737
}
3838

3939
element.addEventListener('transitionend', cleanup);
@@ -43,9 +43,8 @@ function animateStyles(element, styles, animTime = 400, onComplete = null) {
4343

4444
/**
4545
* Run the active cleanup action for the given element.
46-
* @param {Element} element
4746
*/
48-
function cleanupExistingElementAnimation(element) {
47+
function cleanupExistingElementAnimation(element: Element) {
4948
if (animateStylesCleanupMap.has(element)) {
5049
const oldCleanup = animateStylesCleanupMap.get(element);
5150
oldCleanup();
@@ -54,30 +53,24 @@ function cleanupExistingElementAnimation(element) {
5453

5554
/**
5655
* Fade in the given element.
57-
* @param {Element} element
58-
* @param {Number} animTime
59-
* @param {Function|null} onComplete
6056
*/
61-
export function fadeIn(element, animTime = 400, onComplete = null) {
57+
export function fadeIn(element: HTMLElement, animTime: number = 400, onComplete: Function | null = null): void {
6258
cleanupExistingElementAnimation(element);
6359
element.style.display = 'block';
6460
animateStyles(element, {
65-
opacity: ['0', '1'],
61+
'opacity': ['0', '1'],
6662
}, animTime, () => {
6763
if (onComplete) onComplete();
6864
});
6965
}
7066

7167
/**
7268
* Fade out the given element.
73-
* @param {Element} element
74-
* @param {Number} animTime
75-
* @param {Function|null} onComplete
7669
*/
77-
export function fadeOut(element, animTime = 400, onComplete = null) {
70+
export function fadeOut(element: HTMLElement, animTime: number = 400, onComplete: Function | null = null): void {
7871
cleanupExistingElementAnimation(element);
7972
animateStyles(element, {
80-
opacity: ['1', '0'],
73+
'opacity': ['1', '0'],
8174
}, animTime, () => {
8275
element.style.display = 'none';
8376
if (onComplete) onComplete();
@@ -86,20 +79,18 @@ export function fadeOut(element, animTime = 400, onComplete = null) {
8679

8780
/**
8881
* Hide the element by sliding the contents upwards.
89-
* @param {Element} element
90-
* @param {Number} animTime
9182
*/
92-
export function slideUp(element, animTime = 400) {
83+
export function slideUp(element: HTMLElement, animTime: number = 400) {
9384
cleanupExistingElementAnimation(element);
9485
const currentHeight = element.getBoundingClientRect().height;
9586
const computedStyles = getComputedStyle(element);
9687
const currentPaddingTop = computedStyles.getPropertyValue('padding-top');
9788
const currentPaddingBottom = computedStyles.getPropertyValue('padding-bottom');
9889
const animStyles = {
99-
maxHeight: [`${currentHeight}px`, '0px'],
100-
overflow: ['hidden', 'hidden'],
101-
paddingTop: [currentPaddingTop, '0px'],
102-
paddingBottom: [currentPaddingBottom, '0px'],
90+
'max-height': [`${currentHeight}px`, '0px'],
91+
'overflow': ['hidden', 'hidden'],
92+
'padding-top': [currentPaddingTop, '0px'],
93+
'padding-bottom': [currentPaddingBottom, '0px'],
10394
};
10495

10596
animateStyles(element, animStyles, animTime, () => {
@@ -109,21 +100,19 @@ export function slideUp(element, animTime = 400) {
109100

110101
/**
111102
* Show the given element by expanding the contents.
112-
* @param {Element} element - Element to animate
113-
* @param {Number} animTime - Animation time in ms
114103
*/
115-
export function slideDown(element, animTime = 400) {
104+
export function slideDown(element: HTMLElement, animTime: number = 400) {
116105
cleanupExistingElementAnimation(element);
117106
element.style.display = 'block';
118107
const targetHeight = element.getBoundingClientRect().height;
119108
const computedStyles = getComputedStyle(element);
120109
const targetPaddingTop = computedStyles.getPropertyValue('padding-top');
121110
const targetPaddingBottom = computedStyles.getPropertyValue('padding-bottom');
122111
const animStyles = {
123-
maxHeight: ['0px', `${targetHeight}px`],
124-
overflow: ['hidden', 'hidden'],
125-
paddingTop: ['0px', targetPaddingTop],
126-
paddingBottom: ['0px', targetPaddingBottom],
112+
'max-height': ['0px', `${targetHeight}px`],
113+
'overflow': ['hidden', 'hidden'],
114+
'padding-top': ['0px', targetPaddingTop],
115+
'padding-bottom': ['0px', targetPaddingBottom],
127116
};
128117

129118
animateStyles(element, animStyles, animTime);
@@ -134,11 +123,8 @@ export function slideDown(element, animTime = 400) {
134123
* Call with first state, and you'll receive a function in return.
135124
* Call the returned function in the second state to animate between those two states.
136125
* If animating to/from 0-height use the slide-up/slide down as easier alternatives.
137-
* @param {Element} element - Element to animate
138-
* @param {Number} animTime - Animation time in ms
139-
* @returns {function} - Function to run in second state to trigger animation.
140126
*/
141-
export function transitionHeight(element, animTime = 400) {
127+
export function transitionHeight(element: HTMLElement, animTime: number = 400): () => void {
142128
const startHeight = element.getBoundingClientRect().height;
143129
const initialComputedStyles = getComputedStyle(element);
144130
const startPaddingTop = initialComputedStyles.getPropertyValue('padding-top');
@@ -151,10 +137,10 @@ export function transitionHeight(element, animTime = 400) {
151137
const targetPaddingTop = computedStyles.getPropertyValue('padding-top');
152138
const targetPaddingBottom = computedStyles.getPropertyValue('padding-bottom');
153139
const animStyles = {
154-
height: [`${startHeight}px`, `${targetHeight}px`],
155-
overflow: ['hidden', 'hidden'],
156-
paddingTop: [startPaddingTop, targetPaddingTop],
157-
paddingBottom: [startPaddingBottom, targetPaddingBottom],
140+
'height': [`${startHeight}px`, `${targetHeight}px`],
141+
'overflow': ['hidden', 'hidden'],
142+
'padding-top': [startPaddingTop, targetPaddingTop],
143+
'padding-bottom': [startPaddingBottom, targetPaddingBottom],
158144
};
159145

160146
animateStyles(element, animStyles, animTime);

0 commit comments

Comments
 (0)