Files
SCM-Manager/scm-webapp/src/main/webapp/resources/js/sonia.scm.js

496 lines
12 KiB
JavaScript
Raw Normal View History

2010-11-03 18:40:43 +01:00
/**
2010-10-31 19:22:53 +01:00
* Copyright (c) 2010, Sebastian Sdorra
* All rights reserved.
2010-11-03 18:40:43 +01:00
*
2010-10-31 19:22:53 +01:00
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
2010-11-03 18:40:43 +01:00
*
2010-10-31 19:22:53 +01:00
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of SCM-Manager; nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
2010-11-03 18:40:43 +01:00
*
2010-10-31 19:22:53 +01:00
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2010-11-03 18:40:43 +01:00
*
2010-10-31 19:22:53 +01:00
* http://bitbucket.org/sdorra/scm-manager
2010-11-03 18:40:43 +01:00
*
2010-09-04 16:36:25 +02:00
*/
2011-03-05 17:10:18 +01:00
Ext.ns("Sonia.scm");
2010-11-03 18:40:43 +01:00
2011-03-05 17:10:18 +01:00
Sonia.scm.Main = Ext.extend(Ext.util.Observable, {
2010-09-04 16:36:25 +02:00
2011-03-05 17:10:18 +01:00
tabRepositoriesText: 'Repositories',
navChangePasswordText: 'Change Password',
sectionMainText: 'Main',
sectionSecurityText: 'Security',
navRepositoriesText: 'Repositories',
sectionConfigText: 'Config',
navGeneralConfigText: 'General',
tabGeneralConfigText: 'SCM Config',
2010-09-05 13:54:49 +02:00
2011-03-05 17:10:18 +01:00
navRepositoryTypesText: 'Repository Types',
tabRepositoryTypesText: 'Repository Config',
navPluginsText: 'Plugins',
tabPluginsText: 'Plugins',
navUsersText: 'Users',
tabUsersText: 'Users',
navGroupsText: 'Groups',
tabGroupsText: 'Groups',
2010-11-03 18:40:43 +01:00
2011-03-05 17:10:18 +01:00
sectionLoginText: 'Login',
navLoginText: 'Login',
sectionLogoutText: 'Log out',
navLogoutText: 'Log out',
2010-11-03 18:40:43 +01:00
logoutFailedText: 'Logout Failed!',
errorTitle: 'Error',
errorMessage: 'Unknown error occurred.',
errorSessionExpiredTitle: 'Session expired',
errorSessionExpiredMessage: 'Your session is expired. Please relogin.',
2011-03-06 13:45:27 +01:00
mainTabPanel: null,
infoPanels: [],
2011-06-13 17:16:34 +02:00
scripts: [],
stylesheets: [],
2011-03-06 13:45:27 +01:00
constructor : function(config) {
2011-04-29 08:37:57 +02:00
this.addEvents('login', 'logout', 'init');
2011-03-06 13:45:27 +01:00
this.mainTabPanel = Ext.getCmp('mainTabPanel');
2011-03-06 14:14:55 +01:00
this.addListener('login', this.postLogin, this);
2011-03-06 13:45:27 +01:00
Sonia.scm.Main.superclass.constructor.call(this, config);
},
2011-04-29 08:37:57 +02:00
init: function(){
this.fireEvent('init', this);
},
registerInfoPanel: function(type, panel){
this.infoPanels[type] = panel;
},
getInfoPanel: function(type){
var rp = null;
var panel = this.infoPanels[type];
if ( panel == null ){
rp = {
xtype: 'repositoryInfoPanel'
};
} else {
rp = Sonia.util.clone( panel );
}
return rp;
2011-04-29 08:37:57 +02:00
},
2011-03-06 14:14:55 +01:00
postLogin: function(){
this.createMainMenu();
this.createRepositoryPanel();
},
2011-03-05 17:10:18 +01:00
createRepositoryPanel: function(){
if ( debug ){
console.debug('create repository panel');
}
2011-03-06 13:45:27 +01:00
this.mainTabPanel.add({
2011-03-05 17:10:18 +01:00
id: 'repositories',
xtype: 'repositoryPanel',
title: this.tabRepositoriesText,
closeable: false,
autoScroll: true
});
2011-03-06 13:45:27 +01:00
this.mainTabPanel.setActiveTab('repositories');
2011-03-05 17:10:18 +01:00
},
2010-10-15 23:03:00 +02:00
2011-03-05 17:10:18 +01:00
createMainMenu: function(){
2010-11-03 18:40:43 +01:00
if ( debug ){
console.debug('create main menu');
}
var panel = Ext.getCmp('navigationPanel');
2010-11-25 19:08:32 +01:00
panel.addSection({
2010-12-06 17:15:27 +01:00
id: 'navMain',
2011-03-05 17:10:18 +01:00
title: this.sectionMainText,
2011-03-07 08:08:11 +01:00
links: [{
2011-03-05 17:10:18 +01:00
label: this.navRepositoriesText,
fn: function(){
2011-03-06 13:45:27 +01:00
this.mainTabPanel.setActiveTab('repositories');
2011-03-06 13:39:54 +01:00
},
scope: this
2010-09-15 14:18:55 +02:00
}]
2010-11-25 19:08:32 +01:00
});
2011-01-07 13:12:26 +01:00
var securitySection = null;
if ( state.user.type == 'xml' && state.user.name != 'anonymous' ){
securitySection = {
2011-03-05 17:10:18 +01:00
title: this.sectionSecurityText,
2011-03-07 08:08:11 +01:00
links: [{
2011-03-05 17:10:18 +01:00
label: this.navChangePasswordText,
2011-01-07 13:12:26 +01:00
fn: function(){
new Sonia.action.ChangePasswordWindow().show();
2011-03-05 17:10:18 +01:00
}
2011-01-07 13:12:26 +01:00
}]
}
}
2010-11-25 19:08:32 +01:00
if ( admin ){
2011-01-07 13:12:26 +01:00
2010-11-25 19:08:32 +01:00
panel.addSections([{
2010-12-06 17:15:27 +01:00
id: 'navConfig',
2011-03-05 17:10:18 +01:00
title: this.sectionConfigText,
2011-03-07 08:08:11 +01:00
links: [{
2011-03-05 17:10:18 +01:00
label: this.navGeneralConfigText,
2010-11-25 19:08:32 +01:00
fn: function(){
2011-03-06 13:39:54 +01:00
this.addTabPanel("scmConfig", "scmConfig", this.navGeneralConfigText);
},
scope: this
2010-11-25 19:08:32 +01:00
},{
2011-03-05 17:10:18 +01:00
label: this.navRepositoryTypesText,
2010-11-25 19:08:32 +01:00
fn: function(){
2011-03-06 13:39:54 +01:00
this.addTabPanel('repositoryConfig', 'repositoryConfig', this.tabRepositoryTypesText);
},
scope: this
},{
2011-03-05 17:10:18 +01:00
label: this.navPluginsText,
fn: function(){
2011-03-06 13:39:54 +01:00
this.addTabPanel('plugins', 'pluginGrid', this.navPluginsText);
},
scope: this
2010-11-25 19:08:32 +01:00
}]
}]);
2011-01-07 13:12:26 +01:00
if ( securitySection == null ){
securitySection = {
2011-03-05 17:10:18 +01:00
title: this.sectionSecurityText,
2011-03-07 08:08:11 +01:00
links: []
2011-01-07 13:12:26 +01:00
}
}
2011-03-07 08:08:11 +01:00
securitySection.links.push({
2011-03-05 17:10:18 +01:00
label: this.navUsersText,
2011-01-07 13:12:26 +01:00
fn: function(){
2011-03-06 13:39:54 +01:00
this.addTabPanel('users', 'userPanel', this.navUsersText);
},
scope: this
2011-01-07 13:12:26 +01:00
});
2011-03-07 08:08:11 +01:00
securitySection.links.push({
2011-03-05 17:10:18 +01:00
label: this.navGroupsText,
2011-01-07 13:12:26 +01:00
fn: function(){
2011-03-06 13:39:54 +01:00
this.addTabPanel('groups', 'groupPanel', this.tabGroupsText);
},
scope: this
2011-01-07 13:12:26 +01:00
});
}
if ( securitySection != null ){
panel.addSection( securitySection );
2010-11-25 19:08:32 +01:00
}
2010-12-30 13:44:40 +01:00
if ( state.user.name == 'anonymous' ){
panel.addSection({
id: 'navLogin',
2011-03-05 17:10:18 +01:00
title: this.sectionLoginText,
2011-03-07 08:08:11 +01:00
links: [{
2011-03-05 17:10:18 +01:00
label: this.sectionLoginText,
2011-03-06 13:39:54 +01:00
fn: this.login,
scope: this
2010-12-30 13:44:40 +01:00
}]
});
} else {
panel.addSection({
id: 'navLogout',
2011-03-05 17:10:18 +01:00
title: this.sectionLogoutText,
2011-03-07 08:08:11 +01:00
links: [{
2011-03-05 17:10:18 +01:00
label: this.navLogoutText,
2011-03-06 13:39:54 +01:00
fn: this.logout,
scope: this
2010-12-30 13:44:40 +01:00
}]
});
}
2010-11-25 19:08:32 +01:00
//fix hidden logout button
panel.doLayout();
2011-03-06 13:39:54 +01:00
},
addTabPanel: function(id, xtype, title){
2011-04-03 12:48:30 +02:00
var panel = {
id: id,
xtype: xtype,
title: title,
closable: true,
autoScroll: true
};
this.addTab(panel);
},
addTab: function(panel){
var tab = this.mainTabPanel.findById(panel.id);
2011-03-06 13:39:54 +01:00
if ( tab == null ){
2011-04-03 12:48:30 +02:00
this.mainTabPanel.add(panel);
2011-03-06 13:39:54 +01:00
}
2011-04-03 12:48:30 +02:00
this.mainTabPanel.setActiveTab(panel.id);
2011-03-06 13:39:54 +01:00
},
loadState: function(s){
if ( debug ){
console.debug( s );
}
state = s;
admin = s.user.admin;
2011-03-06 13:39:54 +01:00
// call login callback functions
this.fireEvent("login", state);
2011-03-06 13:39:54 +01:00
},
clearState: function(){
// clear state
state = null;
// clear repository store
repositoryTypeStore.removeAll();
// remove all tabs
2011-03-06 13:45:27 +01:00
this.mainTabPanel.removeAll();
2011-03-06 13:39:54 +01:00
// remove navigation items
Ext.getCmp('navigationPanel').removeAll();
},
checkLogin: function(){
Ext.Ajax.request({
url: restUrl + 'authentication.json',
method: 'GET',
scope: this,
success: function(response){
if ( debug ){
console.debug('login success');
}
var s = Ext.decode(response.responseText);
this.loadState(s);
},
failure: function(){
if ( debug ){
console.debug('login failed');
}
var loginWin = new Sonia.login.Window();
loginWin.show();
}
});
},
login: function(){
this.clearState();
var loginWin = new Sonia.login.Window();
loginWin.show();
},
logout: function(){
Ext.Ajax.request({
url: restUrl + 'authentication/logout.json',
method: 'GET',
scope: this,
success: function(response){
if ( debug ){
console.debug('logout success');
}
this.clearState();
// call logout callback functions
this.fireEvent( "logout", state );
2011-03-06 13:39:54 +01:00
var s = null;
var text = response.responseText;
if ( text != null && text.length > 0 ){
s = Ext.decode( text );
}
if ( s != null && s.success ){
this.loadState(s);
} else {
// show login window
var loginWin = new Sonia.login.Window();
loginWin.show();
}
},
failure: function(){
if ( debug ){
console.debug('logout failed');
}
Ext.Msg.alert(this.logoutFailedText);
2011-03-06 13:39:54 +01:00
}
});
2011-03-06 14:14:55 +01:00
},
addListeners: function(event, callbacks){
Ext.each(callbacks, function(callback){
if ( Ext.isFunction(callback) ){
this.addListener(event, callback);
} else if (Ext.isObject(callback)) {
2011-03-06 14:18:20 +01:00
this.addListener(event, callback.fn, callback.scope);
2011-03-06 14:14:55 +01:00
} else if (debug){
console.debug( "callback is not a function or object. " + callback );
}
2011-03-06 14:18:20 +01:00
}, this);
},
handleFailure: function(status, title, message){
if (debug){
console.debug( 'handle failure for status code: ' + status );
}
if ( status == 401 ){
Ext.Msg.show({
title: this.errorSessionExpiredTitle,
msg: this.errorSessionExpiredMessage,
buttons: Ext.Msg.OKCANCEL,
fn: function(btn){
if ( btn == 'ok' ){
this.login();
}
},
scope: this
});
} else {
if ( title == null ){
title = this.errorTitle;
}
if ( message == null ){
message = this.errorMessage;
}
Ext.MessageBox.show({
title: title,
msg: String.format(message, status),
buttons: Ext.MessageBox.OK,
icon:Ext.MessageBox.ERROR
});
}
2011-06-13 17:16:34 +02:00
},
loadScript: function(url, callback, scope){
var doCallback = function(){
if (debug){
console.debug('call callback for script ' + url);
}
if ( scope ){
callback.call(scope);
} else {
callback();
}
}
2011-06-13 17:16:34 +02:00
if ( this.scripts.indexOf(url) < 0 ){
var js = document.createElement('script');
js.type = "text/javascript";
2011-06-14 22:01:52 +02:00
js.language = 'javascript';
2011-06-13 17:16:34 +02:00
js.src = url;
if ( Ext.isIE ){
js.onreadystatechange = function (){
if (this.readyState === 'loaded' ||
this.readyState === 'complete'){
doCallback();
}
}
} else {
js.onload = doCallback;
js.onerror = doCallback;
}
if (debug){
console.debug('load script ' + url);
}
2011-06-14 22:01:52 +02:00
document.body.appendChild(js);
// var head = document.getElementsByTagName('head')[0];
// head.appendChild(js);
2011-06-13 17:16:34 +02:00
this.scripts.push(url);
} else {
if (debug){
console.debug( 'script ' + url + ' allready loaded' );
}
doCallback();
2011-06-13 17:16:34 +02:00
}
},
loadStylesheet: function(url){
if ( this.stylesheets.indexOf(url) < 0 ){
var css = document.createElement('link');
css.rel = 'stylesheet';
css.type = 'text/css';
css.href = url;
document.head.appendChild(css);
this.stylesheets.push(url);
}
2010-09-05 16:14:46 +02:00
}
2011-03-05 17:10:18 +01:00
});
Ext.onReady(function(){
Ext.state.Manager.setProvider(new Ext.state.CookieProvider());
var mainTabPanel = new Ext.TabPanel({
id: 'mainTabPanel',
region: 'center',
2011-06-19 13:24:19 +02:00
deferredRender: false,
enableTabScroll: true
2011-03-05 17:10:18 +01:00
});
new Ext.Viewport({
layout: 'border',
items: [
new Ext.BoxComponent({
region: 'north',
id: 'north-panel',
contentEl: 'north',
height: 75
}), {
region: 'west',
id: 'navigationPanel',
title: 'Navigation',
xtype: 'navPanel',
split: true,
width: 200,
minSize: 175,
maxSize: 400,
collapsible: true,
margins: '0 0 0 5'
},
new Ext.BoxComponent({
region: 'south',
id: 'south-panel',
contentEl: 'south',
height: 16,
margins: '2 2 2 5'
}),
mainTabPanel
]
});
2011-03-06 13:39:54 +01:00
main = new Sonia.scm.Main();
2011-03-05 17:10:18 +01:00
2011-03-06 13:45:27 +01:00
/**
* Adds a tab to main TabPanel
*
* @deprecated use main.addTabPanel
*/
2011-03-05 17:10:18 +01:00
function addTabPanel(id, xtype, title){
2011-03-06 13:45:27 +01:00
main.addTabPanel(id, xtype, title);
2010-11-03 18:40:43 +01:00
}
2011-04-29 08:37:57 +02:00
main.addListeners('init', initCallbacks);
2011-03-06 14:14:55 +01:00
main.addListeners('login', loginCallbacks);
main.addListeners('logout', logoutCallbacks);
2010-09-04 16:36:25 +02:00
2011-04-29 08:37:57 +02:00
main.init();
main.checkLogin();
2010-09-04 16:36:25 +02:00
});