Skip to content

Commit

Permalink
Added support for saving tile.json from the browser
Browse files Browse the repository at this point in the history
  • Loading branch information
njh committed Sep 6, 2014
1 parent ae8a050 commit 2c1bb5b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
13 changes: 13 additions & 0 deletions marquette.js
Expand Up @@ -19,6 +19,7 @@ var util = require("util");
var express = require("express");
var nopt = require("nopt");
var path = require("path");
var fs = require("fs");
var mqtt = require('mqtt');

// Middleware
Expand Down Expand Up @@ -134,6 +135,18 @@ app.get('/tiles', function(req, res) {
res.send(tiles);
});

// FIXME: this should be a PUT
app.post('/tiles', function(req, res) {
fs.writeFile('tiles.json', JSON.stringify(req.body, null, 4), function(err) {
if(err) {
console.log(err);
res.status(500).send(err);
} else {
res.status(204).end();
}
});
});

app.get('/update-stream', function(req, res) {
req.socket.setNoDelay(true);
res.writeHead(200, {
Expand Down
33 changes: 27 additions & 6 deletions public/js/marquette.js
Expand Up @@ -15,6 +15,18 @@ $.postJSON = function(url, data, callback) {

$(function(){ //DOM Ready

function serialise_params(tile, wgd)
{
var params = { row: wgd.row, col: wgd.col };
var blacklist = ['row', 'col', 'coords', 'sizex', 'sizey'];
for(var key in tile.data()) {
if (blacklist.indexOf(key) < 0) {
params[key] = tile.data(key);
}
}
return params;
}

function init_gridster()
{
var win = $( window );
Expand All @@ -36,6 +48,7 @@ $(function(){ //DOM Ready
widget_selector: ".tile",
widget_base_dimensions: [tile_size, tile_size],
widget_margins: [margin_x, margin_y],
serialize_params: serialise_params,
min_cols: columns,
max_cols: columns
}).data('gridster');
Expand Down Expand Up @@ -72,17 +85,25 @@ $(function(){ //DOM Ready
});
});

function save_tiles(button) {
var tile_data = gridster.serialize();
$.postJSON(
'tiles', tile_data
).done(function(data, textStatus) {
$( '.tile' ).removeClass('editable');
gridster.disable();
button.html('Edit...');
});
}

$("#edit").on("click", function(event) {
var target = $( event.target );
if (target.html() == 'Edit...') {
var button = $( event.target );
if (button.html() == 'Edit...') {
$( '.tile' ).addClass('editable');
gridster.enable();
target.html('Save');
button.html('Save');
} else {
$( '.tile' ).removeClass('editable');
gridster.disable();
target.html('Edit...');
save_tiles(button);
}
});

Expand Down

0 comments on commit 2c1bb5b

Please sign in to comment.