jquery selector reuse to optimize

git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1293684 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Olivier Lamy 2012-02-25 20:46:13 +00:00
parent cd9e790dd4
commit 32e04baacf
3 changed files with 45 additions and 42 deletions

View File

@ -125,9 +125,8 @@ $(function() {
* call from menu entry to display root level * call from menu entry to display root level
*/ */
displayBrowse=function(){ displayBrowse=function(){
clearUserMessages(); screenChange()
var mainContent = $("#main-content"); var mainContent = $("#main-content");
mainContent.attr("data-bind","");
mainContent.html($("#browse-tmpl" ).tmpl()); mainContent.html($("#browse-tmpl" ).tmpl());
mainContent.find("#browse_result").html(mediumSpinnerImg()); mainContent.find("#browse_result").html(mediumSpinnerImg());
$.ajax("restServices/archivaServices/browseService/rootGroups", { $.ajax("restServices/archivaServices/browseService/rootGroups", {

View File

@ -170,7 +170,7 @@ $(function() {
var updated = JSON.parse(result); var updated = JSON.parse(result);
if (updated == true) { if (updated == true) {
displaySuccessMessage($.i18n.prop("user.updated",currentUser.username())); displaySuccessMessage($.i18n.prop("user.updated",currentUser.username()));
$("#users-view-tabs-li-user-edit a").html($.i18n.prop("add")); $("#main-content #users-view-tabs-li-user-edit a").html($.i18n.prop("add"));
clearForm("#main-content #user-create"); clearForm("#main-content #user-create");
activateUsersGridTab(); activateUsersGridTab();
return this; return this;
@ -287,7 +287,7 @@ $(function() {
*/ */
adminCreateBox=function() { adminCreateBox=function() {
window.redbackModel.createUser=true; window.redbackModel.createUser=true;
jQuery("#main-content").attr("data-bind",'template: {name:"redback/user-edit-tmpl",data: user}'); $("#main-content").attr("data-bind",'template: {name:"redback/user-edit-tmpl",data: user}');
var viewModel = new AdminUserViewModel(); var viewModel = new AdminUserViewModel();
ko.applyBindings(viewModel); ko.applyBindings(viewModel);
$("#user-create").validate({ $("#user-create").validate({

View File

@ -39,25 +39,26 @@ $(function() {
headerText: "Email", headerText: "Email",
rowText: "email"} rowText: "email"}
], ],
pageSize: 5 pageSize: 10
}); });
this.addUser=function() { this.addUser=function() {
clearUserMessages(); clearUserMessages();
$("#createUserForm").html(""); var mainContent = $("#main-content");
$("#main-content #user-edit").remove(); mainContent.find("#createUserForm").html("");
$('#main-content #user-create').show(); mainContent.find("#user-edit").remove();
mainContent.find("#user-create").show();
var viewModel = new UserViewModel(new User(),false,self); var viewModel = new UserViewModel(new User(),false,self);
$.log("UsersViewModel#addUser"); $.log("UsersViewModel#addUser");
var createUserForm = $("#main-content #createUserForm"); var createUserForm = mainContent.find("#createUserForm");
createUserForm.html(smallSpinnerImg()); createUserForm.html(smallSpinnerImg());
createUserForm.attr("data-bind",'template: {name:"redback/user-edit-tmpl",data: user}'); createUserForm.attr("data-bind",'template: {name:"redback/user-edit-tmpl",data: user}');
ko.applyBindings(viewModel,createUserForm.get(0)); ko.applyBindings(viewModel,createUserForm.get(0));
$("#main-content #createUserForm #user-create-form-cancel-button").on( "click", function(e) { mainContent.find("#createUserForm #user-create-form-cancel-button").on( "click", function(e) {
e.preventDefault(); e.preventDefault();
activateUsersGridTab(); activateUsersGridTab();
}); });
$("#user-create").validate({ mainContent.find("#user-create").validate({
rules: { rules: {
confirmPassword: { confirmPassword: {
equalTo: "#password" equalTo: "#password"
@ -69,7 +70,7 @@ $(function() {
}); });
// desactivate roles pill when adding user // desactivate roles pill when adding user
$("#edit_user_details_pills_headers").hide(); mainContent.find("#edit_user_details_pills_headers").hide();
}; };
@ -124,10 +125,10 @@ $(function() {
editUserBox=function(user) { editUserBox=function(user) {
clearUserMessages(); clearUserMessages();
activateUsersEditTab(); activateUsersEditTab();
var mainContent = $("#main-content");
var viewModel = new UserViewModel(user,true,self); var viewModel = new UserViewModel(user,true,self);
$( "#main-content #user-edit-roles-view" ).append(smallSpinnerImg()); mainContent.find("#user-edit-roles-view" ).append(smallSpinnerImg());
$.ajax("restServices/redbackServices/roleManagementService/getEffectivelyAssignedRoles/"+encodeURIComponent(user.username()), { $.ajax("restServices/redbackServices/roleManagementService/getEffectivelyAssignedRoles/"+encodeURIComponent(user.username()), {
type: "GET", type: "GET",
dataType: 'json', dataType: 'json',
@ -138,19 +139,19 @@ $(function() {
user.assignedRoles = ko.observableArray(mappedRoles); user.assignedRoles = ko.observableArray(mappedRoles);
// user form binding // user form binding
var createUserForm = $("#main-content #createUserForm"); var createUserForm = mainContent.find("#createUserForm");
createUserForm.html(smallSpinnerImg()); createUserForm.html(smallSpinnerImg());
createUserForm.attr("data-bind",'template: {name:"redback/user-edit-tmpl",data: user}'); createUserForm.attr("data-bind",'template: {name:"redback/user-edit-tmpl",data: user}');
ko.applyBindings(viewModel,createUserForm.get(0)); ko.applyBindings(viewModel,createUserForm.get(0));
$("#main-content #users-view-tabs-li-user-edit a").html($.i18n.prop("edit")); mainContent.find("#users-view-tabs-li-user-edit a").html($.i18n.prop("edit"));
$("#main-content #user-create #user-create-form-cancel-button").on("click", function(e) { mainContent.find("#user-create #user-create-form-cancel-button").on("click", function(e) {
e.preventDefault(); e.preventDefault();
activateUsersGridTab(); activateUsersGridTab();
}); });
$("#main-content #user-create").validate({ mainContent.find("#user-create").validate({
rules: { rules: {
confirmPassword: { confirmPassword: {
equalTo: "#password" equalTo: "#password"
@ -160,16 +161,16 @@ $(function() {
customShowError("#main-content #user-create",validator,errorMap,errorMap); customShowError("#main-content #user-create",validator,errorMap,errorMap);
} }
}); });
$("#main-content #createUserForm #user-create #user-create-form-register-button").on("click", function(e) { mainContent.find("#createUserForm #user-create #user-create-form-register-button").on("click", function(e) {
e.preventDefault(); e.preventDefault();
}); });
// user roles binding // user roles binding
$("#main-content #user-edit-roles-view").attr("data-bind",'template: {name:"user_view_roles_list_tmpl"}'); mainContent.find("#user-edit-roles-view").attr("data-bind",'template: {name:"user_view_roles_list_tmpl"}');
ko.applyBindings(viewModel,$("#user-edit-roles-view").get(0)); ko.applyBindings(viewModel,mainContent.find("#user-edit-roles-view").get(0));
$("#main-content #edit_user_details_pills_headers a:first").tab('show'); mainContent.find("#edit_user_details_pills_headers a:first").tab('show');
$("#main-content #edit_user_details_pills_headers").bind('change', function (e) { mainContent.find("#edit_user_details_pills_headers").bind('change', function (e) {
if ($(e.target).attr("href")=="#user-edit-roles-edit") { if ($(e.target).attr("href")=="#user-edit-roles-edit") {
editUserRoles(user); editUserRoles(user);
} }
@ -185,7 +186,8 @@ $(function() {
editUserRoles=function(user){ editUserRoles=function(user){
var viewModel = new UserViewModel(user); var viewModel = new UserViewModel(user);
$("#user-edit-roles-edit").html(smallSpinnerImg()); var mainContent = $("#main-content");
mainContent.find("#user-edit-roles-edit").html(smallSpinnerImg());
$.ajax("restServices/redbackServices/roleManagementService/getApplicationRoles/"+encodeURIComponent(user.username()), { $.ajax("restServices/redbackServices/roleManagementService/getApplicationRoles/"+encodeURIComponent(user.username()), {
type: "GET", type: "GET",
dataType: 'json', dataType: 'json',
@ -195,8 +197,8 @@ $(function() {
}); });
viewModel.applicationRoles=ko.observableArray(mappedApplicationRoles); viewModel.applicationRoles=ko.observableArray(mappedApplicationRoles);
$.log("applicationRoles length:"+mappedApplicationRoles.length); $.log("applicationRoles length:"+mappedApplicationRoles.length);
$("#main-content #user-edit-roles-edit").attr("data-bind",'template: {name:"user_edit_roles_tmpl"}'); mainContent.find("#user-edit-roles-edit").attr("data-bind",'template: {name:"user_edit_roles_tmpl"}');
ko.applyBindings(viewModel,$("#main-content #user-edit-roles-edit").get(0)); ko.applyBindings(viewModel,mainContent.find("#user-edit-roles-edit").get(0));
$.log("assignedRoles:"+user.assignedRoles().length); $.log("assignedRoles:"+user.assignedRoles().length);
} }
} }
@ -236,8 +238,9 @@ $(function() {
*/ */
displayUsersGrid=function() { displayUsersGrid=function() {
screenChange(); screenChange();
$("#main-content").html(mediumSpinnerImg()); var mainContent = $("#main-content");
jQuery("#main-content").attr("data-bind",'template: {name:"usersGrid"}'); mainContent.html(mediumSpinnerImg());
mainContent.attr("data-bind",'template: {name:"usersGrid"}');
$.ajax("restServices/redbackServices/userService/getUsers", { $.ajax("restServices/redbackServices/userService/getUsers", {
type: "GET", type: "GET",
@ -249,8 +252,8 @@ $(function() {
var usersViewModel = new UsersViewModel(); var usersViewModel = new UsersViewModel();
usersViewModel.users(mappedUsers); usersViewModel.users(mappedUsers);
ko.applyBindings(usersViewModel,jQuery("#main-content").get(0)); ko.applyBindings(usersViewModel,jQuery("#main-content").get(0));
$("#main-content #users-view-tabs a:first").tab('show'); mainContent.find("#users-view-tabs a:first").tab('show');
$("#main-content #users-view-tabs a[data-toggle='tab']").on('show', function (e) { mainContent.find("#users-view-tabs a[data-toggle='tab']").on('show', function (e) {
//$.log( $(e.target).attr("href") ); // activated tab //$.log( $(e.target).attr("href") ); // activated tab
//e.relatedTarget // previous tab //e.relatedTarget // previous tab
$.log("tabs shown"); $.log("tabs shown");
@ -258,11 +261,11 @@ $(function() {
usersViewModel.addUser(); usersViewModel.addUser();
} }
if ($(e.target).attr("href")=="#users-view") { if ($(e.target).attr("href")=="#users-view") {
$("#main-content #users-view-tabs-li-user-edit a").html($.i18n.prop("add")); mainContent.find("#users-view-tabs-li-user-edit a").html($.i18n.prop("add"));
} }
}) })
$("#main-content #users-view-tabs-content #users-view").addClass("active"); mainContent.find("#users-view-tabs-content #users-view").addClass("active");
} }
} }
); );
@ -270,21 +273,22 @@ $(function() {
} }
activateUsersGridTab=function(){ activateUsersGridTab=function(){
var mainContent = $("#main-content");
$("#main-content #users-view-tabs li").removeClass("active"); mainContent.find("#users-view-tabs li").removeClass("active");
$("#main-content #users-view-tabs-content div").removeClass("active"); mainContent.find("#users-view-tabs-content div").removeClass("active");
// activate users grid tab // activate users grid tab
$("#main-content #users-view-tabs-content #users-view").addClass("active"); mainContent.find("#users-view-tabs-content #users-view").addClass("active");
$("#users-view-tabs-li-users-grid").addClass("active"); mainContent.find("#users-view-tabs-li-users-grid").addClass("active");
$("#main-content #users-view-tabs-li-user-edit a").html($.i18n.prop("add")); mainContent.find("#users-view-tabs-li-user-edit a").html($.i18n.prop("add"));
} }
activateUsersEditTab=function(){ activateUsersEditTab=function(){
$("#main-content #users-view-tabs li").removeClass("active"); var mainContent = $("#main-content");
$("#main-content #users-view-tabs-content div").removeClass("active"); mainContent.find("#users-view-tabs li").removeClass("active");
mainContent.find("#users-view-tabs-content div").removeClass("active");
// activate users edit tab // activate users edit tab
$("#main-content #users-view-tabs-content #createUserForm").addClass("active"); mainContent.find("#users-view-tabs-content #createUserForm").addClass("active");
$("#users-view-tabs-li-user-edit").addClass("active"); mainContent.find("#users-view-tabs-li-user-edit").addClass("active");
} }