示例#1
0
文件: login.js 项目: vecna/mail-html5
    var LoginCtrl = function($scope, $location) {
        // global state... inherited to all child scopes
        $scope.$root.state = {};
        // attach global error handler
        errorUtil.attachHandler($scope);

        // check for app update
        appController.checkForUpdate();

        // start main application controller
        appController.start({
            onError: $scope.onError
        }, function(err) {
            if (err) {
                $scope.onError(err);
                return;
            }

            initializeUser();
        });

        function initializeUser() {
            // get OAuth token from chrome
            appController.getEmailAddress(function(err, emailAddress) {
                if (err) {
                    $scope.onError(err);
                    return;
                }

                // initiate controller by creating email dao
                appController.init({
                    emailAddress: emailAddress
                }, function(err, availableKeys) {
                    if (err) {
                        $scope.onError(err);
                        return;
                    }

                    redirect(availableKeys);
                });
            });
        }

        function redirect(availableKeys) {
            // redirect if needed
            if (typeof availableKeys === 'undefined') {
                // no public key available, start onboarding process
                $location.path('/login-initial');
            } else if (!availableKeys.privateKey) {
                // no private key, import key
                $location.path('/login-new-device');
            } else {
                // public and private key available, just login 
                $location.path('/login-existing');
            }
            $scope.$apply();
        }
    };
示例#2
0
    var AddAccountCtrl = function($scope, $location) {
        // global state... inherited to all child scopes
        $scope.$root.state = {};
        // attach global error handler
        errorUtil.attachHandler($scope);

        $scope.connectToGoogle = function() {
            appController.fetchOAuthToken(function(err) {
                if (err) {
                    $scope.onError(err);
                    return;
                }

                redirect();
            });
        };

        function redirect() {
            $location.path('/login');
            $scope.$apply();
        }
    };
示例#3
0
    var NavigationCtrl = function($scope) {
        // global state... inherited to all child scopes
        $scope.$root.state = {};
        // attach global error handler
        errorUtil.attachHandler($scope);

        // app controller is initialized
        appController._initialized = true;

        emailDao = appController._emailDao;
        outboxBo = appController._outboxBo;

        //
        // scope functions
        //

        $scope.state.nav = {
            open: false,
            toggle: function(to) {
                this.open = to;
            }
        };

        $scope.openFolder = function(folder) {
            $scope.state.nav.currentFolder = folder;
            $scope.state.nav.toggle(false);
        };

        $scope.onOutboxUpdate = function(err, count) {
            var outbox, mail;

            if (err) {
                $scope.onError(err);
                return;
            }

            outbox = _.findWhere($scope.account.folders, {
                type: 'Outbox'
            });
            // update the outbox mail count
            outbox.count = count;

            // if we're NOT viewing the outbox or we're not looking at any mail now, we're done
            if ($scope.state.nav.currentFolder !== outbox || !$scope.state.mailList.selected) {
                $scope.$apply();
                return;
            }

            // so we're currently in the outbox.
            // if the currently selected mail is still among the pending mails, re-select it, since the object has changed.
            // however, if the mail is NOT in the outbox anymore, select another pending mail
            //
            // this is a workaround due to the fact that the outbox loads pending messages from the indexedDB, 
            // where object identity is broken when you read an object twice, which happens upon the next retry
            // to send the pending messages...

            mail = _.findWhere(outbox.messages, {
                id: $scope.state.mailList.selected.id
            });

            if (mail) {
                // select the 'new old' mail
                $scope.state.mailList.selected = mail;
            } else {
                if (outbox.messages.length) {
                    // there are more mails to show, select another one in the list
                    $scope.state.mailList.selected = outbox.messages[0];
                } else {
                    // no mails to show, don't select anything...
                    $scope.state.mailList.selected = undefined;
                }
            }

            $scope.$apply();
        };

        //
        // Start
        //

        // init folders
        initFolders();
        // select inbox as the current folder on init
        $scope.openFolder($scope.account.folders[0]);

        //
        // helper functions
        //

        function initFolders() {
            if (window.chrome && chrome.identity) {
                // get pointer to account/folder/message tree on root scope
                $scope.$root.account = emailDao._account;

                // set notificatio handler for sent messages
                outboxBo.onSent = sentNotification;
                // start checking outbox periodically
                outboxBo.startChecking($scope.onOutboxUpdate);
                // make function available globally for write controller
                $scope.emptyOutbox = outboxBo._processOutbox.bind(outboxBo);
                return;
            }

            // attach dummy folders for development
            $scope.$root.account = {};
            $scope.account.folders = [{
                type: 'Inbox',
                count: 2,
                path: 'INBOX'
            }, {
                type: 'Sent',
                count: 0,
                path: 'SENT'
            }, {
                type: 'Outbox',
                count: 0,
                path: 'OUTBOX'
            }, {
                type: 'Drafts',
                count: 0,
                path: 'DRAFTS'
            }, {
                type: 'Trash',
                count: 0,
                path: 'TRASH'
            }];
        }

        function sentNotification(email) {
            chrome.notifications.create('o' + email.id, {
                type: 'basic',
                title: 'Message sent',
                message: email.subject.replace(str.subjectPrefix, ''),
                iconUrl: chrome.runtime.getURL(cfg.iconPath)
            }, function() {});
        }
    };