Skip to content
This repository was archived by the owner on Nov 20, 2018. It is now read-only.
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
140 changes: 140 additions & 0 deletions app/assets/javascripts/emberfire.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
"use strict";

var EmberFire = Ember.Namespace.create();

EmberFire._checkType = function(snapshot, cb, binding) {
var obj = snapshot.val();
var type = obj._type;

switch (type) {
case "object":
cb.call(binding, EmberFire.Object.create({ ref: snapshot.ref() }));
break;
case "array":
cb.call(binding, EmberFire.Array.create({ ref: snapshot.ref() }));
break;
default:
cb.call(binding, obj);
}
};

EmberFire.Object = Ember.ObjectProxy.extend({
init: function() {
var object = {};
this.set("content", object);

function applyChange(snapshot) {
var key = snapshot.name();
/*jshint validthis:true */
EmberFire._checkType(snapshot, function(val) {
Ember.set(object, key, val);
}, this);
}

this.ref.child("_type").set("object");

this.ref.on("child_added", applyChange, this);

this.ref.on("child_changed", applyChange, this);

this.ref.on("child_removed", function(snapshot) {
this.set(snapshot.name(), null);
}, this);

this._super();
},

willDestroy: function() {
this.ref.off();
},

toJSON: function() {
var json = {},
object = this.get("content");

for (var key in object) {
json[key] = Ember.get(object, key);
}

json._type = "object";
return json;
},

setUnknownProperty: function(key, value) {
if (value instanceof EmberFire.Object || value instanceof EmberFire.Array) {
value.ref = this.ref.child(key);
value.ref.set(value.toJSON());
} else {
this.ref.child(key).set(value);
return this._super(key, value);
}
},

ref: null
});

EmberFire.Array = Ember.ArrayProxy.extend({
init: function() {
var array = Ember.A([]);
this._index = Ember.A([]);

this.set("content", array);

this.ref.child("_type").set("array");

this.ref.on("child_added", function(snapshot) {
if (snapshot.name() == "_type") {
return;
}
EmberFire._checkType(snapshot, function(val) {
this._index.pushObject(snapshot.name());
array.pushObject(val);
}, this);
}, this);

this.ref.on("child_removed", function(snapshot) {
if (snapshot.name() == "_type") {
return;
}
var idx = this._index.indexOf(snapshot.name());
this._index.removeAt(idx);
array.removeAt(idx);
}, this);

this.ref.on("child_changed", function(snapshot) {
if (snapshot.name() == "_type") {
return;
}
var idx = this._index.indexOf(snapshot.name());
array.replace(idx, 1, [snapshot.val()]);
}, this);

this._super();
},

replaceContent: function(idx, amt, objects) {
for (var i = 0; i < amt; i++) {
var key = this._index[idx+i];
this.ref.child(key).remove();
}
objects.forEach(function(object) {
var val = object;
if (object.toJSON) {
val = object.toJSON();
}
return this.ref.push(val).name();
}, this);
},

toJSON: function() {
var json = {},
values = this.get("content");

for (var i = 0; i < this._index.length; i++) {
json[this._index[i]] = values[i];
}

json._type = "array";
return json;
}
});
145 changes: 145 additions & 0 deletions app/assets/javascripts/firebase.js

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion app/assets/javascripts/live_reader.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
#= require handlebars
#= require ember-canary
#= require ember-data-canary
#= require firebase
#= require bootstrap
#= require pusher
#= require emberfire
#= require_self
#= require app

Expand Down
20 changes: 2 additions & 18 deletions app/assets/javascripts/routes/documents.js.coffee.erb
Original file line number Diff line number Diff line change
@@ -1,20 +1,4 @@
LiveReader.DocumentsRoute = Ember.Route.extend
model: ->
this.store.findAll('document')
activate: ->
# Setup Pusher
pusher = new Pusher('8146487894a3cc81758c')
@set('pusher', pusher)
channel = pusher.subscribe('<%= Rails.env %>_main_channel')
channel.bind 'update', (payload) =>
# TODO: Clean up this big ugly chunk of procedural code
payload.updated_at = new Date(payload.updated_at)
doc = @model().find((item) => item.get('id') == payload.id)
# debugger
if doc
doc.setProperties
title: payload.title
body: payload.body
updated_at: payload.updated_at
else
doc = LiveReader.Document.createRecord(payload)
EmberFire.Array.create
ref: new Firebase("https://lws.firebaseio.com")
33 changes: 27 additions & 6 deletions app/assets/javascripts/write_room.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,45 @@
#= require bootstrap
#= require_self
#= require redactor
#= require handlebars
#= require ember
#= require ember-data
#= require firebase
#= require emberfire

class Editor
dirty: false
snapshot:
body: false
title: false

constructor: ->
constructor: (documentId) ->
console.log("Initialising editor")
@documentId = documentId
@takeSnapshot()
@setupFirebase()

takeSnapshot: ->
console.log("Taking snapshot of current text")
@snapshot.body = @currentBody()
@snapshot.title = @currentTitle()

setupFirebase: (documentId) ->
@db = new Firebase("https://lws.firebaseio.com")
@db.once 'value', (snapshot) =>
snapshot.forEach (child) =>
@doc = child.ref() if child.val().id == @documentId
unless @doc
@doc = @db.push
id: @documentId
isVisible: true
body: @currentBody()
title: @currentTitle()
$(".redactor_editor").on "keyup", (element) =>
@doc.child('body').set @currentBody()
@doc.child('title').set @currentTitle()
console.log @currentBody()

currentBody: ->
$('#redactor').getCode()

Expand Down Expand Up @@ -74,15 +97,13 @@ WriteRoom.register = (label, callback) ->
WriteRoom.require = (label, data) ->
WriteRoom.fragments[label].call(window,data)

WriteRoom.register 'documents/edit', (data) ->
WriteRoom.register 'documents/edit', (documentId) ->
console.log("Attaching redactor to view")
$('#redactor').redactor()
console.log("Initialising Editor instance")

editor = new Editor

editor = new Editor(documentId)
editor.updateConnectionStatus("Ready!", "info")

setInterval ->
editor.update()
, 500
, 5000
5 changes: 1 addition & 4 deletions app/controllers/writeroom/documents_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,7 @@ def create

def update
@document = Document.find(params[:id])

if @document.update(document_params)
Pusher.trigger(pusher_channel, 'update', @document.to_pusher)
end
@document.update(document_params)
respond_with @document
end

Expand Down
8 changes: 0 additions & 8 deletions app/models/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,5 @@ def word_count
def notify_requester
DocumentNotifier.writing_started_on(self).deliver unless self.requester.blank?
end
def to_pusher
{
id: self.id.to_s,
title: self.title,
body: self.body,
updated_at: self.updated_at
}
end

end
2 changes: 1 addition & 1 deletion app/views/writeroom/documents/edit.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

- content_for :javascript do
:javascript
WriteRoom.require('documents/edit', {});
WriteRoom.require('documents/edit', #{@document.id});