text: "use strict";!function(){angular.module("app").controller("lanFormCtrl",["$rootScope","$scope","$state","$q","$timeout","ngDialog","translate","history","lanHelper","unsavedWarningSharedService","devinfo","$location","funcs",function($rootScope,$scope,$state,$q,$timeout,ngDialog,translate,history,helper,unsavedWarningSharedService,devinfo,$location,funcs){function activate(force,overlayId){function __activate(){return $scope.lanForm.supported=helper.supported.lan(),$scope.lanForm.inx=$state.params.inx?$state.params.inx:getInterfaceIndex(),$scope.lanForm.inx&&lan["interface"].has($scope.lanForm.inx)?(lan["interface"].init(),lan["interface"].change($scope.lanForm.inx),$scope.lan.data=lan.data(),$timeout(function(){$scope.$broadcast("lan.update",$scope.lanForm.inx),$scope.$broadcast("lan.ipversion",$scope.lanForm.ipVersion)},0),$scope.lanForm.isPageLoad=!0,$scope.$emit("pageload",{title:"LAN"}),void(overlayId&&overlay.stop(overlayId))):void goToStart()}force||_.isEmpty(lan.data())?fetch().then(__activate):__activate(),$rootScope.dlinkMobileApp.isUserInMobileApp()&&($rootScope.dlinkMobileApp.hasAdvancedIpAddressChanged?$rootScope.dlinkMobileApp.customLocalDomainCheck&&$rootScope.dlinkMobileApp.isDomainNameUsing().then(function(isDomainName){$scope.noWarnAboutDynamic=isDomainName}):$scope.noWarnAboutDynamic=!0)}function fetch(){var deferred=$q.defer();return pull().then(function(){deferred.resolve()},function(){$state.go("error",{code:"msg_pull_error",message:"msg_error_desc"}),deferred.reject()}),deferred.promise}function pull(){var deferred=$q.defer();return lan.pull(function(error){return error?void deferred.reject("pull"):void deferred.resolve()},supported),deferred.promise}function isLanCoincidesWithWANSubnet(){var range;return!["AccessPoint"].includes(devMode)&&$scope.lanForm.wanIp&&$scope.lanForm.wanMask?(range=funcs.ipv4.subnet.getNetworkRange($scope.lanForm.wanIp,$scope.lanForm.wanMask),funcs.ipv4.subnet.belongNetworkRange(range,lan.data()[1].IPv4.StaticIP[1].Address)):!1}function __showDeviceLossMsg(deviceLoss){var properties=function(options){return ngDialog.open({templateUrl:"dialogs/lan_ip_change/dialog.tpl.html",controller:"LanIpChangeDialogCtrl",className:"wifi_device_loss_dialog",data:options})}(deviceLoss);return properties.closePromise.then(function(){return Promise.resolve("change_ip")}),properties.id}function openDialog(){var ip=lan.data()[1].IPv4.StaticIP[1].Address;return $rootScope.dlinkMobileApp&&$rootScope.dlinkMobileApp.ipAddressChanged(ip),__showDeviceLossMsg({newIp:ip,goToNewIp:$rootScope.goToNewIP})}function openDynamicIpWarnDialog(){var properties=function(){return ngDialog.open({templateUrl:"dialogs/dynamic_ip_warning/dialog.tpl.html",controller:"DynamicIpWarningCtrl"})}({});return properties.closePromise.then(function(data){var host,values=data?data.value:void 0;return values&&(values.goToDomainName?(host=lanDataBeforeChange.IPv4.StaticIP[1].AddnHostname,$rootScope.dlinkMobileApp.isUserInMobileApp()&&$rootScope.dlinkMobileApp.ipAddressChangedAdvanced({disconnect:!1,save:!0,address:host}),save(),$rootScope.goToNewIP(host)):values["continue"]&&($scope.noWarnAboutDynamic=!0,apply())),Promise.resolve("dynamic_ip")}),properties.id}function apply(){$scope.form.submitted=!0,$timeout(__apply)}function __apply(){function applyHandler(deviceLoss){return new Promise(function(resolve,reject){save().then(function(flags){function checkRootScopeOverlay(){var ip,newUrl;helper.constraints.constants.DSYSINIT&&"undefined"!=typeof $rootScope.overlay.progress.start(helper.constraints.constants.REBOOT_TIME)&&(ip=lan.data()[1].IPv4.StaticIP[1].Address,newUrl=$location.protocol()+"://"+ip+":"+$location.port(),$rootScope.overlay.progress.start(helper.constraints.constants.REBOOT_TIME).then(function(){window.location.href=newUrl}),devinfo.__setting.url=newUrl+"/devinfo",$rootScope.waitForReboot().then(function(){$rootScope.overlay.progress.stop(),window.location.href=newUrl}))}return flags=flags||{},$scope.form.submitted=!1,flags.saveAndReboot&&confirm(translate("msg_warn_save_and_reboot"))?(checkRootScopeOverlay(),overlay.stop(overlayId),$rootScope.$emit("unsavedStop",!0),device.system.reboot(!0),resolve("change_ip")):resolve()},function(){deviceLoss.loss&&resolve("change_ip"),reject("errors")}),deviceLoss.loss&&$rootScope.dlinkMobileApp&&$timeout(function(){resolve("change_ip")},2500)})}if(isValid()||unsavedWarningSharedService.setBackup(!1),isValid()&&isChange()){if(!$scope.noWarnAboutDynamic&&isDynamicAddress()&&!isLocalDomainUsedForLoad())return void openDynamicIpWarnDialog();var overlayId=overlay.start(),deviceLoss=lan.checkDeviceLoss($location.host()),applyFunc=function(){applyHandler(deviceLoss).then(function(res){res&&"change_ip"==res||($scope.lanForm.isMultiInterfaces?(overlay.stop(overlayId),goToStart()):activate(!0,overlayId))},function(){deviceLoss.loss||(overlay.stop(overlayId),$state.go("error",{code:"msg_push_error",message:"msg_error_desc"}))})};return deviceLoss.loss&&helper.constraints.constants.DSYSINIT?($scope.lan_ip_dialog_cb=applyFunc,void($scope.lan_ip_dialog_id=openDialog())):applyFunc()}}function checkDhcpServer(){var currentLan=lan.data()[1];currentLan.IPv4&&(funcs.is.ipv4(currentLan.IPv4.DHCP.Server.MinAddress)||(currentLan.IPv4.DHCP.Server.MinAddress=lanDataBeforeChange.IPv4.DHCP.Server.MinAddress),funcs.is.ipv4(currentLan.IPv4.DHCP.Server.MaxAddress)||(currentLan.IPv4.DHCP.Server.MaxAddress=lanDataBeforeChange.IPv4.DHCP.Server.MaxAddress)),currentLan.IPv6&&(funcs.is.ipv6(currentLan.IPv6.DHCP.Server.MinAddress)||currentLan.IPv6.DHCP.Server.DHCPAddressShort||(currentLan.IPv6.DHCP.Server.MinAddress=lanDataBeforeChange.IPv6.DHCP.Server.MinAddress),funcs.is.ipv6(currentLan.IPv6.DHCP.Server.MaxAddress)||currentLan.IPv6.DHCP.Server.DHCPAddressShort||(currentLan.IPv6.DHCP.Server.MaxAddress=lanDataBeforeChange.IPv6.DHCP.Server.MaxAddress))}function save(){function success(flags){deferred.resolve(flags)}function error(){deferred.reject()}$scope.lanForm.dhcpPorts&&$scope.lanForm.dhcpPorts.apply();var deferred=$q.defer(),version=$scope.lanForm.ipVersion;return lan.push(function(err,flags){return err?void error():void success(flags)},version,supported),deferred.promise}function remove(inx){function success(flags){deferred.resolve(flags)}function error(){deferred.reject()}var deferred=$q.defer();return lan.remove(inx,function(err,flags){return err?void error():void success(flags)}),deferred.promise}function isValid(){return $scope.form.$valid}function isChange(){return lan.isChange()?!0:$scope.lanForm.dhcpPorts&&$scope.lanForm.dhcpPorts.isChange()?!0:!1}function isDynamicAddress(){return"Dynamic"===$scope.lan.data[1].IPv4.AddressingMode}function isLocalDomainUsedForLoad(){return $location.$$host===$scope.lan.data[1].IPv4.StaticIP[1].AddnHostname}function isNotification(){var notices=lan["interface"].getNotices();return notices.length}function callNotification(){function callNotice(notices,inx){function end(inx){notices.length==inx+1?success():callNotice(notices,inx+1).then(success,error)}function success(){deferred.resolve()}function error(msg){deffered.reject(msg)}var params,deferred=$q.defer(),elem=notices[inx];switch(elem.name){case"dhcp_is_incorrect_pool":params=elem.notice.params(),params.ranges.length?ngDialog.open({template:"dialogs/dhcp_server_address_pool/dialog.tpl.html",className:"IPv6"==$scope.lanForm.ipVersion?"dhcp_pool_v6":"",controller:"DHCPServerAddressPoolDialogCtrl",scope:$scope,data:params}).closePromise.then(function(data){data.value&&elem.notice.correct(data.value),end(inx)}):end(inx)}return deferred.promise}var notices=lan["interface"].getNotices();return callNotice(notices,0).then(function(){var deferred=$q.defer();return $timeout(function(){deferred.resolve()},0),deferred.promise})}function getInterfaceIndex(){return"add"==$scope.lanForm.action?_.keys(lan["interface"].listNew())[0]:"1"}function goToStart(){$state.go("network.lan")}var device=$scope.device,lan=$scope.device.lan,supported=helper.supported.lan(),overlay=$rootScope.overlay.circular,lanDataBeforeChange=funcs.deepClone(lan.data()[1]),devMode=lan.deviceMode();$scope.lanForm={supported:supported,has:function(version){return $scope.lanForm.supported[version]},isShow:function(version){return $scope.lanForm.ipVersion==version},isChange:isChange,isValid:isValid,isNotification:isNotification,isNeedRemove:function(){if(!supported.SUPPORT_MULTI_LAN)return!1;if("edit"!=$scope.lanForm.action)return!1;var allListLen=_.size(lan["interface"].list()),newListLen=_.size(lan["interface"].listNew());return allListLen-newListLen>1},callNotification:callNotification,prepareApply:function(){return isLanCoincidesWithWANSubnet()?void alert(translate("lan_error_is_lan_address_coincides_with_wan_subnet")):(checkDhcpServer(),isNotification()?void callNotification().then(apply):void apply())},removeInterface:function(){var inx=$scope.lanForm.inx,overlayId=overlay.start();remove(inx).then(function(flags){return flags=flags||{},$rootScope.$emit("unsavedStop",!0),history.setCleanLastHistory(!0),flags.saveAndReboot&&confirm(translate("msg_warn_save_and_reboot"))?(overlay.stop(overlayId),void device.system.reboot(!0)):void($scope.lanForm.isMultiInterfaces?(overlay.stop(overlayId),goToStart()):activate(!0,overlayId))})}},$scope.lanForm.isMultiInterfaces=/network.lanInterface/.test($state.current.name),$scope.lanForm.action="network.lanInterface.add"==$state.current.name?"add":"edit",$scope.lanForm.ipVersion=$state.params.ipversion?$state.params.ipversion:"IPv4",$scope.lanForm.isPageLoad=!1,$scope.lanForm.wanIp="",$scope.lanForm.wanMask="",$scope.$on("$destroy",function(){lan.clean(),lan["interface"].clean()}),$scope.$on("lan.interface.change",function($event,value){$scope.lanForm.inx=value,lan["interface"].change(value,!0),$scope.$broadcast("lan.interface.update",value)}),$scope.$on("lan.interface.ip.change",function($event,value){$scope.lanForm.ipVersion=value,$scope.$broadcast("lan.ipversion",value)}),activate(),devinfo.once("net").then(function(data){data&&data.ipv4gw&&($scope.lanForm.wanIp=data.ipv4gw.ip,$scope.lanForm.wanMask=data.ipv4gw.mask)}),$rootScope.$on("ngDialog.opened",function(e,$dialog){$scope.lan_ip_dialog_id===$dialog.attr("id")&&($rootScope.deviceMonitorConfig.definitelyLostThreshold=-1,$scope.lan_ip_dialog_cb())})}])}();