Permalink
Browse files

Merge pull request #13 from richo/wip/modal_new_objects

modal new objects
  • Loading branch information...
2 parents d05e532 + f8b1775 commit 9a471011940c1a23c22c129ae2195a557ec78552 @richo committed Mar 25, 2013
View
@@ -35,4 +35,7 @@ clean:
groundstation_test:
GROUNDSTATION_DEBUG=WARN python -m unittest discover test
-test: groundstation_test
+airship_test:
+ cd airship; ../node_modules/mocha/bin/mocha
+
+test: groundstation_test airship_test
View
@@ -14,6 +14,7 @@
import pygit2
from groundstation.utils import oid2hex
+from groundstation.objects.root_object import RootObject
from groundstation.objects.update_object import UpdateObject
def jsonate(obj, escaped):
@@ -90,4 +91,36 @@ def update_gref(channel, identifier):
station.update_gref(gref, [oid], parents)
return jsonate({"response": "ok"}, False)
+ @app.route("/grefs/<channel>", methods=['PUT'])
+ def create_gref(channel):
+ def _write_object(obj):
+ return station.write(obj.as_object())
+
+ name = request.form["name"]
+ protocol = request.form["protocol"]
+ user = request.form["user"]
+ body = request.form["body"]
+ title = request.form["title"]
+ gref = Gref(station.store, channel, name)
+ root = RootObject(name, channel, protocol)
+ root_oid = _write_object(root)
+
+ _title = UpdateObject([root_oid], json.dumps({
+ "type": "title",
+ "id": None,
+ "body": title,
+ "user": user
+ }))
+ title_oid = _write_object(_title)
+
+ _body = UpdateObject([title_oid], json.dumps({
+ "type": "body",
+ "id": None,
+ "body": body
+ }))
+ body_oid = _write_object(_body)
+
+ station.update_gref(gref, [body_oid], [])
+ return ""
+
return app
@@ -0,0 +1,41 @@
+groundstation.validators.gref = (function() {
+ func = function(name, title, body) {
+ return func.valid_name_p(name) &&
+ func.valid_title_p(title) &&
+ func.valid_body_p(body);
+ };
+ return function(d_name, d_title, d_body) {
+ func.valid_name_p = function(name) {
+ if ((typeof name) !== 'string')
+ return false;
+ if (name.length === 0)
+ return false;
+ if (name === d_name)
+ return false;
+ if (name.indexOf(" ") >= 0)
+ return false;
+ return true;
+ };
+ func.valid_title_p = function(title) {
+ if ((typeof title) !== 'string')
+ return false;
+ if (title.length === 0)
+ return false;
+ if (title === d_title)
+ return false;
+ // TODO
+ return true;
+ };
+ func.valid_body_p = function(body) {
+ if ((typeof body) !== 'string')
+ return false;
+ if (body.length === 0)
+ return false;
+ if (body === d_body)
+ return false;
+ // TODO
+ return true;
+ };
+ return func;
+ };
+})();
View
@@ -7,12 +7,13 @@ function Groundstation() {
this.username = localStorage.getItem("airship.committer") || "Anonymous Coward";
this.renderers = {};
+ this.validators = {};
}
function init_airship(groundstation) {
_.each(groundstation.channels.models, function(channel) {
new ChannelTab({
model: channel,
- id: channel.attributes["name"]
+ id: channel.attributes["name"]
});
});
rendered_gref_content = new RenderedGref({
@@ -34,6 +35,52 @@ function init_airship(groundstation) {
}
}
});
+ var new_gref = {
+ title: $("#new-gref-title").html(),
+ body: $("#new-gref-body").html(),
+ name: $("#new-gref-name").html()
+ };
+ var new_gref_validator = groundstation.validators.gref(new_gref.name, new_gref.title, new_gref.body);
+
+ $("#new-gref").on('click', function() {
+ $("#new-gref-title").html(new_gref.title);
+ $("#new-gref-body").html(new_gref.body);
+ $("#new-gref-name").html(new_gref.name);
+ var modal = $("#new-gref-modal");
+ modal.modal();
+ });
+ $("#new-gref-cancel").on('click', function() {
+ $("#new-gref-modal").modal('hide');
+ });
+ $("#new-gref-create").on('click', function() {
+ var title = $("#new-gref-title").html(),
+ body = $("#new-gref-body").html(),
+ name = $("#new-gref-name").html(),
+ protocol = $("#new-gref-protocol").html();
+
+ if (new_gref_validator(name, title, body)) {
+ $.ajax({
+ type: "PUT",
+ url: groundstation.active_grefs.url,
+ data: {
+ title: title,
+ body: body,
+ name: name,
+ protocol: protocol,
+
+ user: groundstation.username
+
+ },
+ success: function(data, st, xhr) {
+ $("#new-gref-modal").modal('hide');
+ groundstation.active_grefs.redraw();
+ }
+ });
+ } else {
+ // TODO Actually give the user something to go with
+ alert("Validation failed!");
+ }
+ });
}
var Channel = Backbone.Model.extend();
@@ -108,18 +155,21 @@ var ChannelTab = Backbone.View.extend({
var self = this;
var current_grefs = $("#current-grefs")[0];
groundstation.active_grefs.url = '/grefs/' + this.model.attributes["name"];
- groundstation.active_grefs.fetch({
- success: function(collection, response, options) {
- $("#active-channel").html(self.model.attributes["name"]);
- $("#gref-container").show();
- _.each(visible_grefs, function(el) { el.remove(); });
- _.each(collection.models, function(gref) {
- visible_grefs.push(new GrefMenuItem({
- model: gref
- }));
- });
- }
- });
+ groundstation.active_grefs.redraw = function() {
+ groundstation.active_grefs.fetch({
+ success: function(collection, response, options) {
+ $("#active-channel").html(self.model.attributes["name"]);
+ $("#gref-container").show();
+ _.each(visible_grefs, function(el) { el.remove(); });
+ _.each(collection.models, function(gref) {
+ visible_grefs.push(new GrefMenuItem({
+ model: gref
+ }));
+ });
+ }
+ });
+ };
+ groundstation.active_grefs.redraw();
},
events: {
Oops, something went wrong.

0 comments on commit 9a47101

Please sign in to comment.