Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Viz committed Aug 2, 2011
0 parents commit c1ec7ae
Show file tree
Hide file tree
Showing 15 changed files with 2,860 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .couchappignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
// filenames matching these regexps will not be pushed to the database
// uncomment to activate; separate entries with ","
// ".*~$"
// ".*\\.swp$"
// ".*\\.bak$"
]
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.DS_Store
.couchapprc
.dumps/*
.dumps/**/*
.releases/*
.releases/**/*
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
## Generated by CouchApp

CouchApps are web applications which can be served directly from [CouchDB](http://couchdb.apache.org). This gives them the nice property of replicating just like any other data stored in CouchDB. They are also simple to write as they can use the built-in jQuery libraries and plugins that ship with CouchDB.

[More info about CouchApps here.](http://couchapp.org)

## Deploying this app

Assuming you just cloned this app from git, and you have changed into the app directory in your terminal, you want to push it to your CouchDB with the CouchApp command line tool, like this:

couchapp push . http://name:password@hostname:5984/mydatabase

If you don't have a password on your CouchDB (admin party) you can do it like this (but it's a bad, idea, set a password):

couchapp push . http://hostname:5984/mydatabase

If you get sick of typing the URL, you should setup a `.couchapprc` file in the root of your directory. Remember not to check this into version control as it will have passwords in it.

The `.couchapprc` file should have contents like this:

{
"env" : {
"public" : {
"db" : "http://name:[email protected]/mydatabase"
},
"default" : {
"db" : "http://name:pass@localhost:5984/mydatabase"
}
}
}

Now that you have the `.couchapprc` file set up, you can push your app to the CouchDB as simply as:

couchapp push

This pushes to the `default` as specified. To push to the `public` you'd run:

couchapp push public

Of course you can continue to add more deployment targets as you see fit, and give them whatever names you like.
75 changes: 75 additions & 0 deletions _attachments/css/main.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/* add styles here */

body {
font:1em Helvetica, sans-serif;
padding:4px;
}

h1 {
margin-top:0;
}

#account {
float:right;
}

#profile {
border:4px solid #edd;
background:#fee;
padding:8px;
margin-bottom:8px;
}

#items {
border:4px solid #dde;
background:#eef;
padding:8px;
width:60%;
float:left;
}

#sidebar {
border:4px solid #dfd;
padding:8px;
float:right;
width:30%;
}

#items li {
border:4px solid #f5f5ff;
background:#fff;
padding:8px;
margin:4px 0;
}

form {
padding:4px;
margin:6px;
background-color:#ddd;
}

div.avatar {
padding:2px;
padding-bottom:0;
margin-right:4px;
float:left;
font-size:0.78em;
width : 60px;
height : 60px;
text-align: center;
}

div.avatar .name {
padding-top:2px;
}

div.avatar img {
margin:0 auto;
padding:0;
width : 40px;
height : 40px;
}

#items ul {
list-style: none;
}
26 changes: 26 additions & 0 deletions _attachments/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<!DOCTYPE html>
<html>
<head>
<title>My New CouchApp</title>
<link rel="stylesheet" href="style/main.css" type="text/css">
</head>
<body>
<script type="text/template" id="entry-template">
<span class="from_content">{{from}}: </span><span class="text_content">{{message}}</span><span class="delete">X</span>
</script>

<script type="text/template" id="private-entry-template">
<span class="from_content">{{from}} to {{to}}: </span><span class="text_content">{{message}}</span>
</script>
</body>
<script type="text/javascript" src="/_utils/script/sha1.js"></script>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/jquery.couch.js"></script>
<script type="text/javascript" src="js/underscore.js"></script>
<script type="text/javascript" src="js/backbone.js"></script>
<script type="text/javascript" src="js/backbone-couchdb.js"></script>
<script type="text/javascript" src="js/app.js"></script>
<script type="text/javascript" charset="utf-8">

