Last active
August 29, 2015 14:05
-
-
Save bolasblack/2438104bc621e08915f8 to your computer and use it in GitHub Desktop.
未提交番茄的灾难恢复功能的部分代码
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
LAST_POMO_START_TIME_STORAGE_KEY = "#{appEdition}_last_pomo_start_time" | |
getConfirmMessage = -> | |
alerts = | |
processing: $i18next("web:pomo.confirm_leaving_processing") | |
finished: $i18next("web:pomo.confirm_leaving_submit") | |
alerts[$scope.pomoTimer.status] | |
$window.$($window).on 'beforeunload', (event) -> | |
# 没法判断用户是否在之后的对话框里点击了确定或者取消,只能一股脑的直接删掉 | |
# 如果用户确认了,故意关闭了网页,那么 LAST_POMO_START_TIME_STORAGE_KEY 自然也就 | |
# 被删除了,如果用户取消了,那么下面的计时器依旧会执行,LAST_POMO_START_TIME_STORAGE_KEY | |
# 会在1秒之内被再次写入,不会出现 LAST_POMO_START_TIME_STORAGE_KEY 消失的情况 | |
storage().remove LAST_POMO_START_TIME_STORAGE_KEY | |
getConfirmMessage() | |
$scope.$on '$stateChangeStart', (event, toState, toParams, fromState, fromParams) -> | |
# 如果没有相应的提示信息的话,那就不管 | |
return unless confirmContent = getConfirmMessage() | |
# 如果不是个人版和团队版之间的切换,那也不管 | |
return if fromState.name.split('.')[0] is toState.name.split('.')[0] | |
# 向用户确认,如果用户确认,那就删除缓存的番茄信息,并且跳出 | |
# | |
# 其他所有要使用对话框的地方都要使用 $dialog 提供的方法,这样以后要自定义对话框时就 | |
# 不用查找替换所有用到 window.alert 这类方法的代码,直接重写 $dialog 就好了。这里是 | |
# 由于 event.preventDefault() 这个函数必须同步地调用,不能在下一个 Event Loop 里调 | |
# 用,所以才使用了阻塞的 window.confirm 函数 | |
if $window.confirm confirmContent | |
storage().remove LAST_POMO_START_TIME_STORAGE_KEY | |
return | |
event.preventDefault() | |
# 用来不停的写入 LAST_POMO_START_TIME_STORAGE_KEY | |
# | |
# 由于在询问是否真的要取消番茄时会删除 LAST_POMO_START_TIME_STORAGE_KEY ,所以必须在 | |
# 每一秒时都写入这个值,否则一次弹出确认框后,接下来如果应用崩溃的话是无法灾难恢复的 | |
lastPomoStartTimeWriteTimer = $window.setInterval -> | |
method = if $scope.pomoTimer.isWorking() then 'set' else 'remove' | |
storage()[method] LAST_POMO_START_TIME_STORAGE_KEY, $scope.pomoTimer.start_time | |
, 1000 | |
lastPomoStartTime = $moment storage().get LAST_POMO_START_TIME_STORAGE_KEY | |
storage().remove LAST_POMO_START_TIME_STORAGE_KEY | |
if lastPomoStartTime.isValid() | |
interval = $moment().diff lastPomoStartTime, 'minute', true | |
message = "web:pomo.confirm_if_need_#{if interval >= 25 then 'save' else 'recover'}_last_pomo" | |
$dialog.confirm(message: $i18next message).then -> | |
$scope.pomoTimer.start startedAt: lastPomoStartTime.toDate() | |
# 用来强制更新界面上的番茄时间 | |
digestTimer = $window.setInterval $scope.$safeDigest.bind($scope), 1000 | |
$scope.$on '$destroy', -> | |
$document[0].title = originalTitle | |
Piecon.reset() | |
$scope.pomoTimer.reset() | |
$window.$($window).off 'beforeunload', getConfirmMessage | |
$window.clearInterval digestTimer | |
$window.clearInterval lastPomoStartTimeWriteTimer |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment