2017-09-15 12:51:29 +02:00
|
|
|
var ManageAccountDialog = {};
|
2018-03-04 11:55:47 +01:00
|
|
|
|
|
|
|
ManageAccountDialog.firstLogin = true;
|
2018-01-02 18:32:44 +01:00
|
|
|
|
2017-09-15 12:51:29 +02:00
|
|
|
ManageAccountDialog.launch = function(logged_in_username, callback) {
|
2018-01-02 18:32:44 +01:00
|
|
|
$.post(
|
|
|
|
"command/core/get-all-preferences",
|
|
|
|
null,
|
|
|
|
function (preferences) {
|
|
|
|
ManageAccountDialog.display(logged_in_username, preferences.wikidata_credentials, callback);
|
|
|
|
},
|
|
|
|
"json"
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
ManageAccountDialog.display = function(logged_in_username, saved_credentials, callback) {
|
2017-09-15 12:51:29 +02:00
|
|
|
var self = this;
|
|
|
|
var frame = $(DOM.loadHTML("wikidata", "scripts/dialogs/manage-account-dialog.html"));
|
|
|
|
var elmts = this._elmts = DOM.bind(frame);
|
|
|
|
|
2018-11-21 18:30:00 +01:00
|
|
|
this._elmts.dialogHeader.text($.i18n('wikidata-account/dialog-header'));
|
|
|
|
this._elmts.explainLogIn.html($.i18n('wikidata-account/explain-log-in'));
|
|
|
|
this._elmts.usernameLabel.text($.i18n('wikidata-account/username-label'));
|
|
|
|
this._elmts.usernameInput.attr("placeholder", $.i18n('wikidata-account/username-placeholder'));
|
|
|
|
this._elmts.passwordLabel.text($.i18n('wikidata-account/password-label'));
|
|
|
|
this._elmts.passwordInput.attr("placeholder", $.i18n('wikidata-account/password-placeholder'));
|
|
|
|
this._elmts.rememberCredentialsLabel.text($.i18n('wikidata-account/remember-credentials-label'));
|
|
|
|
this._elmts.dialogHeader.text($.i18n('wikidata-account/dialog-header'));
|
|
|
|
this._elmts.cancelButton1.text($.i18n('wikidata-account/close'));
|
|
|
|
this._elmts.cancelButton2.text($.i18n('wikidata-account/close'));
|
|
|
|
this._elmts.loggedInAs.text($.i18n('wikidata-account/logged-in-as'));
|
|
|
|
this._elmts.logoutButton.text($.i18n('wikidata-account/log-out'));
|
2020-01-06 11:40:02 +01:00
|
|
|
this._elmts.loginButton.val($.i18n('wikidata-account/log-in'));
|
|
|
|
|
|
|
|
if (logged_in_username != null) {
|
|
|
|
elmts.loginArea.remove();
|
|
|
|
} else {
|
|
|
|
elmts.logoutArea.remove();
|
|
|
|
}
|
2018-02-23 15:57:16 +01:00
|
|
|
|
2017-09-15 12:51:29 +02:00
|
|
|
this._level = DialogSystem.showDialog(frame);
|
2020-04-26 15:39:10 +02:00
|
|
|
this._elmts.usernameInput.focus();
|
2017-09-15 12:51:29 +02:00
|
|
|
|
|
|
|
var dismiss = function() {
|
|
|
|
DialogSystem.dismissUntil(self._level - 1);
|
|
|
|
};
|
|
|
|
|
2018-03-04 11:55:47 +01:00
|
|
|
elmts.loggedInUsername
|
|
|
|
.text(logged_in_username)
|
|
|
|
.attr('href', 'https://www.wikidata.org/wiki/User:'+logged_in_username);
|
2019-12-21 00:51:43 +01:00
|
|
|
|
2020-01-06 11:40:02 +01:00
|
|
|
elmts.cancelButton1.click(function(e) {
|
|
|
|
dismiss();
|
|
|
|
callback(null);
|
|
|
|
});
|
|
|
|
elmts.cancelButton2.click(function(e) {
|
2017-09-15 12:51:29 +02:00
|
|
|
dismiss();
|
|
|
|
callback(null);
|
|
|
|
});
|
|
|
|
|
2019-12-22 23:40:57 +01:00
|
|
|
elmts.loginForm.submit(function(e) {
|
2019-12-22 15:26:39 +01:00
|
|
|
frame.hide();
|
|
|
|
Refine.postCSRF(
|
|
|
|
"command/wikidata/login",
|
|
|
|
elmts.loginForm.serialize(),
|
|
|
|
function(data) {
|
|
|
|
if (data.logged_in) {
|
|
|
|
dismiss();
|
|
|
|
callback(data.username);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
frame.show();
|
|
|
|
elmts.invalidCredentials.text("Invalid credentials.");
|
|
|
|
}
|
|
|
|
});
|
2019-12-22 23:40:57 +01:00
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
);
|
2019-12-21 00:51:43 +01:00
|
|
|
|
2017-09-15 12:51:29 +02:00
|
|
|
elmts.logoutButton.click(function() {
|
2019-10-17 11:42:05 +02:00
|
|
|
Refine.postCSRF(
|
2017-09-15 12:51:29 +02:00
|
|
|
"command/wikidata/login",
|
|
|
|
"logout=true",
|
|
|
|
function(data) {
|
|
|
|
if (!data.logged_in) {
|
|
|
|
dismiss();
|
|
|
|
callback(null);
|
|
|
|
}
|
2019-12-21 00:51:43 +01:00
|
|
|
});
|
2017-09-15 12:51:29 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
ManageAccountDialog.isLoggedIn = function(callback) {
|
2018-03-04 11:55:47 +01:00
|
|
|
var discardWaiter = function() { };
|
|
|
|
if(ManageAccountDialog.firstLogin) {
|
2018-11-21 18:30:00 +01:00
|
|
|
discardWaiter = DialogSystem.showBusy($.i18n('wikidata-account/connecting-to-wikidata'));
|
2018-03-04 11:55:47 +01:00
|
|
|
}
|
2017-09-15 12:51:29 +02:00
|
|
|
$.get(
|
|
|
|
"command/wikidata/login",
|
|
|
|
function(data) {
|
2018-03-04 11:55:47 +01:00
|
|
|
discardWaiter();
|
|
|
|
ManageAccountDialog.firstLogin = false;
|
2017-09-15 12:51:29 +02:00
|
|
|
callback(data.username);
|
|
|
|
});
|
2019-12-21 00:51:43 +01:00
|
|
|
};
|
2017-09-15 12:51:29 +02:00
|
|
|
|
|
|
|
ManageAccountDialog.ensureLoggedIn = function(callback) {
|
|
|
|
ManageAccountDialog.isLoggedIn(function(logged_in_username) {
|
|
|
|
if (logged_in_username == null) {
|
|
|
|
ManageAccountDialog.launch(null, callback);
|
|
|
|
} else {
|
|
|
|
callback(logged_in_username);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
ManageAccountDialog.checkAndLaunch = function () {
|
|
|
|
ManageAccountDialog.isLoggedIn(function(logged_in_username) {
|
|
|
|
ManageAccountDialog.launch(logged_in_username, function(success) { });
|
|
|
|
});
|
|
|
|
};
|