namespace-panel-directive.js 35.8 KB
Newer Older
lepdou committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
directive_module.directive('apollonspanel', directive);

function directive($window, toastr, AppUtil, EventManager, PermissionService, NamespaceLockService,
                   UserService, CommitService, ReleaseService, InstanceService, NamespaceBranchService, ConfigService) {
    return {
        restrict: 'E',
        templateUrl: '../../views/component/namespace-panel.html',
        transclude: true,
        replace: true,
        scope: {
            namespace: '=',
            appId: '=',
            env: '=',
            cluster: '=',
            user: '=',
            lockCheck: '=',
            createItem: '=',
            editItem: '=',
            preDeleteItem: '=',
            showText: '=',
            showNoModifyPermissionDialog: '=',
            preCreateBranch: '=',
            preDeleteBranch: '=',
            showMergeAndPublishGrayTips: '='
        },
        link: function (scope) {

            //constants
            var namespace_view_type = {
                TEXT: 'text',
                TABLE: 'table',
                HISTORY: 'history',
                INSTANCE: 'instance',
                RULE: 'rule'
            };

            var namespace_instance_view_type = {
                LATEST_RELEASE: 'latest_release',
                NOT_LATEST_RELEASE: 'not_latest_release',
                ALL: 'all'
            };

            var operate_branch_storage_key = 'OperateBranch';

            scope.switchView = switchView;
            scope.toggleItemSearchInput = toggleItemSearchInput;
            scope.searchItems = searchItems;
            scope.loadCommitHistory = loadCommitHistory;
            scope.toggleTextEditStatus = toggleTextEditStatus;
            scope.goToSyncPage = goToSyncPage;
            scope.modifyByText = modifyByText;
            scope.goToParentAppConfigPage = goToParentAppConfigPage;
            scope.switchInstanceViewType = switchInstanceViewType;
            scope.switchBranch = switchBranch;
            scope.loadInstanceInfo = loadInstanceInfo;
            scope.refreshInstancesInfo = refreshInstancesInfo;
            scope.deleteRuleItem = deleteRuleItem;
            scope.rollback = rollback;

            scope.publish = publish;
            scope.mergeAndPublish = mergeAndPublish;
            scope.addRuleItem = addRuleItem;
            scope.editRuleItem = editRuleItem;

lepdou committed
65
            scope.deleteNamespace = deleteNamespace;
66

lepdou committed
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
            var subscriberId = EventManager.subscribe(EventManager.EventType.UPDATE_GRAY_RELEASE_RULES,
                                                      function (context) {
                                                          useRules(context.branch);
                                                      }, scope.namespace.baseInfo.namespaceName);

            scope.$on('$destroy', function () {
                EventManager.unsubscribe(EventManager.EventType.UPDATE_GRAY_RELEASE_RULES,
                                         subscriberId, scope.namespace.baseInfo.namespaceName);
            });

            init();

            function init() {
                initNamespace(scope.namespace);
                initOther();
            }

            function initNamespace(namespace, viewType) {
                namespace.hasBranch = false;
                namespace.isBranch = false;
                namespace.isLinkedNamespace =
                    namespace.isPublic ? namespace.parentAppId != namespace.baseInfo.appId : false;
lepdou committed
89 90
                namespace.displayControl = {
                    currentOperateBranch: 'master',
lepdou committed
91 92
                    showSearchInput: false,
                    show: true
lepdou committed
93
                };
lepdou committed
94 95 96 97 98 99 100 101 102
                namespace.viewItems = namespace.items;
                namespace.isPropertiesFormat = namespace.format == 'properties';
                namespace.isTextEditing = false;
                namespace.instanceViewType = namespace_instance_view_type.LATEST_RELEASE;
                namespace.latestReleaseInstancesPage = 0;
                namespace.allInstances = [];
                namespace.allInstancesPage = 0;
                namespace.commitChangeBtnDisabled = false;

lepdou committed
103
                generateNamespaceId(namespace);
lepdou committed
104 105 106 107 108
                initNamespaceBranch(namespace);
                initNamespaceViewName(namespace);
                initNamespaceLock(namespace);
                initNamespaceInstancesCount(namespace);
                initPermission(namespace);
109
                initLinkedNamespace(namespace);
110
                loadInstanceInfo(namespace);
lepdou committed
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135

                function initNamespaceBranch(namespace) {
                    NamespaceBranchService.findNamespaceBranch(scope.appId, scope.env,
                                                               namespace.baseInfo.clusterName,
                                                               namespace.baseInfo.namespaceName)
                        .then(function (result) {

                            if (!result.baseInfo) {
                                return;
                            }

                            //namespace has branch
                            namespace.hasBranch = true;
                            namespace.branchName = result.baseInfo.clusterName;
                            //init branch
                            namespace.branch = result;
                            namespace.branch.isBranch = true;
                            namespace.branch.parentNamespace = namespace;
                            namespace.branch.viewType = namespace_view_type.TABLE;
                            namespace.branch.isPropertiesFormat = namespace.format == 'properties';
                            namespace.branch.allInstances = [];//master namespace all instances
                            namespace.branch.latestReleaseInstances = [];
                            namespace.branch.latestReleaseInstancesPage = 0;
                            namespace.branch.instanceViewType = namespace_instance_view_type.LATEST_RELEASE;
                            namespace.branch.hasLoadInstances = false;
lepdou committed
136 137 138
                            namespace.branch.displayControl = {
                                show: true
                            };
lepdou committed
139

lepdou committed
140
                            generateNamespaceId(namespace.branch);
lepdou committed
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
                            initBranchItems(namespace.branch);
                            initRules(namespace.branch);
                            loadInstanceInfo(namespace.branch);
                            initNamespaceLock(namespace.branch);
                            initPermission(namespace);
                            initUserOperateBranchScene(namespace);
                        });

                    function initBranchItems(branch) {
                        branch.masterItems = [];
                        branch.branchItems = [];

                        var masterItemsMap = {};
                        branch.parentNamespace.items.forEach(function (item) {
                            if (item.item.key) {
                                masterItemsMap[item.item.key] = item;
                            }
                        });

                        var branchItemsMap = {};

                        var itemModifiedCnt = 0;
                        branch.items.forEach(function (item) {
                            var key = item.item.key;
                            var masterItem = masterItemsMap[key];

                            //modify master item and set item's masterReleaseValue
                            if (masterItem) {
                                if (masterItem.isModified && masterItem.oldValue) {
                                    item.masterReleaseValue = masterItem.oldValue;
                                } else if (masterItem.item.value) {
                                    item.masterReleaseValue = masterItem.item.value;
                                }

                            } else {//delete branch item
                                item.masterReleaseValue = '';
                            }

                            //delete master item. ignore
                            if (item.isDeleted && masterItem) {
                                if (item.masterReleaseValue != item.oldValue) {
                                    itemModifiedCnt++;
                                    branch.branchItems.push(item);
                                }
                            } else {//branch's item
                                branchItemsMap[key] = item;

                                if (item.isModified) {
                                    itemModifiedCnt++;
                                }
                                branch.branchItems.push(item);
                            }

                        });
                        branch.itemModifiedCnt = itemModifiedCnt;

                        branch.parentNamespace.items.forEach(function (item) {
                            if (item.item.key) {
                                if (!branchItemsMap[item.item.key]) {
                                    branch.masterItems.push(item);
                                } else {
                                    item.hasBranchValue = true;
                                }
                            }
                        })

                    }
                }

lepdou committed
210
                function generateNamespaceId(namespace) {
211
                    namespace.id = Math.random().toString(36).substr(2);
lepdou committed
212
                }
213

lepdou committed
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
                function initPermission(namespace) {

                    PermissionService.has_modify_namespace_permission(
                        scope.appId,
                        namespace.baseInfo.namespaceName)
                        .then(function (result) {
                            //branch has same permission
                            namespace.hasModifyPermission = result.hasPermission;
                            if (namespace.branch) {
                                namespace.branch.hasModifyPermission = result.hasPermission;
                            }
                        });

                    PermissionService.has_release_namespace_permission(
                        scope.appId,
                        namespace.baseInfo.namespaceName)
                        .then(function (result) {
                            //branch has same permission
                            namespace.hasReleasePermission = result.hasPermission;
                            if (namespace.branch) {
                                namespace.branch.hasReleasePermission = result.hasPermission;
                            }
                        });
                }

239
                function initLinkedNamespace(namespace) {
240
                    if (!namespace.isPublic || !namespace.isLinkedNamespace || namespace.format != 'properties') {
241 242 243
                        return;
                    }
                    //load public namespace
244 245
                    ConfigService.load_public_namespace_for_associated_namespace(scope.env, scope.appId, scope.cluster,
                                                                                 namespace.baseInfo.namespaceName)
246 247 248 249 250 251 252 253 254 255 256 257 258 259
                        .then(function (result) {
                            var publicNamespace = result;
                            namespace.publicNamespace = publicNamespace;

                            var linkNamespaceItemKeys = [];
                            namespace.items.forEach(function (item) {
                                var key = item.item.key;
                                linkNamespaceItemKeys.push(key);
                            });

                            publicNamespace.viewItems = [];
                            publicNamespace.items.forEach(function (item) {
                                var key = item.item.key;

260
                                if (key) {
261 262 263 264 265 266 267
                                    publicNamespace.viewItems.push(item);
                                }

                                item.covered = linkNamespaceItemKeys.indexOf(key) >= 0;

                                if (item.isModified || item.isDeleted) {
                                    publicNamespace.isModified = true;
268
                                } else if (key) {
269 270 271 272 273 274 275 276
                                    publicNamespace.hasPublishedItem = true;
                                }
                            });

                        });

                }

lepdou committed
277 278 279 280
                function initNamespaceViewName(namespace) {
                    //namespace view name hide suffix
                    namespace.viewName =
                        namespace.baseInfo.namespaceName.replace(".xml", "").replace(
281 282
                            ".properties", "").replace(".json", "").replace(".yml", "")
                            .replace(".yaml", "");
lepdou committed
283 284 285 286 287 288 289 290 291 292 293 294 295

                    if (!viewType) {
                        if (namespace.isPropertiesFormat) {
                            switchView(namespace, namespace_view_type.TABLE);
                        } else {
                            switchView(namespace, namespace_view_type.TEXT);
                        }
                    } else if (viewType == namespace_view_type.TABLE) {
                        namespace.viewType = namespace_view_type.TABLE;
                    }
                }

                function initNamespaceLock(namespace) {
lepdou committed
296 297 298
                    NamespaceLockService.get_namespace_lock(scope.appId, scope.env,
                                                            namespace.baseInfo.clusterName,
                                                            namespace.baseInfo.namespaceName)
lepdou committed
299
                        .then(function (result) {
lepdou committed
300 301
                            namespace.lockOwner = result.lockOwner;
                            namespace.isEmergencyPublishAllowed = result.isEmergencyPublishAllowed;
lepdou committed
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
                        });

                }

                function initUserOperateBranchScene(namespace) {
                    var operateBranchStorage = JSON.parse(localStorage.getItem(operate_branch_storage_key));
                    var namespaceId = [scope.appId, scope.env, scope.cluster, namespace.baseInfo.namespaceName].join(
                        "+");
                    if (!operateBranchStorage) {
                        operateBranchStorage = {};
                    }
                    if (!operateBranchStorage[namespaceId]) {
                        operateBranchStorage[namespaceId] = namespace.branchName;
                    }

                    localStorage.setItem(operate_branch_storage_key, JSON.stringify(operateBranchStorage));

                    switchBranch(operateBranchStorage[namespaceId]);

                }
322

lepdou committed
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
            }

            function initNamespaceInstancesCount(namespace) {
                InstanceService.getInstanceCountByNamespace(scope.appId,
                                                            scope.env,
                                                            scope.cluster,
                                                            namespace.baseInfo.namespaceName)
                    .then(function (result) {
                        namespace.instancesCount = result.num;
                    })
            }

            function initOther() {

                UserService.load_user().then(function (result) {
                    scope.currentUser = result.userId;
                });

                PermissionService.has_assign_user_permission(scope.appId)
                    .then(function (result) {
                        scope.hasAssignUserPermission = result.hasPermission;
                    }, function (result) {

                    });
            }

            function switchBranch(branchName) {
                if (branchName != 'master') {
lepdou committed
351
                    scope.namespace.branch.displayControl.show = true;
lepdou committed
352
                    initRules(scope.namespace.branch);
lepdou committed
353 354
                } else {
                    scope.namespace.displayControl.show = true;
lepdou committed
355
                }
lepdou committed
356
                scope.namespace.displayControl.currentOperateBranch = branchName;
lepdou committed
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417

                //save to local storage
                var operateBranchStorage = JSON.parse(localStorage.getItem(operate_branch_storage_key));
                if (!operateBranchStorage) {
                    return;
                }
                var namespaceId = [scope.appId, scope.env, scope.cluster, scope.namespace.baseInfo.namespaceName].join(
                    "+");
                operateBranchStorage[namespaceId] = branchName;
                localStorage.setItem(operate_branch_storage_key, JSON.stringify(operateBranchStorage));

            }

            function switchView(namespace, viewType) {
                namespace.viewType = viewType;
                if (namespace_view_type.TEXT == viewType) {
                    namespace.text = parseModel2Text(namespace);
                } else if (namespace_view_type.TABLE == viewType) {

                } else if (namespace_view_type.HISTORY == viewType) {
                    loadCommitHistory(namespace);
                } else if (namespace_view_type.INSTANCE == viewType) {
                    refreshInstancesInfo(namespace);
                }
            }

            function switchInstanceViewType(namespace, type) {
                namespace.instanceViewType = type;
                loadInstanceInfo(namespace);
            }

            function loadCommitHistory(namespace) {
                if (!namespace.commits) {
                    namespace.commits = [];
                    namespace.commitPage = 0;
                }

                var size = 10;
                CommitService.find_commits(scope.appId,
                                           scope.env,
                                           namespace.baseInfo.clusterName,
                                           namespace.baseInfo.namespaceName,
                                           namespace.commitPage,
                                           size)
                    .then(function (result) {
                        if (result.length < size) {
                            namespace.hasLoadAllCommit = true;
                        }

                        for (var i = 0; i < result.length; i++) {
                            //to json
                            result[i].changeSets = JSON.parse(result[i].changeSets);
                            namespace.commits.push(result[i]);
                        }
                        namespace.commitPage += 1;
                    }, function (result) {
                        toastr.error(AppUtil.errorMsg(result), "加载修改历史记录出错");
                    });
            }

            function loadInstanceInfo(namespace) {
418

lepdou committed
419 420 421 422 423 424 425 426 427
                var size = 20;
                if (namespace.isBranch) {
                    size = 2000;
                }

                var type = namespace.instanceViewType;

                if (namespace_instance_view_type.LATEST_RELEASE == type) {
                    if (!namespace.latestRelease) {
428 429 430 431 432
                        ReleaseService.findLatestActiveRelease(scope.appId,
                                                               scope.env,
                                                               namespace.baseInfo.clusterName,
                                                               namespace.baseInfo.namespaceName)
                            .then(function (result) {
433 434
                                namespace.isLatestReleaseLoaded = true;

435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
                                if (!result) {
                                    namespace.latestReleaseInstances = {};
                                    namespace.latestReleaseInstances.total = 0;
                                    return;
                                }
                                namespace.latestRelease = result;
                                InstanceService.findInstancesByRelease(scope.env,
                                                                       namespace.latestRelease.id,
                                                                       namespace.latestReleaseInstancesPage,
                                                                       size)
                                    .then(function (result) {
                                        namespace.latestReleaseInstances = result;
                                        namespace.latestReleaseInstancesPage++;
                                    })
                            });
lepdou committed
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
                    } else {
                        InstanceService.findInstancesByRelease(scope.env,
                                                               namespace.latestRelease.id,
                                                               namespace.latestReleaseInstancesPage,
                                                               size)
                            .then(function (result) {
                                if (result && result.content.length) {
                                    namespace.latestReleaseInstancesPage++;
                                    result.content.forEach(function (instance) {
                                        namespace.latestReleaseInstances.content.push(
                                            instance);
                                    })
                                }

                            })
                    }

                } else if (namespace_instance_view_type.NOT_LATEST_RELEASE == type) {
                    if (!namespace.latestRelease) {
                        return;
                    }
                    InstanceService.findByReleasesNotIn(scope.appId,
                                                        scope.env,
                                                        scope.cluster,
                                                        namespace.baseInfo.namespaceName,
                                                        namespace.latestRelease.id)
                        .then(function (result) {
                            if (!result || result.length == 0) {
                                return
                            }

                            var groupedInstances = {},
                                notLatestReleases = [];

                            result.forEach(function (instance) {
                                var configs = instance.configs;
                                if (configs && configs.length > 0) {
                                    configs.forEach(function (instanceConfig) {
                                        var release = instanceConfig.release;
                                        //filter dirty data
                                        if (!release) {
                                            return;
                                        }
                                        if (!groupedInstances[release.id]) {
                                            groupedInstances[release.id] = [];
                                            notLatestReleases.push(release);
                                        }
                                        groupedInstances[release.id].push(instance);
                                    })
                                }
                            });

                            namespace.notLatestReleases = notLatestReleases;
                            namespace.notLatestReleaseInstances = groupedInstances;
                        })

                } else {
                    InstanceService.findInstancesByNamespace(scope.appId,
                                                             scope.env,
                                                             scope.cluster,
                                                             namespace.baseInfo.namespaceName,
                                                             '',
                                                             namespace.allInstancesPage)
                        .then(function (result) {
                            if (result && result.content.length) {
                                namespace.allInstancesPage++;
                                result.content.forEach(function (instance) {
                                    namespace.allInstances.push(instance);
                                })
                            }
                        });
                }

            }

            function refreshInstancesInfo(namespace) {

                namespace.instanceViewType = namespace_instance_view_type.LATEST_RELEASE;

                namespace.latestReleaseInstancesPage = 0;
                namespace.latestReleaseInstances = [];
                namespace.latestRelease = undefined;

                if (!namespace.isBranch) {
                    namespace.notLatestReleaseNames = [];
                    namespace.notLatestReleaseInstances = {};

                    namespace.allInstancesPage = 0;
                    namespace.allInstances = [];
                }

                initNamespaceInstancesCount(namespace);
                loadInstanceInfo(namespace);
            }

            function initRules(branch) {

                NamespaceBranchService.findBranchGrayRules(scope.appId,
                                                           scope.env,
                                                           scope.cluster,
                                                           scope.namespace.baseInfo.namespaceName,
                                                           branch.baseInfo.clusterName)
                    .then(function (result) {
553

lepdou committed
554 555 556
                        if (result.appId) {
                            branch.rules = result;
                        }
557

lepdou committed
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
                    }, function (result) {
                        toastr.error(AppUtil.errorMsg(result), "加载灰度规则出错");
                    });

            }

            function addRuleItem(branch) {
                var newRuleItem = {
                    clientAppId: !branch.parentNamespace.isPublic ? branch.baseInfo.appId : '',
                    clientIpList: [],
                    draftIpList: [],
                    isNew: true
                };

                branch.editingRuleItem = newRuleItem;

                EventManager.emit(EventManager.EventType.EDIT_GRAY_RELEASE_RULES, {
                    branch: branch
                });
            }

            function editRuleItem(branch, ruleItem) {
                ruleItem.isNew = false;
                ruleItem.draftIpList = _.clone(ruleItem.clientIpList);
                branch.editingRuleItem = ruleItem;

                EventManager.emit(EventManager.EventType.EDIT_GRAY_RELEASE_RULES, {
                    branch: branch
                });
            }

            function deleteRuleItem(branch, ruleItem) {
                branch.rules.ruleItems.forEach(function (item, index) {
                    if (item.clientAppId == ruleItem.clientAppId) {
                        branch.rules.ruleItems.splice(index, 1);
                        toastr.success("删除成功");
                    }
                });

                useRules(branch);
            }

            function useRules(branch) {
                NamespaceBranchService.updateBranchGrayRules(scope.appId,
                                                             scope.env,
                                                             scope.cluster,
                                                             scope.namespace.baseInfo.namespaceName,
                                                             branch.baseInfo.clusterName,
                                                             branch.rules
607
                )
lepdou committed
608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696
                    .then(function (result) {
                        toastr.success('灰度规则更新成功');

                        //show tips if branch has not release configs
                        if (branch.itemModifiedCnt) {
                            AppUtil.showModal("#updateRuleTips");
                        }

                        setTimeout(function () {
                            refreshInstancesInfo(branch);
                        }, 1500);

                    }, function (result) {
                        AppUtil.showErrorMsg(result, "灰度规则更新失败");
                    })
            }

            function toggleTextEditStatus(namespace) {
                if (!scope.lockCheck(namespace)) {
                    return;
                }
                namespace.isTextEditing = !namespace.isTextEditing;
                if (namespace.isTextEditing) {//切换为编辑状态
                    namespace.commited = false;
                    namespace.backupText = namespace.text;
                    namespace.editText = parseModel2Text(namespace);

                } else {
                    if (!namespace.commited) {//取消编辑,则复原
                        namespace.text = namespace.backupText;
                    }
                }
            }

            function goToSyncPage(namespace) {
                if (!scope.lockCheck(namespace)) {
                    return false;
                }
                $window.location.href =
                    "config/sync.html?#/appid=" + scope.appId + "&env="
                    + scope.env + "&clusterName="
                    + scope.cluster
                    + "&namespaceName=" + namespace.baseInfo.namespaceName;
            }

            function modifyByText(namespace) {
                var model = {
                    configText: namespace.editText,
                    namespaceId: namespace.baseInfo.id,
                    format: namespace.format
                };

                //prevent repeat submit
                if (namespace.commitChangeBtnDisabled) {
                    return;
                }
                namespace.commitChangeBtnDisabled = true;
                ConfigService.modify_items(scope.appId,
                                           scope.env,
                                           scope.cluster,
                                           namespace.baseInfo.namespaceName,
                                           model).then(
                    function (result) {
                        toastr.success("更新成功, 如需生效请发布");
                        //refresh all namespace items
                        EventManager.emit(EventManager.EventType.REFRESH_NAMESPACE,
                                          {
                                              namespace: namespace
                                          });
                        return true;

                    }, function (result) {
                        toastr.error(AppUtil.errorMsg(result), "更新失败");
                        namespace.commitChangeBtnDisabled = false;
                        return false;
                    }
                );
                namespace.commited = true;
                toggleTextEditStatus(namespace);
            }

            function goToParentAppConfigPage(namespace) {
                $window.location.href = "/config.html?#/appid=" + namespace.parentAppId;
                $window.location.reload();
            }

            function parseModel2Text(namespace) {

                if (namespace.items.length == 0) {
lepdou committed
697
                    namespace.itemCnt = 0;
lepdou committed
698 699 700 701 702 703 704 705 706 707 708 709 710 711 712
                    return "";
                }

                //文件模式
                if (!namespace.isPropertiesFormat) {
                    return parseNotPropertiesText(namespace);
                } else {
                    return parsePropertiesText(namespace);
                }

            }

            function parseNotPropertiesText(namespace) {
                var text = namespace.items[0].item.value;
                var lineNum = text.split("\n").length;
lepdou committed
713
                namespace.itemCnt = lineNum;
lepdou committed
714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736
                return text;
            }

            function parsePropertiesText(namespace) {
                var result = "";
                var itemCnt = 0;
                namespace.items.forEach(function (item) {
                    //deleted key
                    if (!item.item.dataChangeLastModifiedBy) {
                        return;
                    }
                    if (item.item.key) {
                        //use string \n to display as new line
                        var itemValue = item.item.value.replace(/\n/g, "\\n");

                        result +=
                            item.item.key + " = " + itemValue + "\n";
                    } else {
                        result += item.item.comment + "\n";
                    }
                    itemCnt++;
                });

lepdou committed
737
                namespace.itemCnt = itemCnt;
lepdou committed
738 739
                return result;
            }
740

lepdou committed
741
            function toggleItemSearchInput(namespace) {
lepdou committed
742
                namespace.displayControl.showSearchInput = !namespace.displayControl.showSearchInput;
lepdou committed
743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764
            }

            function searchItems(namespace) {
                var searchKey = namespace.searchKey.toLowerCase();
                var items = [];
                namespace.items.forEach(function (item) {
                    var key = item.item.key;
                    if (key && key.toLowerCase().indexOf(searchKey) >= 0) {
                        items.push(item);
                    }
                });
                namespace.viewItems = items;
            }

            //normal release and gray release
            function publish(namespace) {

                if (!namespace.hasReleasePermission) {
                    AppUtil.showModal('#releaseNoPermissionDialog');
                    return;
                } else if (namespace.lockOwner && scope.user == namespace.lockOwner) {
                    //can not publish if config modified by himself
lepdou committed
765 766 767 768
                    EventManager.emit(EventManager.EventType.PUBLISH_DENY, {
                        namespace: namespace,
                        mergeAndPublish: false
                    });
lepdou committed
769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787
                    return;
                }

                if (namespace.isBranch) {
                    namespace.mergeAndPublish = false;
                }

                EventManager.emit(EventManager.EventType.PUBLISH_NAMESPACE,
                                  {
                                      namespace: namespace
                                  });
            }

            function mergeAndPublish(branch) {
                var parentNamespace = branch.parentNamespace;
                if (!parentNamespace.hasReleasePermission) {
                    AppUtil.showModal('#releaseNoPermissionDialog');
                } else if (parentNamespace.itemModifiedCnt > 0) {
                    AppUtil.showModal('#mergeAndReleaseDenyDialog');
lepdou committed
788 789 790 791 792 793
                } else if (branch.lockOwner && scope.user == branch.lockOwner) {
                    EventManager.emit(EventManager.EventType.PUBLISH_DENY, {
                        namespace: branch,
                        mergeAndPublish: true
                    });
                } else {
lepdou committed
794 795 796 797 798 799 800
                    EventManager.emit(EventManager.EventType.MERGE_AND_PUBLISH_NAMESPACE, {branch: branch});
                }
            }

            function rollback(namespace) {
                EventManager.emit(EventManager.EventType.PRE_ROLLBACK_NAMESPACE, {namespace: namespace});
            }
801

lepdou committed
802
            function deleteNamespace(namespace) {
803
                EventManager.emit(EventManager.EventType.PRE_DELETE_NAMESPACE, {namespace: namespace});
lepdou committed
804
            }
lepdou committed
805

806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821
            //theme: https://github.com/ajaxorg/ace-builds/tree/ba3b91e04a5aa559d56ac70964f9054baa0f4caf/src-min
            scope.aceConfig = {
                $blockScrolling: Infinity,
                showPrintMargin: false,
                theme: 'eclipse',
                mode: scope.namespace.format === 'yml' ? 'yaml' : scope.namespace.format,
                onLoad: function (_editor) {
                    _editor.$blockScrolling = Infinity;
                    _editor.setOptions({
                                           fontSize: 13,
                                           minLines: 10,
                                           maxLines: 20
                                       })
                }
            };

822 823
            setTimeout(function () {
                scope.namespace.show = true;
824
            }, 70);
825 826


lepdou committed
827 828 829 830
        }
    }
}