refactored taskbar's client-side code

This commit is contained in:
hyung-hwan 2016-12-19 12:58:01 +00:00
parent 9fa95d5b23
commit b8852b27ee

View File

@ -92,8 +92,42 @@ function show_taskbar ($con, $login)
?> ?>
<script type="text/javascript"> <script type="text/javascript">
var TaskbarApp = (function ()
{
// -------------------------------------------------------------------
// PRIVATE DATA
// -------------------------------------------------------------------
var signin_url = codepot_merge_path('<?php print site_url() ?>', "/main/xhr_signin");
var project_find_url_base = codepot_merge_path("<?php print site_url(); ?>", "/project/enjson_quickfind/");
// -------------------------------------------------------------------
// CONSTRUCTOR
// -------------------------------------------------------------------
function App ()
{
if (this.constructor != App)
{
return new App ();
}
this.user_name = $("#taskbar_user_name");
this.user_pass = $("#taskbar_user_pass");
this.signinout_form = $("#taskbar_signinout_form");
this.signinout_button = $("#taskbar_signinout_button");
this.signin_container = $("#taskbar_signin_container");
this.signin_error = $("#taskbar_signin_error");
this.signin_in_progress = false;
this.signin_ajax = null;
this.project_to_find = $("#taskbar_project_to_find");
this.project_find_ajax = null;
return this;
}
// -------------------------------------------------------------------
// PRIVATE FUNCTIONS
// -------------------------------------------------------------------
function ready_to_signinout() function ready_to_signinout()
{ {
<?php if (isset($login['id']) && $login['id'] != ''): ?> <?php if (isset($login['id']) && $login['id'] != ''): ?>
@ -101,14 +135,136 @@ function ready_to_signinout()
return true; return true;
<?php else: ?> <?php else: ?>
// not signed-in yet. both username and password must be filled. // not signed-in yet. both username and password must be filled.
return $("#taskbar_user_name").val() != "" && $("#taskbar_user_pass").val(); return this.user_name.val() != "" && this.user_pass.val();
<?php endif; ?> <?php endif; ?>
} }
var taskbar_signin_in_progress = 0; function signinout_on_success (data, textStatus, jqXHR)
{
this.signin_in_progress = false;
this.signin_ajax = null;
this.signin_container.dialog('enable');
$(function () { if (data == 'ok')
$('#taskbar_signin_container').dialog ({ {
this.signin_container.dialog('close');
// refresh the page to the head revision
$(location).attr ('href', '<?php print current_url(); ?>');
}
else
{
this.signin_error.text('<?php print $this->lang->line('MSG_SIGNIN_FAILURE')?>');
}
}
function signinout_on_error (jqXHR, textStatus, errorThrown)
{
this.signin_in_progress = false;
this.signin_ajax = null;
this.signin_container.dialog('enable');
var errmsg = textStatus;
if (errmsg == '' && errorThrown != null) errmsg += " - " + errorThrown;
if (errmsg == '') errmsg = 'Unknown error';
this.signin_error.text(errmsg);
}
function do_signinout ()
{
if (this.signin_in_progress) return;
if (!!window.FormData)
{
// FormData is supported
var user_name = this.user_name.val();
var user_pass = this.user_pass.val();
if (user_name == '' || user_pass == '') return;
this.signin_error.text ('');
this.signin_in_progress = true;
var form_data = new FormData();
form_data.append ('user_name', user_name);
form_data.append ('user_pass', user_pass);
this.signin_container.dialog('disable');
this.signin_ajax = $.ajax({
url: signin_url,
type: 'POST',
data: form_data,
mimeType: 'multipart/form-data',
contentType: false,
processData: false,
cache: false,
context: this,
success: signinout_on_success,
error: signinout_on_error
});
}
else
{
this.signin_error.text('NOT SUPPORTED');
}
}
function cancel_signinout ()
{
if (this.signin_in_progress)
{
this.signin_ajax.abort();
this.signin_ajax = null;
this.signin_in_progress = false;
return;
}
this.signin_container.dialog('close');
}
function trigger_signinout ()
{
var buttons = this.signin_container.dialog("option", "buttons");
buttons[Object.keys(buttons)[0]](); // trigger the first button
}
function signinout_button_on_click ()
{
<?php if (isset($login['id']) && $login['id'] != ''): ?>
if (ready_to_signinout.call(this)) this.signinout_form.submit();
<?php else: ?>
this.signin_container.dialog('open');
<?php endif; ?>
}
function get_project_candidates (request, response)
{
var term = codepot_string_to_hex(request.term);
if (this.project_find_ajax != null) { this.project_find_ajax.abort(); }
this.project_find_ajax = $.ajax({
url: project_find_url_base + term,
dataType: "json",
success: function(data, textStatus, jqXHR)
{
this.project_find_ajax = null;
response (data); // call the bacllback function for autocompletion
},
error: function(jqXHR, textStatus, errorThrown)
{
// on error, do nothing special
this.project_find_ajax = null;
}
});
}
// -------------------------------------------------------------------
// PUBLIC FUNCTIONS
// -------------------------------------------------------------------
App.prototype.initWidgets = function ()
{
var self = this;
this.signin_container.dialog ({
title: '<?php print $this->lang->line('Sign in'); ?>', title: '<?php print $this->lang->line('Sign in'); ?>',
resizable: true, resizable: true,
autoOpen: false, autoOpen: false,
@ -117,119 +273,54 @@ $(function () {
height: 'auto', height: 'auto',
buttons: { buttons: {
'<?php print $this->lang->line('OK')?>': function () { '<?php print $this->lang->line('OK')?>': function () {
if (taskbar_signin_in_progress) return; do_signinout.call (self);
if (!!window.FormData)
{
// FormData is supported
var user_name = $('#taskbar_user_name').val();
var user_pass = $('#taskbar_user_pass').val();
if (user_name == '' || user_pass == '') return;
taskbar_signin_in_progress = true;
var form_data = new FormData();
form_data.append ('user_name', user_name);
form_data.append ('user_pass', user_pass);
$('#taskbar_signin_container').dialog('disable');
$.ajax({
url: codepot_merge_path('<?php print site_url() ?>', '<?php print "/main/xhr_signin"; ?>'),
type: 'POST',
data: form_data,
mimeType: 'multipart/form-data',
contentType: false,
processData: false,
cache: false,
success: function (data, textStatus, jqXHR) {
taskbar_signin_in_progress = false;
$('#taskbar_signin_container').dialog('enable');
if (data == 'ok')
{
$('#taskbar_signin_container').dialog('close');
// refresh the page to the head revision
$(location).attr ('href', '<?php print current_url(); ?>');
}
else
{
$('#taskbar_signin_error').text(codepot_htmlspecialchars('<?php print $this->lang->line('MSG_SIGNIN_FAILURE')?>'));
}
},
error: function (jqXHR, textStatus, errorThrown) {
taskbar_signin_in_progress = false;
$('#taskbar_signin_container').dialog('enable');
var errmsg = '';
if (errmsg == '' && errorThrown != null) errmsg = errorThrown;
if (errmsg == '' && textStatus != null) errmsg = textStatus;
if (errmsg == '') errmsg = 'Unknown error';
$('#taskbar_signin_error').text(codepot_htmlspecialchars(errmsg));
}
});
}
else
{
$('#taskbar_signin_error').text('NOT SUPPORTED');
}
}, },
'<?php print $this->lang->line('Cancel')?>': function () { '<?php print $this->lang->line('Cancel')?>': function () {
if (taskbar_signin_in_progress) return; cancel_signinout.call (self);
$('#taskbar_signin_container').dialog('close');
} }
}, },
beforeClose: function() { beforeClose: function() {
// if importing is in progress, prevent dialog closing // if signin is in progress, prevent dialog closing
return !taskbar_signin_in_progress; return !self.signin_in_progress;
} }
}); });
$("#taskbar_user_name").button().bind ('keyup', function(e) { this.user_name.button().bind ('keyup', function(e) {
if (e.keyCode == 13) if (e.keyCode == 13) trigger_signinout.call (self);
{
var buttons = $("#taskbar_signin_container").dialog("option", "buttons");
buttons[Object.keys(buttons)[0]](); // trigger the first button
}
});
$("#taskbar_user_pass").button().bind ('keyup', function(e) {
if (e.keyCode == 13)
{
var buttons = $("#taskbar_signin_container").dialog("option", "buttons");
buttons[Object.keys(buttons)[0]](); // trigger the first button
}
}); });
$("#taskbar_signinout_button").button().click (function() { this.user_pass.button().bind ('keyup', function(e) {
<?php if (isset($login['id']) && $login['id'] != ''): ?> if (e.keyCode == 13) trigger_signinout.call (self);
if (ready_to_signinout()) $("#taskbar_signinout_form").submit(); });
<?php else: ?>
$('#taskbar_signin_container').dialog('open'); this.signinout_button.button().click (function() {
<?php endif; ?> signinout_button_on_click.call (self);
return false; return false;
}); });
$("#taskbar_project_to_find").button().autocomplete({ // it's not a real button. button() call only for styling purpose
this.project_to_find.button().autocomplete ({
minLength: 1, // is this too small? minLength: 1, // is this too small?
delay: 1000, delay: 1000,
source: function (request, response) { source: function (request, response) {
get_project_candidates.call (self, request, response);
var term = codepot_string_to_hex(request.term);
$.ajax({
url: codepot_merge_path("<?php print site_url(); ?>", "/project/enjson_quickfind/" + term),
dataType: "json",
success: function(data) { response(data); },
});
}, },
select: function(event, ui) { select: function(event, ui) {
$(location).attr ('href', codepot_merge_path("<?php print site_url(); ?>", "/project/home/" + ui.item.id)); $(location).attr ('href', codepot_merge_path("<?php print site_url(); ?>", "/project/home/" + ui.item.id));
//ui.item.value , ui.item.id , this.value //ui.item.value , ui.item.id , this.value
} }
}); });
}
return App;
})();
/////////////////////////////////////////////////////////////////////////
$(function () {
var taskbar_app = new TaskbarApp ();
taskbar_app.initWidgets ();
}); });
</script> </script>
@ -237,5 +328,3 @@ $(function () {
<?php <?php
show_taskbar ($this, $login); show_taskbar ($this, $login);
?> ?>