jellyfin-web/dashboard-ui/bower_components/emby-webcomponents/viewmanager/viewmanager.js

176 lines
4.9 KiB
JavaScript
Raw Normal View History

2016-07-05 22:13:26 -07:00
define(['viewcontainer', 'focusManager', 'queryString', 'layoutManager'], function (viewcontainer, focusManager, queryString, layoutManager) {
2016-10-03 22:15:39 -07:00
'use strict';
2016-03-14 13:05:15 -07:00
var currentView;
2016-03-15 11:03:25 -07:00
var dispatchPageEvents;
2016-03-14 13:05:15 -07:00
viewcontainer.setOnBeforeChange(function (newView, isRestored, options) {
var lastView = currentView;
if (lastView) {
2016-04-09 23:24:18 -07:00
var beforeHideResult = dispatchViewEvent(lastView, 'viewbeforehide', null, true);
if (!beforeHideResult) {
// todo: cancel
}
2016-03-14 13:05:15 -07:00
}
if (!newView.initComplete) {
newView.initComplete = true;
var eventDetail = getViewEventDetail(newView, options, false);
if (options.controllerFactory) {
// Use controller method
var controller = new options.controllerFactory(newView, eventDetail.detail.params);
2016-03-15 22:33:31 -07:00
}
if (!options.controllerFactory || dispatchPageEvents) {
2016-03-15 11:03:25 -07:00
dispatchViewEvent(newView, 'viewinit');
2016-03-14 13:05:15 -07:00
}
}
dispatchViewEvent(newView, 'viewbeforeshow', isRestored);
});
function onViewChange(view, options, isRestore) {
var lastView = currentView;
if (lastView) {
dispatchViewEvent(lastView, 'viewhide');
}
currentView = view;
var eventDetail = getViewEventDetail(view, options, isRestore);
if (!isRestore) {
2016-03-15 13:04:29 -07:00
if (options.autoFocus !== false) {
focusManager.autoFocus(view);
}
2016-03-14 13:05:15 -07:00
}
2016-07-05 22:13:26 -07:00
else if (!layoutManager.mobile) {
2016-04-10 14:45:06 -07:00
if (view.activeElement && document.body.contains(view.activeElement) && focusManager.isCurrentlyFocusable(view.activeElement)) {
focusManager.focus(view.activeElement);
} else {
focusManager.autoFocus(view);
}
2016-03-14 13:05:15 -07:00
}
2016-03-15 11:03:25 -07:00
view.dispatchEvent(new CustomEvent('viewshow', eventDetail));
if (dispatchPageEvents) {
view.dispatchEvent(new CustomEvent('pageshow', eventDetail));
}
2016-03-14 13:05:15 -07:00
}
2016-04-09 23:24:18 -07:00
function dispatchViewEvent(view, eventName, isRestored, isCancellable) {
2016-03-14 13:05:15 -07:00
2016-04-09 23:24:18 -07:00
var eventResult = view.dispatchEvent(new CustomEvent(eventName, {
2016-03-14 13:05:15 -07:00
detail: {
type: view.getAttribute('data-type'),
isRestored: isRestored
},
bubbles: true,
2016-04-09 23:24:18 -07:00
cancelable: isCancellable || false
2016-03-14 13:05:15 -07:00
}));
2016-03-15 11:03:25 -07:00
if (dispatchPageEvents) {
view.dispatchEvent(new CustomEvent(eventName.replace('view', 'page'), {
detail: {
type: view.getAttribute('data-type'),
isRestored: isRestored
},
bubbles: true,
cancelable: false
}));
}
2016-04-09 23:24:18 -07:00
return eventResult;
2016-03-14 13:05:15 -07:00
}
function getViewEventDetail(view, options, isRestore) {
var url = options.url;
var state = options.state;
var index = url.indexOf('?');
2016-10-03 22:15:39 -07:00
var params = index === -1 ? {} : queryString.parse(url.substring(index + 1));
2016-03-14 13:05:15 -07:00
return {
detail: {
type: view.getAttribute('data-type'),
params: params,
isRestored: isRestore,
state: options.state,
// The route options
options: options.options || {}
},
bubbles: true,
cancelable: false
};
}
function resetCachedViews() {
// Reset all cached views whenever the skin changes
viewcontainer.reset();
}
document.addEventListener('skinunload', resetCachedViews);
2016-03-14 21:00:17 -07:00
2016-05-24 09:58:12 -07:00
//events.on(connectionManager, 'localusersignedin', resetCachedViews);
//events.on(connectionManager, 'localusersignedout', resetCachedViews);
2016-03-14 13:05:15 -07:00
function ViewManager() {
2016-03-14 13:05:15 -07:00
}
ViewManager.prototype.loadView = function (options) {
2016-03-14 13:05:15 -07:00
var lastView = currentView;
2016-03-14 13:05:15 -07:00
// Record the element that has focus
if (lastView) {
lastView.activeElement = document.activeElement;
}
2016-03-14 13:05:15 -07:00
if (options.cancel) {
return;
}
2016-03-14 13:05:15 -07:00
viewcontainer.loadView(options).then(function (view) {
2016-03-14 13:05:15 -07:00
onViewChange(view, options);
});
};
2016-03-14 13:05:15 -07:00
ViewManager.prototype.tryRestoreView = function (options, onViewChanging) {
2016-03-14 13:05:15 -07:00
if (options.cancel) {
return Promise.reject({ cancelled: true });
}
2016-03-14 13:05:15 -07:00
// Record the element that has focus
if (currentView) {
currentView.activeElement = document.activeElement;
}
2016-03-14 13:05:15 -07:00
return viewcontainer.tryRestoreView(options).then(function (view) {
2016-03-14 13:05:15 -07:00
onViewChanging();
onViewChange(view, options, true);
2016-03-14 13:05:15 -07:00
});
};
2016-03-15 11:03:25 -07:00
ViewManager.prototype.currentView = function () {
return currentView;
};
2016-06-02 10:08:25 -07:00
ViewManager.prototype.dispatchPageEvents = function (value) {
dispatchPageEvents = value;
};
2016-03-14 13:05:15 -07:00
return new ViewManager();
});