- communication/action between IFRAME elements and the parent page
- using srcdoc polyfill (for IE9 compatibility)
Code: Select all
; ************************
; filename : WebGadgetUtils.sbi
; ************************
Procedure SetWebGadgetHTMLText(WebGadget, ContentText.s)
Static isSrcDocPluginReady.b
If Not isSrcDocPluginReady
isSrcDocPluginReady=#True
;// Remark: this is a plugin only for IE compatibility.
;// I load the plugin here instead of using $.GetScript(...) because IE cannot load script from github CDN
!/*! srcdoc-polyfill - v0.1.1 - 2013-03-01
!* http://github.com/jugglinmike/srcdoc-polyfill/
!* Copyright (c) 2013 Mike Pennisi; Licensed MIT */
!(function(t,e){var c,n,o=t.srcDoc,r=!!("srcdoc"in e.createElement("iframe")),i={compliant:function(t,e){e&&t.setAttribute("srcdoc",e)},legacy:function(t,e){var c;t&&t.getAttribute&&(e?t.setAttribute("srcdoc",e):e=t.getAttribute("srcdoc"),e&&(c="javascript: window.frameElement.getAttribute('srcdoc');",t.setAttribute("src",c),t.contentWindow&&(t.contentWindow.location=c)))}},s=t.srcDoc={set:i.compliant,noConflict:function(){return t.srcDoc=o,s}};if(!r)for(s.set=i.legacy,n=e.getElementsByTagName("iframe"),c=n.length;c--;)s.set(n[c])})(this,this.document);
EndIf
Protected gadgetId=GadgetID(WebGadget)
!var iframe=v_gadgetId.gadget;
!srcDoc.set(iframe,v_ContentText);
EndProcedure
Procedure.s CallPageCommand(Command.i, Parameters.s="()")
Parameters=ReplaceString(Parameters,"'","'")
Parameters=ReplaceString(Parameters,#DQUOTE$,""")
Protected cmd.s="parent.window.PB_PageCommand["+Command+"]"+Parameters
ProcedureReturn cmd
EndProcedure
Procedure.i AddPageCommand(*CommandProcedure)
!var cmdKey='PB_PageCommand';
!var cmds=window[cmdKey] ? window[cmdKey] : [];
!var cmdIndex=cmds.push(p_CommandProcedure)-1;
!window[cmdKey]=cmds
!return cmdIndex;
EndProcedure
Procedure AddWebGadgetElement(WebGadget,NewElement.s,TargetElement.s="body")
Protected gadgetId=GadgetID(WebGadget)
!$(v_NewElement).appendTo($(v_gadgetId.gadget).contents().find(v_TargetElement));
EndProcedure
CompilerIf #PB_Compiler_IsMainFile
; ***************************************************
; EXAMPLE 1 - Fullscreen Webgadget & Calling page commands
; ***************************************************
Procedure AddHorzLine()
;Add horizontal line inside the WebGadget (IFRAME)
AddWebGadgetElement(1,"<hr/>","body")
EndProcedure
Procedure ShowAlert(message.s)
;Show alert message
!alert(v_message);
EndProcedure
Procedure GadgetResizing()
;Fullscreen WebGadget (IFRAME)
Protected win=EventWindow()
If win=10 : ResizeGadget(1,#PB_Ignore,#PB_Ignore,WindowWidth(win),WindowHeight(win)) : EndIf
EndProcedure
Procedure GadgetEvents()
Protected gadget=EventGadget()
If gadget=2
AddHorzLine()
EndIf
EndProcedure
If OpenWindow(10, 0, 0, 0, 0, "FullScreen Window", #PB_Window_Background)
BindEvent(#PB_Event_SizeWindow, @GadgetResizing())
BindEvent(#PB_Event_Gadget, @GadgetEvents())
cmdAddLine=AddPageCommand(@AddHorzLine())
cmdAlert=AddPageCommand(@ShowAlert())
callAddLine.s=CallPageCommand(cmdAddLine)
callAlert1.s=CallPageCommand(cmdAlert,"('My name is \'SpiderBasic\'. "+#DQUOTE$+"Oops!"+#DQUOTE$+" Bye.')")
callAlert2.s=CallPageCommand(cmdAlert,"("+#DQUOTE$+"My name is 'PureBasic'. \"+#DQUOTE$+"Doh!\"+#DQUOTE$+" Bye."+#DQUOTE$+")")
WebGadget(1,0,0,DesktopWidth(0),DesktopHeight(0),"")
HTMLCode.s="<!DOCTYPE html>"+
"<html>"+
" <head>"+
" <meta charset='utf-8'>"+
" <meta http-equiv='X-UA-Compatible' content='IE=edge'>"+
" <meta name='viewport' content='width=device-width, initial-scale=1'>"+
" <title>Default Page In IFRAME container</title>"+
" </head>"+
" <body>"+
" <h1>Hello, World!</h1>"+
" <a onclick='"+callAddLine+";return false;' href='#' >Add line </a><br/>"+
" <a onclick='"+callAlert1+";return false;' href='#' >Show Spiderbasic message</a><br/>"+
" <a onclick="+#DQUOTE$+callAlert2+";return false;"+#DQUOTE$+" href='#' >Show Purebasic message</a>"+
" </body>"+
"</html>"
SetWebGadgetHTMLText(1,HTMLCode)
ButtonGadget(2,5,5, 200,20,"Add Line")
EndIf
CompilerEndIf