ancora su Typescript...

suddiviso i ts dai html e scss
This commit is contained in:
paolo
2018-11-07 22:42:22 +01:00
parent 7c1d07797e
commit 6811202571
31 changed files with 8875 additions and 314 deletions

62
src/common/axios.ts Normal file
View File

@@ -0,0 +1,62 @@
import {
default as Axios,
AxiosError,
AxiosRequestConfig,
AxiosResponse
} from "axios";
import { default as VueRouter } from "vue-router";
import { TokenHelper } from "./token-helper";
let initialized: boolean = false;
interface IRequestConfig extends AxiosRequestConfig {
ignore: number[];
}
function handle(status: number, exclude: number[]) {
if (exclude.length === 0) return true;
else return exclude.find(o => o === status) === undefined;
}
export function UseAxios(router: VueRouter) {
if (!initialized) {
Axios.interceptors.request.use((config: IRequestConfig) => {
if (!config.headers["Authorization"]) {
// append authorization header
let bearerToken = TokenHelper.getBearerToken();
if (bearerToken.Authorization)
Object.assign(config.headers, bearerToken);
}
if (!config.maxRedirects || config.maxRedirects === 5)
// ensure axios does not follow redirects, so custom response interceptor below can push to app login page
config.maxRedirects = 0;
return config;
});
Axios.interceptors.response.use(undefined, (config: AxiosError) => {
let response: AxiosResponse = config.response;
let exclude = (<IRequestConfig>config.config).ignore || [];
if (response.status === 401 && handle(response.status, exclude)) {
let location: string =
response.headers["location"] || response.headers["Location"];
if (location) {
let redirectTo = "/" + location;
window.setTimeout(() => router.replace(redirectTo), 200);
}
}
if (response.status === 403 && handle(response.status, exclude)) {
window.setTimeout(() => router.replace("/forbidden"), 200);
}
return config;
});
initialized = true;
}
}

50
src/common/debounce.ts Normal file
View File

@@ -0,0 +1,50 @@
/**
* Returns a function, that, as long as it continues to be invoked, will not
* be triggered. The function will be called after it stops being called for
* N milliseconds. If `immediate` is passed, trigger the function on the
* leading edge, instead of the trailing. The function also has a property 'clear'
* that is a function which will clear the timer to prevent previously scheduled executions.
*
* @source underscore.js
* @see http://unscriptable.com/2009/03/20/debouncing-javascript-methods/
* @param {Function} function to wrap
* @param {Number} timeout in ms (`100`)
* @param {Boolean} whether to execute at the beginning (`false`)
* @api public
*/
export function Debounce(func: Function, wait?: number, immediate?: boolean) {
let timeout, args, context, timestamp, result
if (null == wait) wait = 100
function later() {
let last = Date.now() - timestamp
if (last < wait && last > 0) {
timeout = setTimeout(later, wait - last)
} else {
timeout = null
if (!immediate) {
result = func.apply(context, args)
context = args = null
}
}
}
let debounced = function () {
context = this
args = arguments
timestamp = Date.now()
let callNow = immediate && !timeout
if (!timeout) timeout = setTimeout(later, wait)
if (callNow) {
result = func.apply(context, args)
context = args = null
}
return result
}
return debounced
}

3
src/common/index.ts Normal file
View File

@@ -0,0 +1,3 @@
export * from './pattern'
export * from './axios'
export * from './debounce'

19
src/common/pattern.ts Normal file
View File

@@ -0,0 +1,19 @@
export class Patterns {
/**
* Alphanumeric, spaces and dashes allowed. Min 2 characters length.
*/
public static DisplayName: RegExp = /^[0-9a-zA-Z\s\-]{2,}/i;
/**
* Same pattern used by JQuery userName validation
*/
public static Email: RegExp = /^((“[\w-\s]+”)|([\w-]+(?:\.[\w-]+)*)|(“[\w-\s]+”)([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}[0-9];{1,2})\]?$)/i;
/**
* 6 to 20 characters string with at least one digit, one upper case letter, one lower case letter and one special symbol
*/
public static Password: RegExp = /^((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{6,20})/i;
}