Share your advanced knowledge/code with the community.
poshu
 
Posts: 58
Joined: Mon Feb 24, 2014 11:46 pm

JSTools

by poshu Tue Jul 10, 2018 9:19 am

This is a short module containing JS procedures that I find myself using on almost all my projects. Some I found on this forum, some on stack overflow and some I wrote myself.
Code: Select all
DeclareModule JSTools
   
   Declare.s b64EncodeUnicode(str.s)
   Declare.s b64DecodeUnicode(str.s)
   Declare.i GadgetElement(Gadget, UseJquery.b=#True)
   Declare.s GetLocalLanguage()
   Declare.i IsThisCordova()
   Declare.i LoadCSS(FileName.s, *Callback,Tag)
   Declare.i LoadScript(FileName.s, *OnLoadFunction, Tag.i)
   Declare.i WindowElement(Window, UseJquery.b=#True)
   Declare.s uuidv4()
   Declare.i DestroyDebugOutput()
EndDeclareModule

Module JSTools
   EnableExplicit
   
   ; Private declarations
   Declare   LoadAsynchronously(FileName.s, *Callback, FileType.s,Tag.i)
   
   ; Public procedures
   
   Procedure.s b64EncodeUnicode(str.s) ; Encode a unicode string in base 64 see : https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding
      !return btoa(encodeURIComponent(v_str).replace(/%([0-9A-F]{2})/g,
      !   function toSolidBytes(match, p1) {
      !      return String.fromCharCode('0x' + p1);
      !}));
   EndProcedure
   
   Procedure.s b64DecodeUnicode(str.s) ; Decode a unicode string in base 64
      !return decodeURIComponent(atob(v_str).split('').map(function(c) {
      !   return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
      !}).join(''));
   EndProcedure
   
   Procedure.i GadgetElement(Gadget, UseJquery.b=#True)  ; Get the DOM element for a gadget
      Protected gadgetObject=GadgetID(Gadget), result
      !v_result =  (v_gadgetobject && v_gadgetobject.div)? v_usejquery? $(v_gadgetobject.div):v_gadgetobject.div:null;
      ProcedureReturn result
   EndProcedure
   
   Procedure.i WindowElement(Window, UseJquery.b=#True) ; Get the DOM element for a window
      Protected winObject=WindowID(Window), result
      !v_result = (v_winobject && v_winobject.element)? v_usejquery? $(v_winobject.element):v_winobject.element:null;
      ProcedureReturn result
   EndProcedure
      
   Procedure.i LoadCSS(FileName.s, *Callback,Tag.i) ; Load a CSS. Callback should be : callback(Tag,Result,URL,Error)
      Protected jqxhr=LoadAsynchronously(FileName, *Callback, "text",Tag), result
      !result = v_jqxhr.done(function(data, status, jqxhr) {
      !   $('<style></style>').appendTo('head').html(data);
      !})
      ProcedureReturn result
   EndProcedure
   
   Procedure.i LoadScript(FileName.s, *OnLoadFunction, Tag.i) ; Load a js file. Callback should be : callback(Tag,Result,URL,Error)
      ProcedureReturn LoadAsynchronously(FileName, *OnLoadFunction, "script", Tag)
   EndProcedure
   
   Procedure.s uuidv4() ; Generate an UUID compliant with RFC 4122
      Protected Result.s
      !  v_result = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
      !    var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
      !    return v.toString(16);
      !  });
      ProcedureReturn Result
   EndProcedure
   
   Procedure.i IsThisCordova() ; Will return true if executed in Cordova (iOS/Android export for Spider Basic)
      Protected result
      !if (typeof cordova !== 'undefined') {
      result =  #True
      !}
      
      ProcedureReturn result
   EndProcedure
   
   Procedure.s GetLocalLanguage() ; Will return the user language as a string (EN, FR, etc...)
      Protected result.s
      !var lang = navigator.language.split("-");
      !var v_result = (lang[0]);
      
      ProcedureReturn result
   EndProcedure

   Procedure DestroyDebugOutput() ; Close the debug window for real, to avoid background window misalignment.
      !$('div').eq(0).remove()
   EndProcedure

   ; Private procedures
   
   Procedure.i LoadAsynchronously(FileName.s, *Callback, FileType.s,Tag.i)
      Protected result
      !v_result = $.ajax({ url:v_filename, dataType:v_filetype, beforeSend:function(jqxhr, settings) { jqxhr.url = settings.url; } })
      !.done(function(data, status, jqxhr) { p_callback(v_tag,status,jqxhr.url); })
      !.fail(function(jqxhr, status, errorThrown) { p_callback(v_tag,status,jqxhr.url,errorThrown); });
      ProcedureReturn result
   EndProcedure
   
EndModule
Last edited by poshu on Fri Jul 13, 2018 6:48 pm, edited 3 times in total.
poshu
 
Posts: 58
Joined: Mon Feb 24, 2014 11:46 pm

Re: JSTools

by poshu Fri Jul 13, 2018 6:43 pm

+ Added : DestroyDebugOutput() : CloseDebugOutput() only hide the debug window. when associated with PB_Window_Background, it can result in some misalignment :

Image

DestroyDebugOutput() really closes it!

Thanks to @Falsam.
Return to Tricks 'n' Tips

Who is online

Users browsing this forum: No registered users and 2 guests