</script>
</html>
194 changes: 194 additions & 0 deletions _attachments/js/app.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
$ ->
Backbone.couch_connector.config.db_name = "couchbreak"
Backbone.couch_connector.config.ddoc_name = "couchbreak"
Backbone.couch_connector.config.global_changes = false
_.templateSettings = interpolate: /\{\{(.+?)\}\}/g
UserModel = Backbone.Model.extend(defaults: name: "Anonymus")
window.CurrentUser = new UserModel()
MessageModel = Backbone.Model.extend(initialize: ->
@set date: new Date().getTime() unless @get("date")
)
MessagesList = Backbone.Collection.extend(
db:
view: "messages"
changes: true
filter: Backbone.couch_connector.config.ddoc_name + "/messages"

url: "/messages"
model: MessageModel
comparator: (comment) ->
comment.get "date"
)
Messages = new MessagesList()
PrivateMessage = MessageModel.extend({})
PrivateMessageList = Backbone.Collection.extend(
db:
view: "none__"
changes: false
filter: Backbone.couch_connector.config.ddoc_name + "/private_messages"

url: "/private_messages"
model: PrivateMessage
)
PrivateMessages = new PrivateMessageList()
InputView = Backbone.View.extend(
el: $("#input")
regex: /@(\w+)/
events:
"click #send": "onSubmit"
"keypress #message": "keypress"

initialize: ->
_.bindAll this, "onSubmit", "nameChanged", "keypress"
CurrentUser.bind "change:name", @nameChanged

onSubmit: ->
message = $("#message").val()
message = message.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/"/g, "&quot;")
if message.length > 0
executed = @regex.exec(message)
if executed?
PrivateMessages.create
from: CurrentUser.get("name")
to: executed[1]
message: message.replace(executed[0], "")
else
Messages.create
from: CurrentUser.get("name")
message: message
$("#message").val ""

nameChanged: ->
$("#name").text CurrentUser.get("name")

keypress: (ev) ->
@onSubmit() if ev.keyCode == 13

fillAndFocus: (text) ->
$("#message").val(text).focus()
)
EntryView = Backbone.View.extend(
tagName: "li"
template: _.template($("#entry-template").html())
events: "click .delete": "delete_me"
initialize: ->
_.bindAll this, "render", "delete_me", "delete_row"
@model.bind "change", @render
@model.bind "remove", @delete_row

render: ->
content = @model.toJSON()
$(@el).html @template(content)
this

delete_me: ->
if CurrentUser.get("name") == @model.get("from")
@model.destroy()
@delete_row()
else
alert "You can only delete your own messages!"

delete_row: ->
$(@el).remove()
)
PrivateEntryView = EntryView.extend(
className: "private"
template: _.template($("#private-entry-template").html())
)
MessagesList = Backbone.View.extend(
el: $("#messages")
initialize: ->
_.bindAll this, "reseted", "addRow", "addPrivateRow"
Messages.bind "reset", @reseted
Messages.bind "add", @addRow
PrivateMessages.bind "add", @addPrivateRow

addRow: (comment) ->
view = new EntryView(model: comment)
rendered = view.render().el
@el.append rendered

addPrivateRow: (private_message) ->
view = new PrivateEntryView(model: private_message)
rendered = view.render().el
@el.append rendered

reseted: ->
@el.html ""
Messages.each @addRow if Messages.length > 0
)
UserSession = Backbone.Model.extend({})
UserListCollection = Backbone.Collection.extend(
db: changes: true
url: "/user_list"
model: UserSession
)
UserList = new UserListCollection()
UserListEntry = Backbone.View.extend(
tagName: "li"
className: "user"
initialize: ->
_.bindAll this, "remove_me"
@model.bind "remove", @remove_me

render: ->
@el = $(@el)
@el.html ""
@el.unbind()
@el.text @model.get("name")
temp = "@" + @model.get("name") + " "
@el.click ->
Input.fillAndFocus temp

@el

remove_me: ->
that = this
@el.fadeOut ->
that.el.remove()
)
UserListView = Backbone.View.extend(
el: $("#userlist")
initialize: ->
_.bindAll this, "reseted", "addRow"
UserList.bind "add", @addRow
UserList.bind "reset", @reseted

addRow: (model) ->
@el.append new UserListEntry(model: model).render()

reseted: ->
UserList.each @addRow
)
App = Backbone.Router.extend(initialize: ->
UserList.fetch()
)
CurrentSession = null
Input = new InputView()
_.delay (->
$(window).unload ->
$.ajaxSetup async: false
CurrentSession.destroy() if CurrentSession?

$("#login").couchLogin
loggedIn: (user) ->
CurrentUser.set user
PrivateMessages.listen_to_changes()
unless UserList.detect((user) ->
user.get("name") == CurrentUser.get("name")
)
CurrentSession = UserList.create(
name: CurrentUser.get("name")
logged_in_at: new Date().getTime()
)

loggedOut: ->
PrivateMessages.stop_changes()
CurrentUser.set new UserModel().toJSON()
CurrentUser.trigger "change:name"
CurrentSession.destroy() if CurrentSession?

new MessagesList()
new UserListView()
new App()
), 100
Loading

0 comments on commit c1ec7ae

Please sign in to comment.