Anton Shestakov <av6@dwimlabs.net>, Wed, 01 Nov 2017 00:50:57 +0800
Makefile: use file targets more, results in incremental building
Less work to do when only some files get changed, but more work when building
from scratch. But, multiple compilers can run in parallel.
coffee/contacts.coffee
Permissions: -rw-r--r--
class Tram.Contact extends Backbone.Model @on 'add change:type change:presence change:callstate change:features', -> if @get('type') is 'self' then return @unset('d/actions') if @get('presence') is 'subscribe' then return @set('d/actions', ['authorize', 'unauthorize']) if @get('presence') isnt 'unavailable' and Tram.NS.WEBRTC in (@get('features') ? []) actions.push('wait', 'hang-up') actions.push('accept', 'decline') @set('d/actions', actions) class Tram.Profile extends Backbone.Model @on('add change:avatar', @optimizeAvatar) @on 'add change:fullname change:nickname', -> @set('d/handle', @get('fullname') or @get('nickname') or @get('bjid')) if not (avatar?.mime and avatar?.data) return @unset('avatar/url') data = "data:#{ avatar.mime };base64,#{ avatar.data }" if data.length < @optimizeThreshold return @set('avatar/url', data) img.addEventListener 'load', => if img.width < @optimizeSide and img.height < @optimizeSide return @set('avatar/url', data) ratio = Math.min(@optimizeSide / img.width, @optimizeSide / img.height) canvas = document.createElement('canvas') canvas.height = Math.round(img.height * ratio) canvas.width = Math.round(img.width * ratio) ctx = canvas.getContext('2d') ctx.drawImage(img, 0, 0, canvas.width, canvas.height) if avatar.mime is 'image/jpeg' optimized = canvas.toDataURL(mime) if img.src.length < optimized.length @set('avatar/url', optimized) class Tram.Profiles extends Backbone.Collection class Tram.Entity extends Backbone.Model class Tram.Entities extends Backbone.Collection class Tram.Contacts extends Backbone.Collection if model.get('presence') is 'unavailable' else if model.get('presence') is 'subscribe' else if model.get('type') is 'self' @on('change:presence change:type', @sort) class Tram.AvatarView extends Backbone.View templateEl: $($('#avatar-template').html()) colors: Tram.colors.avatar @setElement(@templateEl.clone()) ci = hash % @colors.length "color: white; background: #{ @colors[ci] };" @rivet = rivets.bind(@el, contact: @model, view: @) class Tram.ContactView extends Backbone.View templateEl: $($('#contact-template').html()) 'click [data-chat]': -> @model.trigger('action/chat') 'click [data-authorize]': -> @model.trigger('action/authorize') 'click [data-unauthorize]': -> @model.trigger('action/unauthorize') 'click [data-call]': (event) -> @model.trigger('action/call', event.currentTarget.getAttribute('data-call')) 'click [data-accept]': (event) -> @model.trigger('action/accept', event.currentTarget.getAttribute('data-accept')) 'click [data-decline]': -> @model.trigger('action/decline') 'click [data-hang-up]': -> @model.trigger('action/hangup') 'click [data-remove]': -> @model.trigger('action/remove') @setElement(@templateEl.clone()) @$avatarColumn = @$('.avatar-column') @listenTo(@model, 'remove', @remove) getBorderColor: (show) => "border-left-color: #{ @colors[show] || @colors['default'] };" "background: #{ @colors[show] || @colors['default'] };" @rivet = rivets.bind(@el, contact: @model, view: @) @av = new Tram.AvatarView(model: @model) @$avatarColumn.prepend(@av.render().el) class Tram.ContactsApp extends Backbone.View @listenTo(@collection, 'add', @onAdd) @listenTo(@collection, 'sort', @onSort) onAdd: (model, collection) -> mi = collection.indexOf(model) model.view = new Tram.ContactView(model: model) el = model.view.render().el @$el.children().eq(mi - 1).after(el) onSort: (collection, options) -> collection.each (model) => @$el.append(model.view.el)