feat(babel6): Convert account.coffee to account.es6

This commit is contained in:
Evan Morikawa 2016-05-04 16:23:19 -07:00
parent 5251819942
commit 409704fe8f
9 changed files with 197 additions and 170 deletions

View file

@ -1,6 +1,6 @@
Contact = require "../../src/flux/models/contact"
AccountStore = require "../../src/flux/stores/account-store"
Account = require "../../src/flux/models/account"
Account = require("../../src/flux/models/account").default
contact_1 =
name: "Evan Morikawa"

View file

@ -2,7 +2,7 @@ ModelQuery = require '../../src/flux/models/query'
{Matcher} = require '../../src/flux/attributes'
Message = require '../../src/flux/models/message'
Thread = require('../../src/flux/models/thread').default
Account = require '../../src/flux/models/account'
Account = require('../../src/flux/models/account').default
describe "ModelQuery", ->
beforeEach ->

View file

@ -306,7 +306,7 @@ describe "NylasAPI", ->
"folder": require('../src/flux/models/folder')
"thread": require('../src/flux/models/thread').default
"draft": require('../src/flux/models/message')
"account": require('../src/flux/models/account')
"account": require('../src/flux/models/account').default
"message": require('../src/flux/models/message')
"contact": require('../src/flux/models/contact')
"calendar": require('../src/flux/models/calendar')

View file

@ -1,7 +1,7 @@
_ = require 'underscore'
keytar = require 'keytar'
AccountStore = require '../../src/flux/stores/account-store'
Account = require '../../src/flux/models/account'
Account = require('../../src/flux/models/account').default
Actions = require '../../src/flux/actions'

View file

