/*
* Spreed WebRTC.
* Copyright (C) 2013-2015 struktur AG
*
* This file is part of Spreed WebRTC.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see .
*
*/
"use strict";
define(["angular"], function(angular) {
var modalController = ["$scope", "$modalInstance", "data", function($scope, $modalInstance, data) {
$scope.input = {
text: ''
};
$scope.id = data.id;
$scope.header = data.header || "";
$scope.msg = data.message || "";
$scope.okButtonLabel = data.okButtonLabel || "Ok";
$scope.cancelButtonLabel = data.cancelButtonLabel || "Cancel";
$scope.cancel = function() {
$modalInstance.dismiss('Canceled');
};
$scope.save = function() {
$modalInstance.close($scope.input.text);
};
$scope.hitEnter = function(evt) {
if (angular.equals(evt.keyCode, 13) && !(angular.equals($scope.input.text, null) || angular.equals($scope.input.text, ''))) {
$scope.save();
}
};
$scope.ok = function() {
$modalInstance.close('Ok');
};
$scope.close = function() {
$modalInstance.close('Close');
};
}];
// Alertify uniquified api wrapper
return ["$window", "$modal", "$templateCache", "translation", function($window, $modal, $templateCache, translation) {
// Overwrite templates from dialogs with fontawesome/i18n variants.
$templateCache.put('/dialogs/error.html', '
');
$templateCache.put('/dialogs/wait.html', '');
$templateCache.put('/dialogs/notify.html', '');
$templateCache.put('/dialogs/confirm.html', '');
// Add new template for prompt.
$templateCache.put('/dialogs/prompt.html', '');
var defaultMessages = {
error: translation._("Error"),
notify: translation._("Hint"),
confirm: translation._("Please confirm"),
prompt: translation._("More information required"),
okButtonLabel: translation._("Ok"),
cancelButtonLabel: translation._("Cancel"),
closeButtonLabel: translation._("Close")
};
var promptIdx = 0;
var api = {
defaultMessages: defaultMessages
};
var setupModal = function(data, type) {
return $modal.open({
templateUrl: '/dialogs/' + type +'.html',
controller: modalController,
resolve: {
data: function() { return data; }
}
});
};
var registeredCustomDialog = [];
var dialog = {
/**
* registerCustom registers a custom dialog. To overwrite an existing custom dialog simply use the same id.
*
* @param {Object} config Preferences for the custom dialog with the following properties:
* @param {String} config.baseType Existing dialog type to use as initial values and from which a template will be used.
* @param {String} config.type The ID of the custom dialog. The template name which is saved in $templateCache. If the the type is 'notify' the templateUrl must be '/dialogs/notify.html'.
* @param {String} [config.template] A custom template to use for the dialog instead of the baseType template.
* @param {String} [config.title] The title which the baseType modal dialog should display. If none is provided the baseType title is used.
* @param {String} [config.message] The message which the baseType modal dialog should display.
* @param {Function} [config.ok_cb] The callback function to be called on success.
* @param {Function} [config.err_cb] The callback function to be called on error.
*/
registerCustom: function(config) {
var conf = angular.extend({}, config);
if (!conf ||
conf && !conf.type ||
conf && !conf.baseType) {
throw new Error("Custom template not configured correctly.");
}
var templateUrl = '/dialogs/' + conf.type + '.html';
if (conf.template) {
$templateCache.put(templateUrl, conf.template);
} else {
$templateCache.put(templateUrl, $templateCache.get('/dialogs/' + conf.baseType + '.html'));
}
if (!conf.title) {
conf.title = api.defaultMessages[conf.baseType];
}
registeredCustomDialog[conf.type] = conf;
},
exec: function(n, title, message, ok_cb, err_cb) {
if (!message && title) {
message = title;
title = null;
}
if (!title) {
title = api.defaultMessages[n] || n;
}
var dlg = setupModal({'header': title, 'message': message}, n);
if (ok_cb) {
dlg.result.then(ok_cb, err_cb);
}
return dlg;
},
custom: function(type) {
var config = registeredCustomDialog[type];
if (!config) {
throw new Error('The custom dialog type "' + type + '" is not registered.');
}
return dialog.exec(config.type, config.title, config.message, config.ok_cb, config.err_cb);
},
error: function(title, message, ok_cb, err_cb) {
return dialog.exec("error", title, message, ok_cb, err_cb);
},
notify: function(title, message, ok_cb, err_cb) {
return dialog.exec("notify", title, message, ok_cb, err_cb);
},
alert: function(message, ok_cb, title) {
// Legacy function for compatibility with alertify.
return dialog.notify(title, message, ok_cb);
},
confirm: function(message, ok_cb, err_cb, title) {
// Legacy function for compatibility with alertify.
return dialog.exec("confirm", null, message, ok_cb, err_cb);
},
prompt: function(title, ok_cb, err_cb) {
var id = "allertifyPrompt" + (promptIdx++);
var data = {
okButtonLabel: api.defaultMessages.okButtonLabel || "Ok",
cancelButtonLabel: api.defaultMessages.cancelButtonLabel || "Cancel",
header: title,
id: id
};
var dlg = setupModal(data, "prompt");
dlg.result.then(function(text) {
if (ok_cb) {
ok_cb(text);
}
}, function() {
if (err_cb) {
err_cb();
}
});
dlg.opened.then(function() {
// Crude hack to get auto focus.
$window.setTimeout(function() {
var element = $window.document.getElementById(id);
if (element) {
element.focus();
}
}, 100);
});
}
};
// Expose the shit.
api.dialog = dialog;
return api;
}];
});