export function Rand ( max ) { const r = Math . floor ( Math . random ( ) * max ) ; return ( r < max ) ? r : max - 1 ; } export namespace Unit { export function CmToInch ( cm ) { return cm / 2.54 ; } export function InchToFoot ( inch ) { return inch / 12 ; } export function MToFoot ( m ) { return m * 3.28 ; } export function KgToPound ( kg ) { return kg * 2.2 ; } } interface IGenerateFileOpts { script : string ; filename : string ; content : string ; } let GenerateFile = ( options ) => { options . script = options . script || "download.php" ; if ( ! options . filename || ! options . content ) { throw new Error ( "Please enter all the required config options!" ) ; } const iframe = $ ( "<iframe>" , { width : 1 , height : 1 , frameborder : 0 , css : { display : "none" , } , } ) . appendTo ( "body" ) ; const formHTML = '<form action="" method="post">' + '<input type="hidden" name="filename" />' + '<input type="hidden" name="content" />' + "</form>" ; setTimeout ( ( ) => { let body = ( iframe . prop ( "contentDocument" ) !== undefined ) ? iframe . prop ( "contentDocument" ) . body : iframe . prop ( "document" ) . body ; body = $ ( body ) ; body . html ( formHTML ) ; const form = body . find ( "form" ) ; form . attr ( "action" , options . script ) ; form . find ( "input[name=filename]" ) . val ( options . filename ) ; form . find ( "input[name=content]" ) . val ( options . content ) ; form . submit ( ) ; } , 50 ) ; } ; GenerateFile . canSaveOffline = false ; ( ( ) => { let lnk ; try { new Blob ( [ JSON . stringify ( { obj : "discarded" } ) ] , { type : "application/json" } ) ; const fl = new File ( [ JSON . stringify ( { obj : "discarded" } ) ] , "FoE.json" , { type : "application/json" } ) ; lnk = document . createElement ( "a" ) ; if ( typeof lnk . hidden !== "boolean" || typeof lnk . href !== "string" || typeof lnk . download !== "string" || typeof lnk . click !== "function" ) { throw new Error ( "Anchor element does not support all necessary features" ) ; } const url = URL . createObjectURL ( fl ) ; URL . revokeObjectURL ( url ) ; lnk . hidden = true ; lnk . download = "application/json" ; document . body . appendChild ( lnk ) ; } catch ( msg ) { console . log ( 'Desired features not supported. "Save to file" can only work through server. ' , msg ) ; return ; } GenerateFile = ( options ) => { if ( ! options . filename || ! options . content ) { throw new Error ( "Please enter all the required config options!" ) ; } if ( lnk . href !== "" ) { URL . revokeObjectURL ( lnk . href ) ; } const fl = new File ( [ options . content ] , options . filename , { type : "application/json" } ) ; lnk . href = URL . createObjectURL ( fl ) ; lnk . download = options . filename ; lnk . click ( ) ; } ; GenerateFile . canSaveOffline = true ; } ) ( ) ; export { GenerateFile } ;