Skip to content
This repository has been archived by the owner on Jan 16, 2018. It is now read-only.

Use cozy-client-js instead of cozydb #309

Open
wants to merge 8 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ results
node_modules
npm-debug.log

client/bower_components
client/public
client/app/locales/*.json
!client/app/locales/en.json
bower_components
public
app/locales/*.json
app/locales/en.json

picture.jpg
File renamed without changes.
20 changes: 13 additions & 7 deletions client/app/application.coffee → app/application.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,13 @@ class Application extends Mn.Application
@model = new AppViewModel null, model: config

@contacts = new ContactsCollection()
@filtered = new FilteredCollection()
@tags = new TagsCollection(@contacts)
@filtered = new FilteredCollection {
contacts: @contacts
model: @model
channel: @channel
}

# As long as Tags require Contacts to be loaded (see Tags.refs), we
# trigger all TagsCollection fetch after syncing ContactsCollection.
@listenToOnce @contacts, 'sync': -> @tags.underlying.fetch reset: true
@tags = new TagsCollection @contacts

@layout = new AppLayout model: @model
@router = new Router()
Expand All @@ -49,10 +50,15 @@ class Application extends Mn.Application

Object.freeze @ if typeof Object.freeze is 'function'


# render components when app starts
onStart: ->
@layout.render()
@contacts.fetch reset: true

@contacts.fetch {reset: true}, (models, collection) =>
# As long as Tags require Contacts to be loaded (see Tags.refs), we
# trigger all TagsCollection fetch after syncing ContactsCollection.
@tags.underlying.fetch {reset: true}

# prohibit pushState because URIs mapped from cozy-home rely on
# fragment
Expand All @@ -62,7 +68,7 @@ class Application extends Mn.Application
search: (pattern, string) ->
filter = @model.get 'filter'
input = "`#{pattern}:#{string}`"
_pattern = require('config').search.pattern pattern
_pattern = require('const-config').search.pattern pattern
prev = filter?.match _pattern

if string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ Contact = require 'models/contact'
module.exports = class Contacts extends Backbone.Collection

model: Contact
url: 'contacts'

sortDisabled: false

Expand All @@ -20,6 +19,71 @@ module.exports = class Contacts extends Backbone.Collection
@listenToOnce @, 'sync', -> @contactListener.watch @


fetch: (options={}, success = -> ) ->
# This event is listened by layout
# to display applications views
@trigger 'sync', options

@reset() if options.reset

# FIXME: remove this fakeData
# when request will work
n = 'last name;first name;middle name;préfix;suffixe'
datapoints = []
datapoints.push {
id: "06",
name: "email",
value: "[email protected]",
type: "work"
}
datapoints.push {
id: "05",
name: "email",
value: "[email protected]",
type: "work"
}
datapoints.push {
id: "04",
name: "tel",
value: "0619384462",
type: "main"
}
datapoints.push {
id: "03",
name: "tel",
value: "0619384462",
type: "portable"
}
datapoints.push {
id: "02",
name: "tel",
value: "n0619384462",
type: "work"
}


result = []
result.push { _id: '01', n, datapoints }
result.push { _id: '02', n, datapoints }
result.push { _id: '03', n, datapoints }
result.push { _id: '04', n, datapoints }
result.push { _id: '05', n, datapoints }
#
@add result
success result, @

# cozy.init { isV2: true }
# cozy.defineIndex 'io.cozy.contacts', ['id']
# .then (index) =>
# cozy.query index, { selector: id: {"$gte": " "} }
# .then (result=[]) =>
# @add result
# success result, @
# , (err) =>
# success null, err



# Turns a vcard text into a list of contacts. Then it saves every contacts
# in the data system. Finally it reloads the contact list.
importFromVCF: (vcard) ->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
ContactViewModel = require 'views/models/contact'

{indexes, search} = require 'config'
{indexes, search} = require 'const-config'

tagRegExp = search.pattern 'tag'
app = null
scores = new Map()


Expand All @@ -29,25 +28,27 @@ module.exports = class FilteredCollection

query: null

constructor: ->
app = require 'application'

constructor: (options={}) ->
{ model, channel, contacts } = options
@app = { model, channel, contacts }

@models = []
@indexes = new Map Array::map.call indexes, (char) ->
vmodels = []
vmodels.channel = Backbone.Radio.channel "idx.#{char}"
[char, vmodels]

@listenTo app.model,
@listenTo @app.model,
'change:sort': @reset
'change:scored': (nil, scored) ->
@comparator = if scored
(a, b) -> (scores.get(b?.model) or 0) - scores.get(a.model)
else 'model.attributes.sortedName'

@listenTo app.channel, 'filter:text': @setQuery
@listenTo @app.channel, 'filter:text': @setQuery

@listenTo app.contacts,
@listenTo @app.contacts,
'reset': @reset
'add': @add
'remove': @remove
Expand All @@ -68,7 +69,7 @@ module.exports = class FilteredCollection
@query = query

if query
res = app.contacts.filter filter @query
res = @contacts.filter filter @query

# Reduce to get max score and get a median limit, then excludes all
# results $lt it.
Expand Down Expand Up @@ -102,7 +103,7 @@ module.exports = class FilteredCollection

get: (opts = {}) ->
base = if opts.index then @indexes.get(opts.index) else @models
tag = _.last app.model.get('filter')?.match tagRegExp
tag = _.last @app.model.get('filter')?.match tagRegExp

if tag and opts.tagged
base.filter (vmodel) -> _.includes vmodel.get('tags'), tag
Expand All @@ -113,9 +114,9 @@ module.exports = class FilteredCollection
# Internal models collection handlers
# ###################################
reset: ->
@models = app.contacts
@models = @app.contacts
.filter filter @query
.map (model) -> new ContactViewModel {}, model: model
.map (model) -> new ContactViewModel @app, { model }

@resetIndexes()

Expand All @@ -126,14 +127,15 @@ module.exports = class FilteredCollection
models = if _.isArray models then _.clone models else [models]

models.forEach (model) =>
return unless filter(@query, model)

vmodel = new ContactViewModel {}, model: model
return unless filter @query, model

vmodel = new ContactViewModel @app, { model }
idx = _.sortedIndex @models, vmodel, @comparator

@models.splice idx, 0, vmodel

@addToIndex vmodel

@trigger 'add', vmodel, @, {add:true, index: idx}


Expand Down Expand Up @@ -162,9 +164,9 @@ module.exports = class FilteredCollection

addToIndex: (vmodel, opts = {}) ->
set = @indexes.get vmodel.getIndexKey()

return vmodel if _.includes set, vmodel

# add model to collection
idx = _.sortedIndex set, vmodel, @comparator
set.splice idx, 0, vmodel

Expand Down
28 changes: 25 additions & 3 deletions client/app/collections/tags.coffee → app/collections/tags.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,22 @@ class Tags extends Backbone.Collection

model: require 'models/tag'

url: 'tags'


initialize: ->
@listenToOnce @, 'sync', -> (new TagsListener()).watch @


# We convert all tags to lowercase and remove duplicates at pers time
parse: (tags) ->
_.uniq tags, (tag) -> tag.name.toLowerCase()


fetch: (success) ->
# TODO: add fake data
# TODO: add cozy-client-js code
console.log "(fetch) TAGS"


# Export TagsCollection is the Contacts aware projection. All tags Collection
# can be accessed at `underlying` property.
module.exports = class FilteredTags extends Filtered
Expand All @@ -49,12 +54,29 @@ module.exports = class FilteredTags extends Filtered
# concact tags.
updateRefs: ->
@refs = @contacts.chain()
.map (model) -> model.get 'tags'
.map (model) -> model.get('tags') or []
.flatten()
.uniq()
.value()



create: (data={}, options={}) ->
callback = options.success or null

cozy.init { isV2: true }
cozy.create 'io.cozy.tags', data
.then (resp) =>
# Add model to collection
# if data is correct
@add resp, options

callback resp, @ if callback

, () =>
console.log '(save.error) TAG', arguments


update: ->
@updateRefs()
super
File renamed without changes.
5 changes: 3 additions & 2 deletions client/app/initialize.coffee → app/initialize.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ Sets the browser environment to prepare it to launch the app, and then require
the application.
###


###
Logging

Expand Down Expand Up @@ -56,12 +55,14 @@ Starts
Trigger locale initilization and starts application singleton.
###
application = require './application'


# Temporary use a global variable to store the `t` helpers, waiting for
# Marionette allow to register global helpers.
# see https://github.com/marionettejs/backbone.marionette/issues/2164
window.t = require 'lib/i18n'

ColorHash.addScheme 'cozy', require('config').colorSet
ColorHash.addScheme 'cozy', require('const-config').colorSet
Mn.Behaviors.behaviorsLookup = require 'lib/behaviors'

document.addEventListener 'DOMContentLoaded', ->
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 4 additions & 0 deletions app/models/config.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = class Config extends Backbone.Model

fetch: (success) ->
console.log "(fetch) CONFIG"
Loading