/////////////////////////////////////////////////////////////
//
// pgAdmin 4 - PostgreSQL Tools
//
// Copyright (C) 2013 - 2020, The pgAdmin Development Team
// This software is released under the PostgreSQL Licence
//
//////////////////////////////////////////////////////////////
define([
'underscore', 'jquery', 'backbone', 'sources/pgadmin', 'pgadmin.browser',
'sources/gettext', 'sources/utils',
], function(_, $, Backbone, pgAdmin, pgBrowser, gettext, commonUtils) {
/* Wizard individual Page Model */
pgBrowser.WizardPage = Backbone.Model.extend({
defaults: {
id: undefined,
/* Id */
page_title: undefined,
/* Page Title */
view: undefined,
/* A Backbone View */
html: undefined,
/* HTML tags to be rendered */
image: undefined,
/* Left hand side image */
disable_prev: false,
/* Previous Button Flag */
disable_next: false,
/* Next Button Flag */
disable_cancel: false,
/* Cancel Button Flag */
show_progress_bar: '',
/* Callback for OnLoad */
onLoad: function() {
return true;
},
/* Callback for before Next */
beforeNext: function() {
return true;
},
onNext: function() {},
onBefore: function() {},
/* Callback for before Previous */
beforePrev: function() {
return true;
},
},
});
pgBrowser.Wizard = Backbone.View.extend({
options: {
title: 'Wizard',
/* Main Wizard Title */
image: 'left_panel.png',
/* TODO:: We can use default image here */
curr_page: 0,
/* Current Page to Load */
disable_next: false,
disable_prev: false,
disable_finish: false,
disable_cancel: false,
show_header_cancel_btn: false,
/* show cancel button at wizard header */
show_header_maximize_btn: false,
/* show maximize button at wizard header */
dialog_api: null,
height: 400,
width: 650,
show_left_panel: true,
wizard_help: '',
},
tmpl: _.template(
'
' +
' ' +
'
' +
' <% if (this.options.show_left_panel) { %>' +
'
' +
'
![' + gettext('Left panel logo') + '](<%= this.options.image %>)
' +
' <% } %>' +
'
' +
' <% if ( typeof show_description != "undefined"){ %>' +
'
' +
' <%= show_description %>' +
'
' +
' <% } %>' +
'
<% if (show_progress_bar) { %>' +
'
<%= show_progress_bar %>
<% } %>' +
'
' +
'
' +
'
' +
'
' +
'
' +
' ' +
'
'),
events: {
'click button.wizard-next': 'nextPage',
'click button.wizard-back': 'prevPage',
'click button.wizard-cancel': 'onCancel',
'click button.wizard-cancel-event': 'onCancel',
'click button.wizard-maximize-event': 'onMaximize',
'click button.wizard-finish': 'finishWizard',
'click button.wizard-help': 'onDialogHelp',
'click a.close-error': 'closeErrorMsg',
},
initialize: function(options) {
this.options = _.extend({}, this.options, options.options);
this.currPage = this.collection.at(this.options.curr_page).toJSON();
},
render: function() {
var self = this,
data = this.currPage;
/* Check Status of the buttons */
this.options.disable_next = (this.options.disable_next ? true : this.evalASFunc(this.currPage.disable_next));
this.options.disable_prev = (this.options.disable_prev ? true : this.evalASFunc(this.currPage.disable_prev));
this.options.disable_cancel = (this.currPage.canCancel ? true : this.evalASFunc(this.currPage.disable_cancel));
/* HTML Content */
if (data.html) {
data.content = data.html;
}
/* Backbone View */
else if (data.view) {
data.content = data.view.render().el;
}
$(this.el).html(this.tmpl(data));
$(this.el).find('.wizard-right-panel_content').html(data.content);
/* OnLoad Callback */
this.onLoad();
setTimeout(function() {
var container = $(self.el);
commonUtils.findAndSetFocus(container);
}, 100);
return this;
},
nextPage: function() {
if (!this.beforeNext()) {
return false;
}
var page_id = this.onNext();
if (page_id) {
this.currPage = this.collection.get(page_id).toJSON();
this.options.curr_page = this.collection.indexOf(this.collection.get(page_id));
} else if (this.options.curr_page < (this.collection.length - 1)) {
this.options.curr_page = this.options.curr_page + 1;
this.currPage = this.collection.at(this.options.curr_page).toJSON();
}
this.enableDisableNext();
this.enableDisablePrev();
return this.render();
},
prevPage: function() {
if (!this.beforePrev()) {
return false;
}
var page_id = this.onPrev();
if (page_id) {
this.currPage = this.collection.get(page_id).toJSON();
this.options.curr_page = this.collection.indexOf(this.collection.get(page_id));
} else if (this.options.curr_page > 0) {
this.options.curr_page = this.options.curr_page - 1;
this.currPage = this.collection.at(this.options.curr_page).toJSON();
}
this.enableDisableNext();
this.enableDisablePrev();
return this.render();
},
finishWizard: function() {
this.onFinish();
this.remove(); // Remove view from DOM
this.off(); // Unbind all local event bindings
delete this.$el; // Delete the jQuery wrapped object variable
delete this.el; // Delete the variable reference to this node
return true;
},
enableDisableNext: function(disable) {
if (typeof(disable) != 'undefined') {
this.options.disable_next = disable;
} else if (this.options.curr_page >= (this.collection.length - 1)) {
this.options.disable_next = true;
} else {
this.options.disable_next = false;
}
},
enableDisablePrev: function(disable) {
if (typeof(disable) != 'undefined') {
this.options.disable_prev = disable;
} else if (this.options.curr_page <= 0) {
this.options.disable_prev = true;
} else {
this.options.disable_prev = false;
}
},
closeErrorMsg: function() {
$(this.el).find('.pg-prop-status-bar .alert-text').empty();
$(this.el).find('.pg-prop-status-bar').css('visibility', 'hidden');
},
beforeNext: function() {
return this.evalASFunc(this.currPage.beforeNext);
},
beforePrev: function() {
return this.evalASFunc(this.currPage.beforePrev);
},
onPrev: function() {
return this.evalASFunc(this.currPage.onPrev);
},
onNext: function() {
return this.evalASFunc(this.currPage.onNext);
},
onLoad: function() {
return this.evalASFunc(this.currPage.onLoad);
},
onFinish: function() {
return true;
},
onCancel: function() {
this.$el.remove();
return true;
},
onMaximize: function() {
var dialog_api = this.options.dialog_api,
_el = this.$el.find('.wizard-maximize-event');
// If no dialog api found then return
if (!dialog_api) return;
if (dialog_api.isMaximized()) {
// toggle the icon
_el.removeClass('ajs-maximized');
dialog_api.restore();
} else {
// toggle the icon
_el.addClass('ajs-maximized ' + _el.attr('class'));
dialog_api.maximize();
}
},
evalASFunc: function(func, ctx) {
var self = this;
ctx = ctx || self.currPage;
return (_.isFunction(func) ? func.apply(ctx, [self]) : func);
},
onDialogHelp: function() {
window.open(this.options.wizard_help, 'pgadmin_help');
},
});
return pgBrowser;
});