(function ($, adcell) {
DialogNavigator = function (options) {
var HTMLtemplate = '
';
var navId = 'dlgnav-nav';
var contentId = 'dlgnav-content';
var currentPageIndex = 0;
var pages = [];
var currentPage = {};
var dialog = {};
var defaultOptions = {};
var onPageShow;
var onPageLeave;
var init = function (options) {
defaultOptions = $.extend(defaultOptions, options);
currentPageIndex = options.currentPageIndex ? options.currentPageIndex : 0;
pages = options.pages ? options.pages : [];
if (pages.length > currentPageIndex) {
currentPage = pages[currentPageIndex];
} else {
currentPage = pages[0];
}
if (options.pageShow && typeof options.pageShow == 'function') {
onPageShow = options.pageShow
}
if (options.pageLeave && typeof options.pageLeave == 'function') {
onPageLeave = options.pageLeave
}
}
var nextPage = function () {
if (pageLeave()) {
pages[currentPageIndex].content = '';
currentPageIndex = (currentPageIndex + 1) % pages.length;
currentPage = pages[currentPageIndex];
loadPageContent(currentPage);
}
}
var previousPage = function () {
if (pageLeave()) {
pages[currentPageIndex].content = '';
currentPageIndex = (currentPageIndex - 1) < 0 ? (pages.length - 1) : currentPageIndex - 1;
currentPage = pages[currentPageIndex];
loadPageContent(currentPage);
}
}
var savePage = function () {
if (pageLeave()) {
$('.ui-icon-closethick').trigger('click');
}
}
var nthPage = function (n) {
if (n < pages.length && n != currentPageIndex) {
if (pageLeave()) {
currentPageIndex = n;
if (pages[n].hasOwnProperty('directLink')) {
activeProgramActions(currentPageIndex);
} else {
currentPage = pages[currentPageIndex];
loadPageContent(currentPage);
}
}
}
}
var activeProgramActions = function (n) {
if (pages[n].hasOwnProperty('directLink')) {
location.href = pages[n].directLink;
}
}
var close = function () {
if (!$.isEmptyObject(dialog)) {
$('.ui-dialog-content').dialog("close").dialog("destroy");
}
}
var showDialog = function () {
if (!$.isEmptyObject(dialog)) {
return;
}
var buttons = {};
if (defaultOptions.buttons) {
buttons[defaultOptions.buttons.buttonTitle] = {
icon: (defaultOptions.buttons.icon ? defaultOptions.buttons.icon : ''),
callback: (defaultOptions.buttons.callback ? defaultOptions.buttons.callback : '')
}
}
if (defaultOptions.programStatus == 'active') {
buttons['speichern'] = {
icon: 'check',
callback: this.savePage
}
} else {
buttons['zurück'] = {
icon: 'arrowthick-1-w',
callback: this.previousPage
}
buttons['weiter'] = {
icon: ('arrowthick-1-e'),
callback: this.nextPage
}
}
dialog = adcell.dialog.template({
dialogClass: 'btn-no-highlight',
data: {
title: currentPage.dialogTitle ? currentPage.dialogTitle : '',
template: HTMLtemplate,
},
buttons: buttons,
height: 'auto',
maxHeight: window.innerHeight-100,
width: 900,
minWidth: 900,
position: {
my: 'top',
at: 'top+50',
of: window
}
});
// next & prev Button
addNavigation();
loadPageContent();
}
var addNavigation = function () {
var navbar = $('#' + navId);
// add prev button
if (defaultOptions.programStatus != 'active') {
navbar.append('
');
}
navbar.append('
')
var navgroup = $('.dlgnav-navgroup', navbar);
// add page nav
for (var i = 0; i < pages.length; i++) {
if (pages[i].CSSclass || pages[i].navItem) {
navgroup.append('
' + (pages[i].navItem ? pages[i].navItem : '') + '');
}
}
// add next button
if (defaultOptions.programStatus != 'active') {
navbar.append('
');
}
bind();
}
var bind = function () {
var navbar = $('#' + navId);
// bind next/prev click
if (defaultOptions.programStatus != 'active') {
$('.dlgnav-prev', navbar).click(previousPage);
$('.dlgnav-next', navbar).click(nextPage);
}
// bind nav click
var scope = this;
for (var i = 0; i < pages.length; i++) {
if (pages[i].CSSclass || pages[i].navItem) {
$('.dlgnav-item' + i, navbar).click($.proxy(nthPage, scope, i));
}
}
}
var showPageContent = function() {
var currentPage = pages[currentPageIndex];
var content = $('#' + contentId);
content.children().remove();
content.append(currentPage.content);
if (currentPage.dialogTitle) {
dialog.dialog('option', 'title', currentPage.dialogTitle);
}
pageShow();
adcell.ui.initPageQtips();
}
/**
* Lade Content per AJAX, wenn URL angegeben
* @return {[type]} [description]
*/
var loadPageContent = function () {
if (currentPage.url.length > 0) {
var url = currentPage.url;
adcell.fn.load(
url,
(currentPage.data ? currentPage.data : {}),
{
success: function(result){
if(typeof(result.data) == 'object'){
currentPage.content = result.data.template;
currentPage.dialogTitle = result.data.dialogTitle;
currentPage.buttonTitle = result.data.buttonTitle;
currentPage.icon = result.data.icon;
if (result.data.pageLeave) {
currentPage.pageLeave = result.data.pageLeave;
}
if (result.data.pageShow) {
currentPage.pageShow = result.data.pageShow;
}
pages[currentPageIndex] = currentPage;
showPageContent();
} else if(result.hasOwnProperty('errors')){
for(var key in result.errors){
if(result.errors.hasOwnProperty(key)){
adcell.message.error('Fehler', result.errors[key]);
}
}
close();
}
else{
alert('Fehler: '+result);
}
}
}
);
} else {
showPageContent();
}
}
var addPage = function (page) {
options.pages.push(page);
}
var showSpinner = function () {
$('#' + contentId).append(
'
'
);
}
var stopSpinner = function () {
$('#' + contentId).remove('div.spinner-bg');
}
var pageLeave = function () {
var result = true;
$('.dlgnav-item' + currentPageIndex).removeClass('active');
if (pages[currentPageIndex].pageLeave && typeof eval(pages[currentPageIndex].pageLeave) == 'function') {
result = pages[currentPageIndex].pageLeave = eval(pages[currentPageIndex].pageLeave);
if (result == false) {
return false;
}
}
if (pages[currentPageIndex].pageLeave && typeof pages[currentPageIndex].pageLeave == 'function') {
result = pages[currentPageIndex].pageLeave();
if (result == false) {
return false;
}
}
if (onPageLeave && typeof onPageLeave == 'function') {
result = onPageLeave();
if (result == false) {
return false;
}
}
return true;
}
var pageShow = function () {
$('.dlgnav-item' + currentPageIndex).addClass('active');
if (pages[currentPageIndex].pageShow && typeof eval(pages[currentPageIndex].pageShow) == 'function') {
pages[currentPageIndex].pageShow = eval(pages[currentPageIndex].pageShow);
}
if (pages[currentPageIndex].pageShow && typeof pages[currentPageIndex].pageShow == 'function') {
pages[currentPageIndex].pageShow();
}
if (onPageShow && typeof onPageShow == 'function') {
onPageShow();
}
}
var reload = function (additionalData) {
currentPage.data = $.extend({}, currentPage.data, additionalData);
loadPageContent();
}
var setPageData = function(dataObject) {
if(!pages[currentPageIndex].hasOwnProperty('data')){
pages[currentPageIndex].data = {};
}
for (var property in dataObject) {
if (dataObject.hasOwnProperty(property)) {
pages[currentPageIndex].data[property] = dataObject[property];
}
}
}
var setAllPagesData = function(dataObject){
for(var index in pages){
if(!pages[index].hasOwnProperty('data')){
pages[index].data = {};
}
for (var property in dataObject) {
if (dataObject.hasOwnProperty(property)) {
pages[index].data[property] = dataObject[property];
}
}
}
}
var getDialog = function(){
return dialog;
}
init(options);
return {
nextPage: nextPage,
previousPage: previousPage,
savePage: savePage,
nthPage: nthPage,
close: close,
showDialog: showDialog,
reload: reload,
setPageData: setPageData,
setAllPagesData: setAllPagesData,
getDialog: getDialog
}
}
adcell.dialog.dialognavigator = function (options) {
return DialogNavigator(options);
}
} (jQuery, adcell))