@ -1,161 +0,0 @@
ModelWithMetadata = require('./model-with-metadata').default
Attributes = require '../attributes'
_ = require 'underscore'
CategoryStore = null
Contact = null
###
Public: The Account model represents a Account served by the Nylas Platform API.
Every object on the Nylas platform exists within a Account, which typically represents
an email account.
For more information about Accounts on the Nylas Platform, read the
[Account API Documentation](https://nylas.com/docs/api#Account)
## Attributes
`name`: {AttributeString} The name of the Account.
`provider`: {AttributeString} The Account's mail provider (ie: `gmail`)
`emailAddress`: {AttributeString} The Account's email address
(ie: `ben@nylas.com`). Queryable.
`organizationUnit`: {AttributeString} Either "label" or "folder".
Depending on the provider, the account may be organized by folders or
labels.
This class also inherits attributes from {Model}
Section: Models
###
class Account extends ModelWithMetadata
@SYNC_STATE_RUNNING = "running"
@SYNC_STATE_STOPPED = "stopped"
@SYNC_STATE_AUTH_FAILED = "invalid"
@SYNC_STATE_ERROR = "sync_error"
@attributes: _.extend {}, ModelWithMetadata.attributes,
'name': Attributes.String
modelKey: 'name'
'provider': Attributes.String
modelKey: 'provider'
'emailAddress': Attributes.String
queryable: true
modelKey: 'emailAddress'
jsonKey: 'email_address'
'organizationUnit': Attributes.String
modelKey: 'organizationUnit'
jsonKey: 'organization_unit'
'label': Attributes.String
queryable: false
modelKey: 'label'
'aliases': Attributes.Object
queryable: false
modelKey: 'aliases'
'defaultAlias': Attributes.Object
queryable: false
modelKey: 'defaultAlias'
jsonKey: 'default_alias'
'syncState': Attributes.String
queryable: false
modelKey: 'syncState'
jsonKey: 'sync_state'
constructor: ->
super
@aliases ||= []
@label ||= @emailAddress
@syncState ||= "running"
fromJSON: (json) ->
json["label"] ||= json[@constructor.attributes['emailAddress'].jsonKey]
super
# Returns a {Contact} model that represents the current user.
me: ->
Contact ?= require './contact'
return new Contact
accountId: @id
name: @name
email: @emailAddress
meUsingAlias: (alias) ->
Contact ?= require './contact'
return @me() unless alias
return Contact.fromString(alias, accountId: @id)
defaultMe: ->
if @defaultAlias
return @meUsingAlias(@defaultAlias)
else
return @me()
usesLabels: ->
@organizationUnit is "label"
usesFolders: ->
@organizationUnit is "folder"
categoryLabel: ->
if @usesFolders()
'Folders'
else if @usesLabels()
'Labels'
else
'Unknown'
categoryCollection: ->
"#{@organizationUnit}s"
categoryIcon: ->
if @usesFolders()
'folder.png'
else if @usesLabels()
'tag.png'
else
'folder.png'
# Public: Returns the localized, properly capitalized provider name,
# like Gmail, Exchange, or Outlook 365
displayProvider: ->
if @provider is 'eas'
return 'Exchange'
else if @provider is 'gmail'
return 'Gmail'
else
return @provider
canArchiveThreads: ->
CategoryStore ?= require '../stores/category-store'
CategoryStore.getArchiveCategory(@)?
canTrashThreads: ->
CategoryStore ?= require '../stores/category-store'
CategoryStore.getTrashCategory(@)?
defaultFinishedCategory: ->
CategoryStore ?= require '../stores/category-store'
preferDelete = NylasEnv.config.get('core.reading.backspaceDelete')
archiveCategory = CategoryStore.getArchiveCategory(@)
trashCategory = CategoryStore.getTrashCategory(@)
if preferDelete or not archiveCategory
trashCategory
else
archiveCategory
hasSyncStateError: ->
# TODO: ignoring "stopped" until it's no longer overloaded on API
return @syncState != Account.SYNC_STATE_RUNNING &&
@syncState != Account.SYNC_STATE_STOPPED
module.exports = Account

188
src/flux/models/account.es6 Normal file
View file

@ -0,0 +1,188 @@
import Attributes from '../attributes'
import ModelWithMetadata from './model-with-metadata'
let CategoryStore = null
let Contact = null
/**
* Public: The Account model represents a Account served by the Nylas Platform API.
* Every object on the Nylas platform exists within a Account, which typically represents
* an email account.
*
* For more information about Accounts on the Nylas Platform, read the
* [Account API Documentation](https://nylas.com/docs/api#Account)
*
* ## Attributes
*
* `name`: {AttributeString} The name of the Account.
*
* `provider`: {AttributeString} The Account's mail provider (ie: `gmail`)
*
* `emailAddress`: {AttributeString} The Account's email address
* (ie: `ben@nylas.com`). Queryable.
*
* `organizationUnit`: {AttributeString} Either "label" or "folder".
* Depending on the provider, the account may be organized by folders or
* labels.
*
* This class also inherits attributes from {Model}
*
* Section: Models
*/
export default class Account extends ModelWithMetadata {
static SYNC_STATE_RUNNING = "running"
static SYNC_STATE_STOPPED = "stopped"
static SYNC_STATE_AUTH_FAILED = "invalid"
static SYNC_STATE_ERROR = "sync_error"
static attributes = Object.assign({}, ModelWithMetadata.attributes, {
name: Attributes.String({
modelKey: 'name',
}),
provider: Attributes.String({
modelKey: 'provider',
}),
emailAddress: Attributes.String({
queryable: true,
modelKey: 'emailAddress',
jsonKey: 'email_address',
}),
organizationUnit: Attributes.String({
modelKey: 'organizationUnit',
jsonKey: 'organization_unit',
}),
label: Attributes.String({
queryable: false,
modelKey: 'label',
}),
aliases: Attributes.Object({
queryable: false,
modelKey: 'aliases',
}),
defaultAlias: Attributes.Object({
queryable: false,
modelKey: 'defaultAlias',
jsonKey: 'default_alias',
}),
syncState: Attributes.String({
queryable: false,
modelKey: 'syncState',
jsonKey: 'sync_state',
}),
})
constructor() {
super()
this.aliases = this.aliases || [];
this.label = this.label || this.emailAddress;
this.syncState = this.syncState || "running";
}
fromJSON(inJSON) {
const json = inJSON;
json.label = json.label || json[Account.attributes.emailAddress.jsonKey]
return super.fromJSON(json)
}
// Returns a {Contact} model that represents the current user.
me() {
Contact = Contact || require('./contact')
return new Contact({
accountId: this.id,
name: this.name,
email: this.emailAddress,
})
}
meUsingAlias(alias) {
Contact = Contact || require('./contact')
if (!alias) {
return this.me()
}
return Contact.fromString(alias, {accountId: this.id})
}
defaultMe() {
if (this.defaultAlias) {
return this.meUsingAlias(this.defaultAlias)
}
return this.me()
}
usesLabels() {
return this.organizationUnit === "label"
}
usesFolders() {
return this.organizationUnit === "folder"
}
categoryLabel() {
if (this.usesFolders()) {
return 'Folders'
} else if (this.usesLabels()) {
return 'Labels'
}
return 'Unknown'
}
categoryCollection() {
return "#{this.organizationUnit}s"
}
categoryIcon() {
if (this.usesFolders()) {
return 'folder.png'
} else if (this.usesLabels()) {
return 'tag.png'
}
return 'folder.png'
}
// Public: Returns the localized, properly capitalized provider name,
// like Gmail, Exchange, or Outlook 365
displayProvider() {
if (this.provider === 'eas') {
return 'Exchange'
} else if (this.provider === 'gmail') {
return 'Gmail'
}
return this.provider
}
canArchiveThreads() {
CategoryStore = CategoryStore || require('../stores/category-store')
return CategoryStore.getArchiveCategory(this)
}
canTrashThreads() {
CategoryStore = CategoryStore || require('../stores/category-store')
return CategoryStore.getTrashCategory(this)
}
defaultFinishedCategory() {
CategoryStore = CategoryStore || require('../stores/category-store')
const preferDelete = NylasEnv.config.get('core.reading.backspaceDelete')
const archiveCategory = CategoryStore.getArchiveCategory(this)
const trashCategory = CategoryStore.getTrashCategory(this)
if (preferDelete || !archiveCategory) {
return trashCategory
}
return archiveCategory
}
hasSyncStateError() {
// TODO: ignoring "stopped" until it's no longer overloaded on API
return this.syncState !== Account.SYNC_STATE_RUNNING &&
this.syncState !== Account.SYNC_STATE_STOPPED
}
}

View file

@ -3,7 +3,7 @@ _ = require 'underscore'
request = require 'request'
NylasLongConnection = require('./nylas-long-connection').default
Utils = require './models/utils'
Account = require './models/account'
Account = require('./models/account').default
Message = require './models/message'
Actions = require './actions'
{APIError} = require './errors'
@ -351,7 +351,7 @@ class NylasAPI
"folder": require('./models/folder')
"thread": require('./models/thread').default
"draft": require('./models/message')
"account": require('./models/account')
"account": require('./models/account').default
"message": require('./models/message')
"contact": require('./models/contact')
"calendar": require('./models/calendar')

View file

@ -1,7 +1,7 @@
_ = require 'underscore'
NylasStore = require 'nylas-store'
Actions = require '../actions'
Account = require '../models/account'
Account = require('../models/account').default
Utils = require '../models/utils'
DatabaseStore = require './database-store'
keytar = require 'keytar'
@ -237,7 +237,7 @@ class AccountStore extends NylasStore
fs = require 'fs-plus'
path = require 'path'
Message = require '../models/message'
Account = require '../models/account'
Account = require('../models/account').default
Thread = require('../models/thread').default
Label = require '../models/label'

View file

@ -3,7 +3,7 @@ Rx = require 'rx-lite'
NylasStore = require 'nylas-store'
AccountStore = require './account-store'
NylasSyncStatusStore = require './nylas-sync-status-store'
Account = require '../models/account'
Account = require('../models/account').default
{StandardCategoryNames} = require '../models/category'
{Categories} = require 'nylas-observables'