jellyfin-web/dashboard-ui/cordova/registrationservices.js

347 lines
10 KiB
JavaScript
Raw Normal View History

2015-05-22 12:16:14 -07:00
(function () {
2015-05-16 12:09:02 -07:00
2015-06-07 14:21:30 -07:00
function validateServerManagement(deferred) {
deferred.resolve();
}
2015-10-01 23:14:04 -07:00
function getRegistrationInfo(feature) {
2015-05-26 10:48:05 -07:00
return ConnectionManager.getRegistrationInfo(feature, ApiClient);
}
2015-08-29 17:40:52 -07:00
var validatedFeatures = [];
2015-10-01 23:14:04 -07:00
function validateFeature(feature, deferred) {
2015-06-03 21:50:10 -07:00
2015-10-01 23:14:04 -07:00
var id = IapManager.getStoreFeatureId(feature);
if (validatedFeatures.indexOf(feature) != -1) {
2015-08-29 17:40:52 -07:00
deferred.resolve();
return;
}
2015-10-01 23:14:04 -07:00
var info = IapManager.getProductInfo(feature) || {};
2015-05-26 08:31:50 -07:00
2015-06-03 21:50:10 -07:00
if (info.owned) {
2015-09-22 21:00:30 -07:00
notifyServer(id);
2015-10-01 23:14:04 -07:00
validatedFeatures.push(feature);
2015-05-26 08:31:50 -07:00
deferred.resolve();
return;
}
2015-10-02 10:55:26 -07:00
var unlockableProductInfo = IapManager.isPurchaseAvailable(feature) ? {
2015-10-01 23:14:04 -07:00
enableAppUnlock: IapManager.isPurchaseAvailable(feature),
2015-06-03 21:50:10 -07:00
id: id,
2015-10-01 23:14:04 -07:00
price: info.price,
feature: feature
2015-10-02 10:55:26 -07:00
} : null;
2015-05-26 10:48:05 -07:00
2015-10-01 23:14:04 -07:00
var prefix = $.browser.android ? 'android' : 'ios';
2015-05-27 22:51:48 -07:00
2015-05-26 08:31:50 -07:00
// Get supporter status
2015-10-01 23:14:04 -07:00
getRegistrationInfo(prefix + 'appunlock').done(function (registrationInfo) {
2015-05-26 08:31:50 -07:00
if (registrationInfo.IsRegistered) {
2015-10-01 23:14:04 -07:00
validatedFeatures.push(feature);
2015-05-26 08:31:50 -07:00
deferred.resolve();
return;
}
2015-10-01 23:14:04 -07:00
IapManager.getSubscriptionOptions().done(function (subscriptionOptions) {
2015-10-02 10:55:26 -07:00
var dialogOptions = {
title: Globalize.translate('HeaderUnlockApp')
};
showInAppPurchaseInfo(subscriptionOptions, unlockableProductInfo, registrationInfo, dialogOptions, deferred);
2015-10-01 23:14:04 -07:00
});
2015-05-26 08:31:50 -07:00
}).fail(function () {
deferred.reject();
});
}
2015-09-22 21:00:30 -07:00
function notifyServer(id) {
if (!$.browser.android) {
return;
}
HttpClient.send({
type: "POST",
2015-09-23 19:31:40 -07:00
url: "https://mb3admin.com/admin/service/appstore/addDeviceFeature",
2015-09-22 21:00:30 -07:00
data: {
deviceId: ConnectionManager.deviceId(),
feature: 'com.mb.android.unlock'
},
contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
headers: {
"X-EMBY-TOKEN": "EMBY_DEVICE"
}
}).done(function (result) {
Logger.log('addDeviceFeature succeeded');
}).fail(function () {
Logger.log('addDeviceFeature failed');
});
}
2015-10-02 10:55:26 -07:00
function cancelInAppPurchase() {
var elem = document.querySelector('.inAppPurchaseOverlay');
if (elem) {
PaperDialogHelper.close(elem);
}
}
var isCancelled = true;
var currentDisplayingProductInfos = [];
var currentDisplayingDeferred = null;
function clearCurrentDisplayingInfo() {
currentDisplayingProductInfos = [];
currentDisplayingDeferred = null;
}
function showInAppPurchaseElement(subscriptionOptions, unlockableProductInfo, dialogOptions, deferred) {
cancelInAppPurchase();
// clone
currentDisplayingProductInfos = subscriptionOptions.slice(0);
if (unlockableProductInfo) {
currentDisplayingProductInfos.push(unlockableProductInfo);
}
2015-05-26 10:48:05 -07:00
2015-10-01 23:14:04 -07:00
var dlg = PaperDialogHelper.createDialog();
2015-05-26 10:48:05 -07:00
var html = '';
2015-10-01 23:14:04 -07:00
html += '<h2 class="dialogHeader">';
html += '<paper-fab icon="arrow-back" class="mini btnCloseDialog"></paper-fab>';
2015-10-02 10:55:26 -07:00
html += '<div style="display:inline-block;margin-left:.6em;vertical-align:middle;">' + dialogOptions.title + '</div>';
2015-10-01 23:14:04 -07:00
html += '</h2>';
2015-05-26 10:48:05 -07:00
2015-10-01 23:14:04 -07:00
html += '<div class="editorContent">';
2015-05-26 10:48:05 -07:00
2015-10-01 23:14:04 -07:00
html += '<form style="max-width: 800px;margin:auto;">';
2015-05-26 10:48:05 -07:00
html += '<p style="margin:2em 0;">';
2015-10-02 10:55:26 -07:00
if (unlockableProductInfo) {
2015-05-26 10:48:05 -07:00
html += Globalize.translate('MessageUnlockAppWithPurchaseOrSupporter');
}
2015-10-01 23:14:04 -07:00
else {
2015-05-26 10:48:05 -07:00
html += Globalize.translate('MessageUnlockAppWithSupporter');
}
html += '</p>';
2015-10-01 23:14:04 -07:00
html += '<p style="margin:2em 0;">';
html += Globalize.translate('MessageToValidateSupporter');
html += '</p>';
2015-05-26 10:48:05 -07:00
2015-10-02 10:55:26 -07:00
if (unlockableProductInfo) {
2015-06-03 21:50:10 -07:00
var unlockText = Globalize.translate('ButtonUnlockWithPurchase');
2015-10-02 10:55:26 -07:00
if (unlockableProductInfo.price) {
unlockText = Globalize.translate('ButtonUnlockPrice', unlockableProductInfo.price);
2015-06-03 21:50:10 -07:00
}
2015-06-23 15:13:06 -07:00
html += '<p>';
2015-10-02 10:55:26 -07:00
html += '<paper-button raised class="secondary block btnPurchase" data-feature="' + unlockableProductInfo.feature + '"><iron-icon icon="check"></iron-icon><span>' + unlockText + '</span></paper-button>';
2015-06-23 15:13:06 -07:00
html += '</p>';
2015-10-01 23:14:04 -07:00
}
2015-09-08 13:40:48 -07:00
2015-10-01 23:14:04 -07:00
for (var i = 0, length = subscriptionOptions.length; i < length; i++) {
html += '<p>';
html += '<paper-button raised class="submit block btnPurchase" data-email="true" data-feature="' + subscriptionOptions[i].feature + '"><iron-icon icon="check"></iron-icon><span>';
html += subscriptionOptions[i].buttonText;
html += '</span></paper-button>';
html += '</p>';
2015-05-26 10:48:05 -07:00
}
2015-10-01 23:14:04 -07:00
if (IapManager.restorePurchase) {
html += '<p>';
html += '<paper-button raised class="secondary block btnRestorePurchase" style="background-color: #673AB7;"><iron-icon icon="check"></iron-icon><span>' + Globalize.translate('ButtonRestorePreviousPurchase') + '</span></paper-button>';
html += '</p>';
}
2015-05-26 08:31:50 -07:00
2015-10-01 23:14:04 -07:00
html += '</form>';
2015-06-03 21:50:10 -07:00
html += '</div>';
2015-05-26 10:48:05 -07:00
2015-10-01 23:14:04 -07:00
dlg.innerHTML = html;
document.body.appendChild(dlg);
2015-10-02 10:55:26 -07:00
initInAppPurchaseElementEvents(dlg, deferred);
2015-05-26 10:48:05 -07:00
2015-10-01 23:14:04 -07:00
PaperDialogHelper.openWithHash(dlg, 'iap');
2015-05-26 10:48:05 -07:00
2015-10-01 23:14:04 -07:00
$('.btnCloseDialog', dlg).on('click', function () {
PaperDialogHelper.close(dlg);
});
2015-10-07 14:42:29 -07:00
$(dlg).on('iron-overlay-closed', function () {
if (window.TabBar) {
TabBar.show();
}
});
2015-10-01 23:14:04 -07:00
dlg.classList.add('inAppPurchaseOverlay');
2015-06-01 07:49:23 -07:00
}
2015-10-02 10:55:26 -07:00
function initInAppPurchaseElementEvents(elem, deferred) {
2015-05-26 10:48:05 -07:00
2015-10-02 10:55:26 -07:00
isCancelled = true;
2015-05-26 10:48:05 -07:00
2015-10-02 10:55:26 -07:00
$('.btnPurchase', elem).on('click', function () {
2015-06-01 07:49:23 -07:00
2015-10-02 10:55:26 -07:00
isCancelled = false;
2015-05-26 10:48:05 -07:00
2015-10-02 10:55:26 -07:00
if (this.getAttribute('data-email') == 'true') {
promptForEmail(this.getAttribute('data-feature'));
} else {
IapManager.beginPurchase(this.getAttribute('data-feature'));
}
});
2015-05-26 10:48:05 -07:00
2015-10-02 10:55:26 -07:00
$('.btnRestorePurchase', elem).on('click', function () {
2015-09-08 13:40:48 -07:00
2015-10-02 10:55:26 -07:00
isCancelled = false;
IapManager.restorePurchase(info.feature);
});
2015-09-08 13:40:48 -07:00
2015-10-02 10:55:26 -07:00
$(elem).on('iron-overlay-closed', function () {
2015-06-01 07:49:23 -07:00
2015-10-02 10:55:26 -07:00
if (isCancelled) {
clearCurrentDisplayingInfo();
2015-05-26 10:48:05 -07:00
2015-10-02 10:55:26 -07:00
deferred.reject();
}
$(this).remove();
});
}
2015-05-26 10:48:05 -07:00
2015-10-02 10:55:26 -07:00
function showInAppPurchaseInfo(subscriptionOptions, unlockableProductInfo, serverRegistrationInfo, dialogOptions, deferred) {
2015-10-01 23:14:04 -07:00
2015-10-02 10:55:26 -07:00
require(['components/paperdialoghelper'], function () {
2015-06-01 07:49:23 -07:00
2015-10-07 14:42:29 -07:00
if (window.TabBar) {
TabBar.hide();
}
2015-10-02 10:55:26 -07:00
showInAppPurchaseElement(subscriptionOptions, unlockableProductInfo, dialogOptions, deferred);
2015-10-01 23:14:04 -07:00
2015-10-02 10:55:26 -07:00
currentDisplayingDeferred = deferred;
2015-10-01 23:14:04 -07:00
});
}
function promptForEmail(feature) {
require(['prompt'], function (prompt) {
prompt({
text: Globalize.translate('TextPleaseEnterYourEmailAddressForSubscription'),
title: Globalize.translate('HeaderEmailAddress'),
2015-10-02 10:55:26 -07:00
callback: function (email) {
2015-10-01 23:14:04 -07:00
if (email) {
2015-10-02 10:55:26 -07:00
IapManager.beginPurchase(feature, email);
2015-10-01 23:14:04 -07:00
}
2015-05-26 10:48:05 -07:00
}
});
});
2015-05-17 18:27:48 -07:00
}
2015-05-22 12:16:14 -07:00
2015-06-01 07:49:23 -07:00
function onProductUpdated(e, product) {
var deferred = currentDisplayingDeferred;
2015-10-01 23:14:04 -07:00
if (deferred && product.owned) {
if (currentDisplayingProductInfos.filter(function (p) {
return product.id == p.id;
2015-06-01 07:49:23 -07:00
2015-10-01 23:14:04 -07:00
}).length) {
2015-10-02 10:55:26 -07:00
isCancelled = false;
2015-06-01 07:49:23 -07:00
clearCurrentDisplayingInfo();
cancelInAppPurchase();
deferred.resolve();
}
}
}
2015-07-29 19:08:35 -07:00
function validateSync(deferred) {
Dashboard.getPluginSecurityInfo().done(function (pluginSecurityInfo) {
2015-07-29 20:02:37 -07:00
if (pluginSecurityInfo.IsMBSupporter) {
2015-07-29 19:08:35 -07:00
deferred.resolve();
return;
}
2015-10-02 10:55:26 -07:00
// Get supporter status
getRegistrationInfo('Sync').done(function (registrationInfo) {
2015-07-29 19:08:35 -07:00
2015-07-29 20:02:37 -07:00
if (registrationInfo.IsRegistered) {
2015-10-02 10:55:26 -07:00
validatedFeatures.push(feature);
2015-07-29 19:08:35 -07:00
deferred.resolve();
return;
}
2015-10-02 10:55:26 -07:00
IapManager.getSubscriptionOptions().done(function (subscriptionOptions) {
2015-07-29 19:08:35 -07:00
2015-10-02 10:55:26 -07:00
var dialogOptions = {
title: Globalize.translate('HeaderUnlockSync')
};
2015-07-29 19:08:35 -07:00
2015-10-02 10:55:26 -07:00
showInAppPurchaseInfo(subscriptionOptions, null, registrationInfo, dialogOptions, deferred);
2015-07-29 19:08:35 -07:00
});
2015-10-02 10:55:26 -07:00
}).fail(function () {
deferred.reject();
});
2015-07-29 19:08:35 -07:00
});
}
2015-05-22 12:16:14 -07:00
window.RegistrationServices = {
renderPluginInfo: function (page, pkg, pluginSecurityInfo) {
},
validateFeature: function (name) {
var deferred = DeferredBuilder.Deferred();
if (name == 'playback') {
2015-10-01 23:14:04 -07:00
validateFeature(name, deferred);
2015-05-22 12:16:14 -07:00
} else if (name == 'livetv') {
2015-10-01 23:14:04 -07:00
validateFeature(name, deferred);
2015-07-29 19:08:35 -07:00
} else if (name == 'sync') {
validateSync(deferred);
2015-05-22 12:16:14 -07:00
} else {
deferred.resolve();
}
return deferred.promise();
}
};
2015-06-01 07:49:23 -07:00
function onIapManagerLoaded() {
Events.on(IapManager, 'productupdated', onProductUpdated);
}
2015-10-01 23:14:04 -07:00
if ($.browser.android) {
2015-06-19 21:48:45 -07:00
requirejs(['cordova/android/iap'], onIapManagerLoaded);
2015-05-31 14:07:44 -07:00
} else {
2015-06-19 21:48:45 -07:00
requirejs(['cordova/iap'], onIapManagerLoaded);
2015-05-31 14:07:44 -07:00
}
2015-05-26 08:31:50 -07:00
2015-05-22 12:16:14 -07:00
})();