如何在没有删除按钮的情况下获得第一个修订版?

时间:2018-02-02 19:49:13

标签: javascript angularjs svn ng-switch

<div class="right modal-footer">
        <a class="modal-action waves-effect btn-flat left" ng-switch-when="true" ng-click="delete()">Delete</a>
        <a class="modal-action waves-effect btn-flat" ng-click="close()">Cancel</a>
        <a class="modal-action waves-effect btn-flat" ng-click="save()">Save</a>
    </div>
</div>

第一个修订版或修订版A不应该有删除按钮。每个其他版本都需要有一个,所以修订版B,C等等。有没有人对我如何做到这一点有任何想法?

以上部分是摘要修订对话框的页脚。它包括保存,取消和删除的操作按钮。删除按钮是这里的主要焦点。

下面的部分是JavaScript控制器代码,它告诉按钮要做什么。当修订版打开时,它将显示原始修订版的详细信息以及这是哪个修订版。 close函数很简单,只需关闭对话框而不保存输入和更改的信息。保存功能也非常简单,保存输入的数据,并在单击保存按钮时显示更改。删除功能将删除当前版本并在其之前移回修订版本,例如,删除版本C将显示版本B.但我要做的是隐藏修订版A(第一版本)上的删除按钮,以便它不会删除初始版本,并保留删除按钮显示任何其他版本。

感谢开发人员的帮助。

angular.module('Comet').controller('RevisionEditController', function ($scope, $rootScope, $objectExtensions, $odataModel, $validation, $toast, ProposalsService, ErrorsService) {
const DIALOG_SEL = '#revisionEditDialog';
$scope.originalRevision = null;
$scope.revision = null;


/**
 * Opens the dialog.
 * @param {number} proposal - The proposal that the revision is for.
 * @param {object} [revision] - The existing revision to edit. Null when creating a new revision.
 */
$scope.open = function (proposal, revision) {
    $scope.originalRevision = new $odataModel.ProposalDetail(revision);
    $scope.revision = new $odataModel.ProposalDetail(revision);
    $scope.revision.rev = revision ? revision.rev : getNextRevision(proposal.proposalDetails);
    $scope.revision.proposal = {
        id: proposal.id
    };

    $(DIALOG_SEL).modal('open');
};


/**
 * Closes the dialog.
 */
$scope.close = function () {
    $(DIALOG_SEL).modal('close');
};


/**
 * Saves the revision.
 */
$scope.save = function () {
    if ($validation.validate($scope.revisionEditForm)) {
        $rootScope.dataReady = false;

        if ($scope.revision.id) {
            ProposalsService.editProposalDetail($scope.revision.proposal.id, $scope.revision.id, $scope.originalRevision, $scope.revision)
                .then(onSaveSuccess, onSaveFailure);
        } else {
            ProposalsService.addProposalDetail($scope.revision.proposal.id, $scope.revision)
                .then(onSaveSuccess, onSaveFailure);
        }
    }
};


/**
 * Deletes the revision.
 */
$scope.delete = function () {
    ProposalsService.deleteProposalDetail($scope.revision.proposal.id, $scope.revision.id)
        .then(onDeleteSuccess, onDeleteFailure);
};


/**
 * Calls the revision updated callback and closes the dialog.
 * @param {object} updatedRevision - The updated revision.
 */
function onSaveSuccess(updatedRevision) {
    $scope.$ctrl.onRevisionUpdated({ revision: updatedRevision });
    $scope.close();
    $rootScope.dataReady = true;
}


/**
 * Displays an error message and logs the exception.
 * @param {object} ex - The exception to log.
 */
function onSaveFailure(ex) {
    $toast.error('There was an error saving the revision. Please try again.');
    ErrorsService.log(ex);
    $rootScope.dataReady = true;
}


/**
 * Calls the revision deleted callback and closes the dialog.
 * @param {number} id - The ID of the revision that was deleted.
 */
function onDeleteSuccess(id) {
    $scope.$ctrl.onRevisionDeleted({ id: id });
    $scope.close();
    $rootScope.dataReady = true;
}


/**
 * Displays an error message and logs the exception.
 * @param {object} ex - The exception to log.
 */
function onDeleteFailure(ex) {
    $toast.error('There was an error deleting the revision. Please try again.');
    ErrorsService.log(ex);
    $rootScope.dataReady = true;
}


/**
 * Gets the next revision number.
 * @param {object[]} revisions - The previous revisions.
 * @returns {string} The next revision number.
 */
function getNextRevision(revisions) {
    var nextRevision = '';
    var latestRevision = revisions
        .sort(function (a, b) {
            var order = 0;

            if (a.id > b.id)
                order = -1;
            else if (a.id < b.id)
                order = 1;

            return order;
        })[0];

    if (latestRevision) {
        nextRevision = latestRevision.rev ? '' : 'A';
        var increment = true;

        for (var idx = latestRevision.rev.length - 1; idx >= 0 && increment; idx--) {
            var currLetter = latestRevision.rev[idx].charCodeAt(0);

            if (currLetter == 90) {
                nextRevision = nextRevision + 'A';
            } else {
                increment = false;
                nextRevision = String.fromCharCode(currLetter + 1) + nextRevision;
            }
        }

        if (nextRevision.length < latestRevision.rev.length) {
            nextRevision = latestRevision.substring(0, nextRevision.length - 1) + nextRevision;
        }
    }

    return nextRevision;
}

1 个答案:

答案 0 :(得分:0)

我假设有一个循环用于迭代修订,使用循环索引检查ngIf是否应显示该按钮。