File size: 4,811 Bytes
4b876a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright © 2010 - May 2015 Rise Vision Incorporated.
// Use of this software is governed by the GPLv3 license
// (reproduced in the LICENSE file).

var $rv = $rv || {}; //Rise Vision namespace
console.log(launchData);

$rv.debug = launchData.debugMode;

onload = function (e) {
    console.log('body.onload is called');

	var w = chrome.app.window.current()
	        w.setBounds({
            left: launchData.windowOptions.left,
            top: launchData.windowOptions.top,
            width: launchData.windowOptions.width,
            height: launchData.windowOptions.height
    	} );

    w.onBoundsChanged.addListener(updateDisplayProperties);
    $rv.browser = document.querySelector('#viewer');
    
    $rv.browser.addEventListener('exit', function(e) {
    	console.log("[WebView.exit] reason: " + e.reason);
		if (e.reason === 'crash' || e.reason === 'crashed') {
			//$rv.browser.src = 'data:text/plain,Browser crashed. It will restart shortly!';
			//TODO: restart WebView ASAP
		}
	});
    
    $rv.browser.addEventListener('permissionrequest', function(e) {
	    console.log('Permission '+e.permission+' requested by webview');
    	  if ( e.permission === 'geolocation' ) {
    	    e.request.allow();
    	  }
    	  else if ( e.permission === 'loadplugin' ) {
    	    e.request.allow();
    	  } else {
    	    e.request.deny();
    	  }
    });
    
    if ($rv.debug) {
    	onresize = resizeBrowser;

//      document.querySelector('#btClose').onclick = function() {
//    	window.close();
//    };

    	var btRefresh = document.querySelector('#btRefresh')
        if (btRefresh) {
        	btRefresh.style.display = "inline";
        	btRefresh.onclick = function() {
                if ($rv.browser) {
                	$rv.browser.reload();
                }
        	};
        }

    	var btCacheStatus = document.querySelector('#btCacheStatus')
        if (btCacheStatus) {
        	btCacheStatus.style.display = "inline";
        	btCacheStatus.onclick = function() {
            	$rv.cache.printStats();
            	
        	};
        }

    }
    try {
        $rv.config = new rvConfig();
        $rv.config.init(onConfigLoad);
        resizeBrowser();
    } catch (e) {
        console.log("onload error" + e.message);
    }        
};

var onConfigLoad = function() {

    try {
        $rv.extLogger = new rvExtLogger();
        $rv.extLogger.log("launch from " + $rv.config.launchSource);

        $rv.cache = new rvCache();
        $rv.cache.init($rv.config);
    
        $rv.player = new rvPlayer();
        $rv.player.init($rv.config);
        $rv.extLogger.log("player initialized");
        
        resizeBrowser();
    } catch (e) {
        console.log("onConfigLoad error" + e.message);
    }
};

function resizeBrowser() {
    $rv.heightOffset = $rv.debug ? 50 : 0;
    if ($rv.browser) {
        var windowWidth = launchData.windowOptions.width;
        var windowHeight = launchData.windowOptions.height - $rv.heightOffset;
        $rv.browser.style.width = windowWidth + "px";
        $rv.browser.style.height = windowHeight + "px";
        //$rv.config.saveScreenSize(windowWidth, windowHeight);
        console.log('resize to screenWidth = ' + windowWidth + ' | screenHeight = ' + windowHeight);

    }
} 

function updateDisplayProperties() {
    console.log('updateDisplayProperties event called');
	chrome.system.display.getInfo(function(displays) {
		console.log(displays[0].bounds.left);	
		var f_mon=0;var f_left=0;var f_top=0;var max_width=0;var max_height=0;var total_width=0;var total_height=0;
		for (var i = 0; i < displays.length; i++) {
		        var display = displays[i];
		        if(launchData.os == "win") {
		        	if(display.bounds.left > f_left) {f_left=display.bounds.left;f_mon=i;}
		        	if(display.bounds.top > f_top) {f_top=display.bounds.top;f_mon=i;}
		        	if(display.bounds.width > max_width) {max_width=display.bounds.width;}
		        	if(display.bounds.height > max_height) {max_height=display.bounds.height;}
			} else {
				if(display.bounds.left === 0 && display.bounds.top === 0) {
					total_width=display.bounds.width;
					total_height=display.bounds.height;	
				}
			}
		}
		
		if(launchData.os == "win") {
			var display = displays[f_mon];
			launchData.windowOptions.width = display.bounds.left+max_width;
			launchData.windowOptions.height = display.bounds.top+max_height;
		} else {
			launchData.windowOptions.width = total_width;
			launchData.windowOptions.height = total_height;
		}
		
		if(launchData.os != "win" || displays.length === 1) {
			launchData.windowOptions.state = launchData.debugMode ? 'normal' : 'fullscreen';
		}
		resizeBrowser();
	});
}


$rv.close = function() {
	window.close();
};

$rv.onSocketCreated = function(socketId) {
    launchData.sockets.push(socketId);
};