@@ -1236,70 +1236,114 @@ var mvInitializer = function(){
1236
1236
template : '<div><img></div>' ,
1237
1237
link : function ( scope , element , attrs ) {
1238
1238
var inlineUrl = "//" + authType + domain + "/" + scope . ngModel . config . imageUrl ;
1239
- var popupUrl = "//" + authType + domain + "/" + scope . ngModel . config . streamUrl ;
1239
+ var popupUrl = scope . ngModel . config . streamUrl ? "//" + authType + domain + "/" + scope . ngModel . config . streamUrl : null ;
1240
1240
var inlineRefreshInterval = scope . ngModel . config . imageRefresh ;
1241
1241
var imageWidth = 0 ;
1242
1242
var imageHeight = 0 ;
1243
1243
var timeoutRef = null ;
1244
1244
1245
- function setInlineUrl ( element )
1245
+ function setUrl ( element , url , interval , width , height , timeoutRef , callback )
1246
1246
{
1247
- let _url = inlineUrl ;
1247
+ let _url = url ;
1248
1248
1249
- _url += ( _url . indexOf ( "?" ) == - 1 ? "?" : "&" ) + 'rand=' + Math . random ( ) ;
1249
+ _url += ( _url . indexOf ( "?" ) == - 1 ? "?" : "&" ) + 'time=' + ( new Date ( ) . getTime ( ) ) ;
1250
+
1251
+ //console.log(element[0].firstChild.firstChild.clientWidth);
1250
1252
1251
- _url = _url . replace ( "{width}" , imageWidth ) ; //element.parent()[0].clientWidth);
1252
- _url = _url . replace ( "{height}" , imageHeight ) ; //:,element.parent()[0].clientHeight);
1253
- _url = _url . replace ( "{age}" , inlineRefreshInterval * 1000 ) ;
1253
+ _url = _url . replace ( "{width}" , width ) ; //element.parent()[0].clientWidth);
1254
+ _url = _url . replace ( "{height}" , height ) ; //:,element.parent()[0].clientHeight);
1255
+ _url = _url . replace ( "{age}" , interval * 1000 ) ;
1254
1256
1255
- var img = new Image ( ) ;
1256
- img . onload = function ( )
1257
- {
1258
- //console.log("set real");
1259
- element [ 0 ] . firstChild . firstChild . src = _url ;
1257
+ const startTime = performance . now ( ) ;
1258
+ var xhr = new XMLHttpRequest ( ) ;
1259
+ xhr . open ( "GET" , _url ) ;
1260
+ xhr . withCredentials = true ;
1261
+ xhr . responseType = 'blob' ;
1262
+ xhr . onreadystatechange = function ( ) {
1263
+ if ( this . readyState != 4 ) return ;
1264
+
1265
+ if ( this . status == 200 )
1266
+ {
1267
+ var image = element [ 0 ] . firstChild . firstChild ;
1268
+ var imageURL = window . URL . createObjectURL ( this . response ) ;
1269
+ image . onload = function ( )
1270
+ {
1271
+ sessionStorage . setItem ( 'mvWidgetImagePopup.inlineUrl_' + inlineUrl , _url ) ;
1272
+
1273
+ const duration = performance . now ( ) - startTime ;
1274
+ callback ( duration , timeoutRef ) ;
1275
+ }
1276
+ image . onerror = function ( event )
1277
+ {
1278
+ console . error ( "Image loading error" ) ;
1279
+ const duration = performance . now ( ) - startTime ;
1280
+ callback ( duration , timeoutRef ) ;
1281
+ }
1282
+ image . src = imageURL ;
1283
+ }
1284
+ else
1285
+ {
1286
+ console . error ( "Image response error: " + this . status ) ;
1287
+ const duration = performance . now ( ) - startTime ;
1288
+ callback ( duration , timeoutRef ) ;
1289
+ }
1290
+ } ;
1291
+ xhr . send ( ) ;
1292
+ }
1260
1293
1261
- sessionStorage . setItem ( 'mvWidgetImagePopup.inlineUrl_' + inlineUrl , _url ) ;
1294
+ function handleImageRefresh ( element , url , interval , width , height )
1295
+ {
1296
+ function handleTrigger ( )
1297
+ {
1298
+ setUrl ( element , url , interval , width , height , timeoutRef , function ( duration , _timeoutRef ) {
1299
+ if ( timeoutRef != _timeoutRef ) return ;
1300
+ var _interval = ( interval * 1000 ) - duration ;
1301
+ if ( _interval > 0 ) timeoutRef = $timeout ( handleTrigger , _interval , 0 , false ) ;
1302
+ else handleTrigger ( ) ;
1303
+ } ) ;
1262
1304
}
1263
- img . src = _url ;
1305
+
1306
+ handleTrigger ( ) ;
1264
1307
}
1265
1308
1266
1309
function togglePopup ( element , forceInline )
1267
1310
{
1311
+ $timeout . cancel ( timeoutRef ) ;
1312
+ timeoutRef = null ;
1313
+
1268
1314
if ( forceInline || element [ 0 ] . firstChild . classList . contains ( "popup" ) )
1269
1315
{
1270
1316
//console.log("mvWidgetImagePopup: start inline refresh " + inlineRefreshInterval );
1271
1317
1272
1318
element [ 0 ] . firstChild . classList . remove ( "popup" ) ;
1273
-
1274
- function handleTrigger ( )
1275
- {
1276
- //console.log("mvWidgetImagePopup: call inline refresh");
1277
- setInlineUrl ( element ) ;
1278
1319
1279
- timeoutRef = $timeout ( handleTrigger , inlineRefreshInterval * 1000 , 0 , false ) ;
1280
- }
1281
-
1282
- handleTrigger ( ) ;
1283
-
1284
- scope . $on ( '$destroy' , function ( event )
1285
- {
1286
- //console.log("mvWidgetImagePopup: cancel inline refresh");
1287
- $timeout . cancel ( timeoutRef ) ;
1288
- } ) ;
1320
+ handleImageRefresh ( element , inlineUrl , inlineRefreshInterval , imageWidth , imageHeight ) ;
1289
1321
}
1290
1322
else
1291
1323
{
1292
1324
//console.log("mvWidgetImagePopup: stop inline refresh");
1293
1325
1294
1326
element [ 0 ] . firstChild . classList . add ( "popup" ) ;
1295
- element [ 0 ] . firstChild . firstChild . src = popupUrl ;
1296
-
1297
- $timeout . cancel ( timeoutRef ) ;
1327
+
1328
+ if ( popupUrl )
1329
+ {
1330
+ element [ 0 ] . firstChild . firstChild . src = popupUrl ;
1331
+ }
1332
+ else
1333
+ {
1334
+ handleImageRefresh ( element , inlineUrl , 1 , 0 , 0 ) ;
1335
+ }
1298
1336
}
1299
1337
}
1300
1338
1301
1339
function initLoader ( $timeout , scope , element )
1302
1340
{
1341
+ scope . $on ( '$destroy' , function ( event )
1342
+ {
1343
+ //console.log("mvWidgetImagePopup: cancel inline refresh");
1344
+ $timeout . cancel ( timeoutRef ) ;
1345
+ } ) ;
1346
+
1303
1347
//console.log( element[0].clientWidth);
1304
1348
if ( element [ 0 ] . clientWidth > 0 )
1305
1349
{
@@ -1316,11 +1360,12 @@ var mvInitializer = function(){
1316
1360
}
1317
1361
1318
1362
var timeout = 0 ;
1363
+ var _url = sessionStorage . getItem ( 'mvWidgetImagePopup.inlineUrl_' + inlineUrl ) ;
1319
1364
// force last cached img
1320
- if ( sessionStorage . getItem ( 'mvWidgetImagePopup.inlineUrl_' + inlineUrl ) )
1365
+ if ( _url )
1321
1366
{
1322
- //console.log("use cache" );
1323
- element [ 0 ] . firstChild . firstChild . src = sessionStorage . getItem ( 'mvWidgetImagePopup.inlineUrl_' + inlineUrl ) ;
1367
+ //console.log("use cache " + _url );
1368
+ element [ 0 ] . firstChild . firstChild . src = _url ;
1324
1369
timeout = 1000 ;
1325
1370
}
1326
1371
0 commit comments