From 47aa5067521679edf4976aa6c2a0bc2c0a680dc0 Mon Sep 17 00:00:00 2001 From: Olivier Lamy Date: Thu, 2 Feb 2012 20:43:30 +0000 Subject: [PATCH] cleanup redback part to be more knockout based git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1239831 13f79535-47bb-0310-9956-ffa450edef68 --- .../src/main/webapp/js/redback/redback.js | 2 +- .../js/redback/templates/user-edit.html | 8 +- .../js/redback/templates/user-grids.html | 10 +- .../src/main/webapp/js/redback/user.js | 81 ++++----- .../src/main/webapp/js/redback/users.js | 164 ++++++++++-------- 5 files changed, 139 insertions(+), 126 deletions(-) diff --git a/archiva-modules/archiva-web/archiva-webapp-js/src/main/webapp/js/redback/redback.js b/archiva-modules/archiva-web/archiva-webapp-js/src/main/webapp/js/redback/redback.js index 186ddec26..eff82689a 100644 --- a/archiva-modules/archiva-web/archiva-webapp-js/src/main/webapp/js/redback/redback.js +++ b/archiva-modules/archiva-web/archiva-webapp-js/src/main/webapp/js/redback/redback.js @@ -19,7 +19,7 @@ $(function() { // define a container object with various datas - window.redbackModel = {usersViewModel:null,userOperationNames:null,key:null,userCreate:false,i18n:$.i18n.map,rolesViewModel:null}; + window.redbackModel = {userOperationNames:null,key:null,i18n:$.i18n.map,rolesViewModel:null}; // unbinding $("#user-create-form-cancel-button").on("click", function(){ diff --git a/archiva-modules/archiva-web/archiva-webapp-js/src/main/webapp/js/redback/templates/user-edit.html b/archiva-modules/archiva-web/archiva-webapp-js/src/main/webapp/js/redback/templates/user-edit.html index 5bd61c45e..904bb2e65 100644 --- a/archiva-modules/archiva-web/archiva-webapp-js/src/main/webapp/js/redback/templates/user-edit.html +++ b/archiva-modules/archiva-web/archiva-webapp-js/src/main/webapp/js/redback/templates/user-edit.html @@ -29,8 +29,8 @@
-
-
+
+
@@ -85,11 +85,11 @@
- +
-
+
diff --git a/archiva-modules/archiva-web/archiva-webapp-js/src/main/webapp/js/redback/templates/user-grids.html b/archiva-modules/archiva-web/archiva-webapp-js/src/main/webapp/js/redback/templates/user-grids.html index 936862057..8a38f6041 100644 --- a/archiva-modules/archiva-web/archiva-webapp-js/src/main/webapp/js/redback/templates/user-grids.html +++ b/archiva-modules/archiva-web/archiva-webapp-js/src/main/webapp/js/redback/templates/user-grids.html @@ -34,7 +34,7 @@ {{each(j, columnDefinition) columns}} ${ typeof columnDefinition.rowText == 'function' ? columnDefinition.rowText(row) : row[columnDefinition.rowText] } {{/each}} - Edit + Edit {{if row.username()=="admin" || row.username()=="guest"}} {{else}} @@ -49,7 +49,7 @@ {{if row.username()=="admin" || row.username()=="guest"}} {{else}} - + {{/if}} {{else}} @@ -57,7 +57,7 @@ {{if row.username()=="admin" || row.username()=="guest"}} {{else}} - + {{/if}} {{/if}} @@ -67,7 +67,7 @@ {{if row.username()=="admin" || row.username()=="guest"}} {{else}} - + {{/if}} @@ -77,7 +77,7 @@ {{if row.username()=="admin" || row.username()=="guest"}} {{else}} - + {{/if}} diff --git a/archiva-modules/archiva-web/archiva-webapp-js/src/main/webapp/js/redback/user.js b/archiva-modules/archiva-web/archiva-webapp-js/src/main/webapp/js/redback/user.js index 2a97aff5a..c857b0a19 100644 --- a/archiva-modules/archiva-web/archiva-webapp-js/src/main/webapp/js/redback/user.js +++ b/archiva-modules/archiva-web/archiva-webapp-js/src/main/webapp/js/redback/user.js @@ -53,19 +53,20 @@ $(function() { this.locked = ko.observable(locked); this.passwordChangeRequired = ko.observable(passwordChangeRequired); this.assignedRoles = ko.observableArray(new Array()); + this.remove = function() { if (ownerViewModel) { ownerViewModel.users.destroy(this); } }; - this.create = function() { + this.create = function(successFnCallback) { if (username == 'admin') { this.createAdmin(); } else { - this.createUser(); + this.createUser(successFnCallback); } }; - this.createUser = function() { + this.createUser = function(successFnCallback) { $.log("user#createUser"); var valid = $("#user-create").valid(); if (!valid) { @@ -81,7 +82,9 @@ $(function() { var created = JSON.parse(result); if (created == true) { displaySuccessMessage("user created:"+currentUser.username()); - window.redbackModel.usersViewModel.users.push(currentUser); + if (successFnCallback){ + successFnCallback(currentUser); + } clearForm("#main-content #user-create"); $("#main-content #user-create").hide(); activateUsersGridTab(); @@ -239,43 +242,43 @@ $(function() { }); } - this.unlock=function(){ - this.locked(false); - var curUser = this; - clearUserMessages(); - $.ajax("restServices/redbackServices/userService/unlockUser/"+encodeURIComponent(curUser.username()), { - type: "GET", - success: function(result) { - displaySuccessMessage($.i18n.prop("user.unlocked",curUser.username())); - }, - error: function(result) { - var obj = jQuery.parseJSON(result.responseText); - displayRedbackError(obj); - } - }); - } - - // value is boolean - this.changePasswordChangeRequired=function(value){ - this.passwordChangeRequired(value); - var curUser = this; - var url = "restServices/redbackServices/userService/passwordChangeRequired/"+encodeURIComponent(curUser.username()); - if (value==false){ - url = "restServices/redbackServices/userService/passwordChangeNotRequired/"+encodeURIComponent(curUser.username()); + this.unlock=function(){ + this.locked(false); + var curUser = this; + clearUserMessages(); + $.ajax("restServices/redbackServices/userService/unlockUser/"+encodeURIComponent(curUser.username()), { + type: "GET", + success: function(result) { + displaySuccessMessage($.i18n.prop("user.unlocked",curUser.username())); + }, + error: function(result) { + var obj = jQuery.parseJSON(result.responseText); + displayRedbackError(obj); + } + }); } - $.ajax(url, { - type: "GET", - success: function(result) { - displaySuccessMessage($.i18n.prop("user.passwordChangeRequired.updated",curUser.username())); - }, - error: function(result) { - var obj = jQuery.parseJSON(result.responseText); - displayRedbackError(obj); - } - }); - }; - this.i18n = $.i18n.prop; + // value is boolean + this.changePasswordChangeRequired=function(value){ + this.passwordChangeRequired(value); + var curUser = this; + var url = "restServices/redbackServices/userService/passwordChangeRequired/"+encodeURIComponent(curUser.username()); + if (value==false){ + url = "restServices/redbackServices/userService/passwordChangeNotRequired/"+encodeURIComponent(curUser.username()); + } + $.ajax(url, { + type: "GET", + success: function(result) { + displaySuccessMessage($.i18n.prop("user.passwordChangeRequired.updated",curUser.username())); + }, + error: function(result) { + var obj = jQuery.parseJSON(result.responseText); + displayRedbackError(obj); + } + }); + }; + + this.i18n = $.i18n.prop; } /** diff --git a/archiva-modules/archiva-web/archiva-webapp-js/src/main/webapp/js/redback/users.js b/archiva-modules/archiva-web/archiva-webapp-js/src/main/webapp/js/redback/users.js index 75bbcf599..c70e6819b 100644 --- a/archiva-modules/archiva-web/archiva-webapp-js/src/main/webapp/js/redback/users.js +++ b/archiva-modules/archiva-web/archiva-webapp-js/src/main/webapp/js/redback/users.js @@ -25,20 +25,6 @@ $(function() { this.users = ko.observableArray([]); var self = this; - this.loadUsers = function() { - $.ajax("restServices/redbackServices/userService/getUsers", { - type: "GET", - async: false, - dataType: 'json', - success: function(data) { - var mappedUsers = $.map(data.user, function(item) { - return mapUser(item); - }); - self.users(mappedUsers); - } - } - ); - }; this.gridViewModel = new ko.simpleGrid.viewModel({ data: this.users, viewModel: this, @@ -58,11 +44,15 @@ $(function() { this.addUser=function() { clearUserMessages(); - window.redbackModel.createUser=true; $("#createUserForm").html(""); $("#main-content #user-edit").remove(); $('#main-content #user-create').show(); - ko.renderTemplate("redback/user-edit-tmpl", new User(), null, $("#createUserForm").get(0),"replaceChildren"); + var viewModel = new UserViewModel(new User(),false,self); + $.log("UsersViewModel#addUser"); + var createUserForm = $("#main-content #createUserForm"); + createUserForm.html(smallSpinnerImg()); + createUserForm.attr("data-bind",'template: {name:"redback/user-edit-tmpl",data: user}'); + ko.applyBindings(viewModel,createUserForm.get(0)); $("#main-content #createUserForm #user-create-form-cancel-button").on( "click", function(e) { e.preventDefault(); activateUsersGridTab(); @@ -77,26 +67,23 @@ $(function() { customShowError("#main-content #user-create",validator,errorMap,errorMap); } }); - $("#main-content #createUserForm #user-create").delegate("#user-create-form-register-button", "click keydown", function(e) { - e.preventDefault(); - }); // desactivate roles pill when adding user $("#edit_user_details_pills_headers").hide(); }; - this.lock = function(user){ + lock = function(user){ clearUserMessages(); user.lock(); } - this.unlock = function(user){ + unlock = function(user){ clearUserMessages(); user.unlock(); } - this.passwordChangeRequire = function(user,forceChangedPassword){ + passwordChangeRequire = function(user,forceChangedPassword){ clearUserMessages(); user.changePasswordChangeRequired(forceChangedPassword); } @@ -107,49 +94,15 @@ $(function() { }); }; - - this.editUserBox=function(user) { - window.redbackModel.createUser=false; + editUserBox=function(user) { clearUserMessages(); activateUsersEditTab(); - $("#main-content #createUserForm").html(smallSpinnerImg()); - $("#main-content #createUserForm").attr("data-bind",'template: {name:"redback/user-edit-tmpl",data: user}'); - var viewModel = new UserViewModel(user); - - ko.applyBindings(viewModel,$("#main-content #createUserForm").get(0)); - - $("#main-content #users-view-tabs-li-user-edit a").html($.i18n.prop("edit")); - - $("#main-content #user-create #user-create-form-cancel-button").on("click", function(e) { - e.preventDefault(); - activateUsersGridTab(); - }); - - $("#main-content #user-create").validate({ - rules: { - confirmPassword: { - equalTo: "#password" - } - }, - showErrors: function(validator, errorMap, errorList) { - customShowError("#main-content #user-create",validator,errorMap,errorMap); - } - }); - $("#main-content #user-create").delegate("#user-create-form-save-button", "click keydown", function(e) { - e.preventDefault(); - $.log("users.js#editUserBox"); - var valid = $("#user-create").valid(); - if (!valid) { - return; - } - user.update(); - }); + var viewModel = new UserViewModel(user,true,self); $( "#main-content #user-edit-roles-view" ).append(smallSpinnerImg()); $.ajax("restServices/redbackServices/roleManagementService/getEffectivelyAssignedRoles/"+encodeURIComponent(user.username()), { type: "GET", - async: false, dataType: 'json', success: function(data) { var mappedRoles = $.map(data.role, function(item) { @@ -157,13 +110,39 @@ $(function() { }); user.assignedRoles = ko.observableArray(mappedRoles); + // user form binding + var createUserForm = $("#main-content #createUserForm"); + createUserForm.html(smallSpinnerImg()); + createUserForm.attr("data-bind",'template: {name:"redback/user-edit-tmpl",data: user}'); + ko.applyBindings(viewModel,createUserForm.get(0)); + + $("#main-content #users-view-tabs-li-user-edit a").html($.i18n.prop("edit")); + + $("#main-content #user-create #user-create-form-cancel-button").on("click", function(e) { + e.preventDefault(); + activateUsersGridTab(); + }); + + $("#main-content #user-create").validate({ + rules: { + confirmPassword: { + equalTo: "#password" + } + }, + showErrors: function(validator, errorMap, errorList) { + customShowError("#main-content #user-create",validator,errorMap,errorMap); + } + }); + $("#main-content #createUserForm #user-create #user-create-form-register-button").on("click", function(e) { + e.preventDefault(); + }); + + // user roles binding $("#main-content #user-edit-roles-view").attr("data-bind",'template: {name:"user_view_roles_list_tmpl"}'); ko.applyBindings(viewModel,$("#user-edit-roles-view").get(0)); $("#main-content #edit_user_details_pills_headers").tabs(); $("#main-content #edit_user_details_pills_headers").bind('change', function (e) { - //$.log( $(e.target).attr("href") ); // activated tab - //e.relatedTarget // previous tab if ($(e.target).attr("href")=="#user-edit-roles-edit") { editUserRoles(user); } @@ -174,6 +153,7 @@ $(function() { ); } + } editUserRoles=function(user){ @@ -196,14 +176,32 @@ $(function() { ); } - UserViewModel=function(user) { + UserViewModel=function(user,updateMode,usersViewModel) { this.user=user; this.applicationRoles = ko.observableArray(new Array()); - + this.usersViewModel=usersViewModel; + this.updateMode=updateMode; + var self=this; updateUserRoles=function(){ this.user.updateAssignedRoles(); } + saveUser=function(){ + $.log("UserViewModel#saveUser"); + var valid = $("#main-content #user-create").valid(); + if (valid==false) { + $.log("user#save valid:false"); + return; + } else { + $.log("user#save valid:true,update:"+self.updateMode); + } + if (self.updateMode==false){ + return user.create(function(){self.usersViewModel.users.push(user)}); + } else { + return user.update(); + } + } + } /** @@ -213,23 +211,35 @@ $(function() { screenChange(); $("#main-content").html(mediumSpinnerImg()); jQuery("#main-content").attr("data-bind",'template: {name:"usersGrid"}'); - window.redbackModel.usersViewModel = new UsersViewModel(); - window.redbackModel.usersViewModel.loadUsers(); - ko.applyBindings(window.redbackModel.usersViewModel,jQuery("#main-content").get(0)); - $("#main-content #users-view-tabs a:first").tab('show'); - $("#main-content #users-view-tabs a[data-toggle='tab']").on('show', function (e) { - //$.log( $(e.target).attr("href") ); // activated tab - //e.relatedTarget // previous tab - $.log("tabs shown"); - if ($(e.target).attr("href")=="#createUserForm") { - window.redbackModel.usersViewModel.addUser(); - } - if ($(e.target).attr("href")=="#users-view") { - $("#main-content #users-view-tabs-li-user-edit a").html($.i18n.prop("add")); + + $.ajax("restServices/redbackServices/userService/getUsers", { + type: "GET", + dataType: 'json', + success: function(data) { + var mappedUsers = $.map(data.user, function(item) { + return mapUser(item); + }); + var usersViewModel = new UsersViewModel(); + usersViewModel.users(mappedUsers); + ko.applyBindings(usersViewModel,jQuery("#main-content").get(0)); + $("#main-content #users-view-tabs a:first").tab('show'); + $("#main-content #users-view-tabs a[data-toggle='tab']").on('show', function (e) { + //$.log( $(e.target).attr("href") ); // activated tab + //e.relatedTarget // previous tab + $.log("tabs shown"); + if ($(e.target).attr("href")=="#createUserForm") { + usersViewModel.addUser(); + } + if ($(e.target).attr("href")=="#users-view") { + $("#main-content #users-view-tabs-li-user-edit a").html($.i18n.prop("add")); + } + + }) + $("#main-content #users-view-tabs-content #users-view").addClass("active"); + } } + ); - }) - $("#main-content #users-view-tabs-content #users-view").addClass("active"); } activateUsersGridTab=function(){ -- 2.39.5