Permalink
Browse files

added initial files

  • Loading branch information...
1 parent 4332bea commit 4e77deab3672ab97a59df93fe41b91ba3a90292c @jamesor committed Feb 1, 2013
View
@@ -0,0 +1 @@
+node_modules
View
@@ -0,0 +1,4 @@
+.git*
+examples/
+test/
+.DS_Store
View
22 LICENSE
@@ -0,0 +1,22 @@
+(The MIT License)
+
+Copyright (c) 2013 James O'Reilly <james@jamesor.com>
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+'Software'), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
@@ -0,0 +1 @@
+node_modules
View
@@ -0,0 +1,32 @@
+var express = require('express')
+ , mongoose = require('mongoose')
+ , story = require('./models/story')
+ , routes = require('./routes')
+ , http = require('http')
+ , path = require('path');
+
+var app = express();
+
+app.configure(function () {
+ app.set('port', process.env.PORT || 3000);
+ app.set('views', __dirname + '/views');
+ app.set('view engine', 'jade');
+ app.use(express.favicon());
+ app.use(express.logger('dev'));
+ app.use(express.bodyParser());
+ app.use(express.methodOverride());
+ app.use(app.router);
+ app.use(express.static(path.join(__dirname, 'public')));
+});
+
+app.configure('development', function () {
+ app.use(express.errorHandler());
+});
+
+mongoose.connect("127.0.0.1", "test", 27017);
+
+routes.init(app);
+
+http.createServer(app).listen(app.get('port'), function () {
+ console.log("Express server listening on port " + app.get('port'));
+});
View
@@ -0,0 +1,13 @@
+var mongoose = require('mongoose')
+ , versioner = require('../../index')
+
+ , StorySchema = new mongoose.Schema({
+ title: {'type': String, 'default': 'Untitled Story'},
+ deck: {'type': String, 'default': ''},
+ created: {'type': Date, 'default': function () { return new Date(); }},
+ updated: {'type': Date, 'default': function () { return new Date(); }}
+ });
+
+StorySchema.plugin(versioner, {modelName:'Story', mongoose:mongoose});
+
+module.exports = mongoose.model('Story', StorySchema);
View
@@ -0,0 +1,36 @@
+{
+ "name": "mongoose-versioner-example",
+ "version": "0.0.1",
+ "description": "Example use of the document versioning plugin for Mongoose",
+ "main": "./app.js",
+ "scripts": {
+ "start": "node app"
+ },
+ "dependencies": {
+ "async" : "0.1.22",
+ "express" : "3.1.0",
+ "http" : "*",
+ "jade" : "0.28.1",
+ "mongoose" : "3.5.2",
+ "node-uuid" : "1.4.0",
+ "path" : "0.4.9",
+ "underscore" : "1.4.4"
+ },
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/jamesor/mongoose-versioner"
+ },
+ "homepage" : "https://github.com/jamesor/mongoose-versioner",
+ "keywords": [
+ "mongoose",
+ "plugin",
+ "version"
+ ],
+ "readmeFilename": "README.md",
+ "author": {
+ "name": "James O'Reilly",
+ "email": "james@jamesor.com",
+ "url": "https://github.com/jamesor/"
+ },
+ "licence": "MIT"
+}
@@ -0,0 +1,21 @@
+function deleteVersion(id, url) {
+ 'use strict';
+
+ if (confirm('Are you sure you wish to delete this item?')) {
+ var $el = $('#' + id);
+ $.ajax({
+ url:url,
+ type:'DELETE',
+ success:function (data, textStatus, jqXHR) {
+ if (data.success) {
+ $el.fadeOut('slow', function () {
+ $el.remove();
+ });
+ }
+ },
+ error:function (jqXHR, textStatus, errorThrown) {
+ //console.log(arguments);
+ }
+ });
+ }
+}
@@ -0,0 +1,47 @@
+body {
+ padding: 50px;
+ font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
+}
+a {
+ color: #00B7FF;
+}
+table {
+ border-collapse: collapse;
+}
+table, th, td {
+ border: 1px solid black;
+}
+tr.selected {
+ background-color: #ffffe0;
+}
+th {
+ background-color: gray;
+ color: white;
+ font-size: 11px;
+}
+td {
+ font-size: 12px;
+ height: 25px;
+ padding: 0 5px;
+}
+th.version {
+ width: 200px;
+}
+th.timestamp {
+ width: 300px;
+}
+td.timestamp {
+ font-family: consolas;
+}
+td.actions button {
+ width: 75px;
+}
+td.actions button:first-child {
+ margin-right: 5px;
+}
+form div {
+ margin-top: 10px;
+}
+form div.itemId span {
+ color: #a9a9a9;
+}
View
@@ -0,0 +1,98 @@
+(function (exports) {
+
+ 'use strict';
+
+ var async = require('async'),
+ _ = require('underscore'),
+ uuid = require('node-uuid'),
+ mongoose = require('mongoose'),
+ Story = mongoose.model('Story');
+
+ function errMsg(msg) {
+ return {'error':{'message':msg.toString()}};
+ }
+
+ function item_list(req, res) {
+ Story.find({}, function (err, result) {
+ if (err) {
+ res.send(errMsg(err));
+ } else {
+ res.render('index', {title:'Documents', stories:result});
+ }
+ });
+ }
+
+ function item_new(req, res) {
+ res.render('story', {title:'Create Document', item:{_id:'', title:'', deck:'', versions:[]}});
+ }
+
+ function item_read(req, res) {
+ Story.findVersionById(req.params.vid, null, null, function (err, result) {
+ if (err) {
+ res.send(errMsg(err));
+ } else {
+ Story.findVersions(result.versionOfId, null, null, function (err, versions) {
+ if (err) {
+ res.send(errMsg(err));
+ } else {
+ res.render('story', {title:'Edit Document', item:result, versions:versions, selectedId:req.params.vid});
+ }
+ });
+ }
+ });
+ }
+
+ function item_save(req, res) {
+ var dataObj = {
+ data:req.body,
+ versionId:req.params.vid,
+ versionOfId:req.params.id
+ };
+
+ if (req.body.saveAsNewVersion === 'true') {
+ delete dataObj.data.saveAsNewVersion;
+ dataObj.versionId = null;
+ }
+
+ dataObj.data.updated = new Date();
+
+ Story.saveVersion(dataObj, function (err, version) {
+ if (err) {
+ res.send(errMsg(err));
+ } else {
+ res.redirect("/story/" + version.versionOfId + "/v/" + version._id);
+ }
+ });
+ }
+
+ function item_delete(req, res) {
+ Story.deleteVersion(req.params.vid, function (err, result) {
+ if (err) {
+ res.send({'success':false});
+ } else {
+ res.send(result);
+ }
+ });
+ }
+
+ function item_activate(req, res) {
+ Story.activateVersion(req.params.vid, function (err, result) {
+ if (err || result === null) {
+ res.send({'success':false});
+ } else {
+ res.redirect("/story/" + result._id + "/v/" + result.versionId);
+ }
+ });
+ }
+
+ exports.init = function (app) {
+ app.get('/', item_list);
+ app.get('/story', item_new);
+ app.post('/story', item_save);
+ app.get('/story/:id/v/:vid', item_read);
+ app.post('/story/:id/v/:vid', item_save);
+ app.del('/story/:id/v/:vid', item_delete);
+ app.get('/story/:id/v/:vid/activate', item_activate);
+ };
+
+})(exports);
View
@@ -0,0 +1,20 @@
+extends layout
+
+block content
+ h1 Stories
+ button(onclick="document.location.href='/story'") Create New Story
+
+ table
+ thead
+ tr
+ th Title
+ th Created
+ th Updated
+ tbody
+ - for item, i in stories
+ - item = item.toObject()
+ tr
+ td.title
+ a(href="/story/#{item._id}/v/#{item.versionId}") #{item.title}
+ td.timestamp= item.created
+ td.timestamp= item.updated
@@ -0,0 +1,9 @@
+doctype 5
+html
+ head
+ title= title
+ link(rel='stylesheet', href='/stylesheets/style.css')
+ script(src='http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js')
+ block head-scripts
+ body
+ block content
View
@@ -0,0 +1,59 @@
+extends layout
+
+block head-scripts
+ script(src='/javascripts/story.js')
+
+block content
+ button(onclick="document.location.href='/'") Back to Document List
+
+ h1= title
+
+
+ form(method="post")
+ div.itemId
+ label(for="Id") Id:
+ span #{item._id}
+ div
+ label(for="title") Title
+ br
+ input(type="text", name="title", maxlength="100", size="80", value="#{item.title}")
+ div
+ label(for="deck") Deck
+ br
+ textarea(name="deck", rows="20", cols="80")= item.deck
+ div
+ -if (item._id == '')
+ input(type="submit", value="Create New Story")
+ input(type="reset", value="Clear Form")
+ -else
+ input(type="submit", value="Save Changes")
+ input(type="reset", value="Revert Changes")
+ button(name="saveAsNewVersion", value="true") Save Changes as New Version
+ -if (typeof versions != 'undefined' && typeof versions.docs !== 'undefined' && versions.docs.length > 0)
+ h2 Versions List
+ table
+ thead
+ tr
+ th.version Version Id
+ th.timestamp Last Modified
+ th.actions Actions
+ tbody
+ -var action, isActive, isSelected, trClass, version, i
+ -for version, i in versions.docs
+ -action = '/story/' + version.versionOfId + '/v/' + version._id
+ -isActive = (versions.activeId.toString() == version._id.toString())
+ -isSelected = (version._id.toString() == selectedId)
+ -trClass = (isSelected) ? 'selected' : ''
+ tr(id=version._id, class=trClass)
+ td.version
+ -if (isSelected)
+ | #{version._id}
+ -else
+ a(href=action)= version._id
+ td.timestamp= version.updated
+ td.actions
+ -if (isActive)
+ | &lt;- Active Version
+ -else
+ button(onclick='document.location.href="#{action}/activate"') Activate
+ button(onclick='deleteVersion("#{version._id}", "#{action}")') Delete
Oops, something went wrong.

0 comments on commit 4e77dea

Please sign in to comment.