|
52
|
1 |
/* Saves the Full JSON once */ |
|
|
2 |
|
|
|
3 |
Rkns.jsonIO = function(_renkan, _opts) { |
|
|
4 |
var _proj = _renkan.project, |
|
|
5 |
_saveWarn = false, |
|
|
6 |
_onLeave = function() { |
|
|
7 |
return "Project not saved"; |
|
|
8 |
}; |
|
|
9 |
if (typeof _opts.http_method == "undefined") { |
|
|
10 |
_opts.http_method = 'POST'; |
|
|
11 |
} |
|
|
12 |
var _load = function() { |
|
|
13 |
var getdata = {}, |
|
|
14 |
rx = /id=([^&#?=]+)/, |
|
|
15 |
matches = document.location.hash.match(rx); |
|
|
16 |
if (matches) { |
|
|
17 |
getdata.id = matches[1]; |
|
|
18 |
} |
|
|
19 |
Rkns.$.ajax({ |
|
|
20 |
url: _opts.url, |
|
|
21 |
data: getdata, |
|
|
22 |
success: function(_data) { |
|
|
23 |
_proj.set(_data); |
|
|
24 |
} |
|
|
25 |
}); |
|
|
26 |
} |
|
|
27 |
var _save = function() { |
|
|
28 |
_proj.set("saved_at", new Date()); |
|
|
29 |
var _data = _proj.toJSON(); |
|
|
30 |
Rkns.$.ajax({ |
|
|
31 |
type: _opts.http_method, |
|
|
32 |
url: _opts.url, |
|
|
33 |
contentType: "application/json", |
|
|
34 |
data: JSON.stringify(_data), |
|
|
35 |
success: function(data, textStatus, jqXHR) { |
|
|
36 |
$(window).off("beforeunload", _onLeave); |
|
|
37 |
_saveWarn = false; |
|
|
38 |
document.location.hash = "#id=" + data.id; |
|
|
39 |
$(".Rk-Notifications").text("Saved as "+document.location.href).fadeIn().delay(2000).fadeOut(); |
|
|
40 |
} |
|
|
41 |
}); |
|
|
42 |
} |
|
|
43 |
var _checkLeave = function() { |
|
|
44 |
var title = _proj.get("title"); |
|
|
45 |
if (title && _proj.get("nodes").length) { |
|
|
46 |
$(".Rk-Save-Button").removeClass("disabled"); |
|
|
47 |
} else { |
|
|
48 |
$(".Rk-Save-Button").addClass("disabled"); |
|
|
49 |
} |
|
|
50 |
if (title) { |
|
|
51 |
$(".Rk-PadTitle").css("border-color","#333333"); |
|
|
52 |
} |
|
|
53 |
if (!_saveWarn) { |
|
|
54 |
_saveWarn = true; |
|
|
55 |
$(window).on("beforeunload", _onLeave); |
|
|
56 |
} |
|
|
57 |
} |
|
|
58 |
_load(); |
|
|
59 |
_proj.on("add:nodes add:edges add:users change", function(_model) { |
|
|
60 |
_model.on("change remove", function(_model) { |
|
|
61 |
_checkLeave(); |
|
|
62 |
}); |
|
|
63 |
_checkLeave(); |
|
|
64 |
}); |
|
|
65 |
$(".Rk-Save-Button").click(function() { |
|
|
66 |
if ($(this).hasClass("disabled")) { |
|
|
67 |
if (!_proj.get("title")) { |
|
|
68 |
$(".Rk-PadTitle").css("border-color","#ff0000"); |
|
|
69 |
} |
|
|
70 |
} else { |
|
|
71 |
_save(); |
|
|
72 |
} |
|
|
73 |
}); |
|
|
74 |
} |