utility.ts 4.82 KB
Newer Older
Dmytro Bogatov's avatar
Dmytro Bogatov committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
import "bootstrap-notify";


/**
 * Static class that provides useful utility methods.
 * 
 * @export
 * @class Utility
 */
export class Utility {

	/**
	 * Returns properly formatted GET request query URL.
	 * 
	 * @static
	 * @param {string} url - URL of the endpoint
	 * @param {...[string, string][]} parameters - set of tuples: param - value
	 * @returns {string} - proper URL for GET request
	 * 
	 * @memberOf Utility
	 */
	public static generateQuery(url: string, ...parameters: [string, string][]): string {

		let result = `${url}?`;
		for (var param of parameters) {
			result += `${param[0]}=${param[1]}&`;
		}

		if (result.charAt(result.length - 1) == '&') {
			result.substr(0, result.length - 1);
		}

		return result;
	}

	/**
	 * GET request AJAX wrapper that works on promises
	 * 
	 * @static
	 * @param {string} url - endpoint URL
	 * @returns {Promise<any>} - promise of get request result
	 * 
	 * @memberOf Utility
	 */
	public static async get(url: string): Promise<any> {
		return new Promise((resolve, reject) => {

			$.ajax(url, {
				type: "GET",
				dataType: "json",
				statusCode: {
					204: (result) => { resolve([]) },
					200: (result) => { resolve(result) }
				},
				error: (result) => { reject(result) }
			});
		});
	}


	/**
	 * DELETE request AJAX wrapper that works on promises
	 * 
	 * @static
	 * @param {string} url - endpoint URL
	 * @param {*} data - request parameters
	 * @returns {Promise<any>} - promise of delete request result
	 * 
	 * @memberOf Utility
	 */
	public static async delete(url: string, data: any): Promise<any> {
		return new Promise((resolve, reject) => {

			$.ajax(url, {
				type: "DELETE",
				data: data,
				statusCode: {
					200: (result) => { resolve(result) }
				},
				error: (result) => { reject(result) }
			});
		});
	}

	/**
Dmytro Bogatov's avatar
Dmytro Bogatov committed
86 87
	 * Converts .NET timestamp (ISO8601) string to TS Date
	 * Thanks to https://stackoverflow.com/a/20223090/1644554
Dmytro Bogatov's avatar
Dmytro Bogatov committed
88 89 90 91 92 93 94 95
	 * 
	 * @static
	 * @param {string} dotNetDate - .NET formatted timestamp
	 * @returns {Date} - equivalent TS Date object
	 * 
	 * @memberOf Utility
	 */
	public static toDate(dotNetDate: string): Date {
Dmytro Bogatov's avatar
Dmytro Bogatov committed
96 97 98 99 100
		var parts = dotNetDate.match(/\d+/g).map(el => parseInt(el));
		var isoTime = Date.UTC(parts[0], parts[1] - 1, parts[2], parts[3], parts[4], parts[5]);
		var isoDate = new Date(isoTime);

		return isoDate;
Dmytro Bogatov's avatar
Dmytro Bogatov committed
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
	}

	/**
	 * Mutes thread for a time interval
	 * 
	 * @static
	 * @param {number} time - time interval in milliseconds
	 * @returns {Promise<any>} 
	 * 
	 * @memberOf Utility
	 */
	public static async sleep(time: number): Promise<any> {
		return new Promise(resolve => window.setTimeout(resolve, time));
	}

	/**
	 * Returns nicely looking string representing time.
	 * Example: 3:09:05 PM
	 * 
	 * @static
	 * @param {Date} date - date to display
	 * @returns {string} - string representation of the date
	 * 
	 * @memberOf Utility
	 */
	public static toHHMMSS(date: Date): string {
		let hours = date.getHours();

		let ampm = hours >= 12 ? 'PM' : 'AM';

		hours = hours % 12;
		hours = hours ? hours : 12; // the hour '0' should be '12'

		let minutes = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
		let seconds = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();

		return `${hours}:${minutes}:${seconds} ${ampm}`;
	}

	// http://stackoverflow.com/a/18330682/1644554
	public static toLocalTimezone(date: Date): Date {
		let newDate = new Date(date.getTime() + date.getTimezoneOffset() * 60 * 1000);

		let offset = date.getTimezoneOffset() / 60;
		let hours = date.getHours();

		newDate.setHours(hours - offset);

		return newDate;
	}

	public static fixUtcTime(): void {

		$(".utc-time").each(function () {
			$(this).text(
				Utility.toHHMMSS(
					Utility.toLocalTimezone(
						new Date($(this).text())
					)
				)
			);
		});

Dmytro Bogatov's avatar
Fix.  
Dmytro Bogatov committed
164 165 166 167 168 169 170 171
		$(".utc-date").each(function () {
			$(this).text(
				Utility.toLocalTimezone(
					new Date($(this).text())
				).toString()
			);
		});

Dmytro Bogatov's avatar
Dmytro Bogatov committed
172 173
	}

Dmytro Bogatov's avatar
Fix.  
Dmytro Bogatov committed
174
	public static toUtcDate(date: Date): number {
Dmytro Bogatov's avatar
Dmytro Bogatov committed
175
		return Date.UTC(
Dmytro Bogatov's avatar
Fix.  
Dmytro Bogatov committed
176 177 178 179 180 181
			date.getUTCFullYear(),
			date.getUTCMonth(),
			date.getUTCDate(),
			date.getUTCHours(),
			date.getUTCMinutes(),
			date.getUTCSeconds(),
Dmytro Bogatov's avatar
Dmytro Bogatov committed
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
			date.getUTCMilliseconds()
		);
	}
}

/**
 * Static class that provides useful UI related methods.
 * 
 * @export
 * @class UIHelper
 */
export class UIHelper {

	/**
	 * Triggers notification message in the UI.
	 * Uses "bootstrap-notify" library.
	 * 
	 * @static
	 * @param {any} message 
	 * @param {any} type 
	 * 
	 * @memberOf UIHelper
	 */
	public static notify(message, type) {
		$.notify(
			{
				message: message
			},
			{
				type: type,
				allow_dismiss: true,
				allow_duplicates: false,
				timer: 1000,
				placement: {
					from: 'bottom',
					align: 'left'
				},
				delay: 500,
				animate: {
					enter: 'animated fadeInUp',
					exit: 'animated fadeOutDown'
				},
				offset: {
					x: 30,
					y: 30
				}
			}
		);
	};
}