How to omit loading a widget (or some other work) based on user being inside or outside LAN?

344
2
02-21-2019 03:25 PM
AndrewHayden1
Occasional Contributor

I'd like to have a set of widgets load for outside users, but if user is inside I'd like to include an additional widget (or conversely, load them all on the inside but skip a particular widget if the user is on the outside).  I was thinking I could maybe use sessionStorage and set a variable indicating if the user is in or out and then check that variable when the widgets load.  Might there be a pitfall in using the logic below or with the idea in general?  If not, where would be the best place to call detectIntranet()?  Is there somewhere more appropriate than index.html (which I tested and it seemed to work)?  Looking a little further ahead, I'm not sure where I'd be filtering out the widget I don't want...should I be looking at filtering it out somewhere in the sidebarcontroller or should I be looking in the widgetmanager?  I would appreciate any thoughts or suggestions that might move me along a little further.

<script type="text/javascript">

var detectionCounter = 0;
var detectionTimeOut = 5;
var detectionImage = 'http://internalwebserver/someimage.jpg?' + (new Date()).getTime();
var detectionElement = document.createElement('img');
detectionElement.src = detectionImage;

function detectIntranet() {

    detectionCounter = detectionCounter + 1;
       
    if (detectionElement.complete) {
        if (detectionElement.width > 0 && detectionElement.height > 0) {
            sessionStorage.setItem('InOrOut', 'In');
        } else {
            sessionStorage.setItem('InOrOut', 'Out');
        }
    } else {
        if (detectionCounter < detectionTimeOut) {
            setTimeout("detectIntranet()", 1000);
        } else {
            sessionStorage.setItem('InOrOut', 'Out');
        }
    }
}

window.onload = function () {
    detectIntranet();
}

</script>‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
2 Replies
RobertScheitlin__GISP
MVP Emeritus

Andrew,

1.

    a. Might there be a pitfall in using the logic below or with the idea in general? Not that I am aware of.

    b. If not, where would be the best place to call detectIntranet()? Index.html.

    c. Is there somewhere more appropriate than index.html (which I tested and it seemed to work)? Nope.

2.    Looking a little further ahead, I'm not sure where I'd be filtering out the widget I don't want...should I be looking at filtering it out somewhere in the sidebarcontroller or should I be looking in the widgetmanager? I would look at the WidgetManager loadWidget function

AndrewHayden1
Occasional Contributor

Thanks Robert.  I ended up accomplishing my task using code similar to below in SidebarController createTabs.  Stepping through WidgetManager loadWidget, I was able to step through the widgetOnScreen widgets but not the widgetPool widgets.  I could simplify and make configurable below...but it shows the general idea.  Thanks again.  

if (sessionStorage.getItem("InOrOut") === 'In') {
			//Do this if inside
			var index = allIconConfigs.findIndex(x => x.name == 'SomeWidgetNameToRemove');
			if (index > -1) {
				allIconConfigs.splice(index, 1);
			}
		} else {
			//Do this if outside
			var index = allIconConfigs.findIndex(x => x.name == 'SomeOtherWidgetNameToRemove');
			if (index > -1) {
				allIconConfigs.splice(index, 1);
			}
		}‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