|
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 _renkan.renderer.autoScale(); |
|
25 } |
|
26 }); |
|
27 } |
|
28 var _save = function() { |
|
29 _proj.set("saved_at", new Date()); |
|
30 var _data = _proj.toJSON(); |
|
31 Rkns.$.ajax({ |
|
32 type: _opts.http_method, |
|
33 url: _opts.url, |
|
34 contentType: "application/json", |
|
35 data: JSON.stringify(_data), |
|
36 success: function(data, textStatus, jqXHR) { |
|
37 $(window).off("beforeunload", _onLeave); |
|
38 _saveWarn = false; |
|
39 document.location.hash = "#id=" + data.id; |
|
40 $(".Rk-Notifications").text("Saved as "+document.location.href).fadeIn().delay(2000).fadeOut(); |
|
41 } |
|
42 }); |
|
43 } |
|
44 var _checkLeave = function() { |
|
45 var title = _proj.get("title"); |
|
46 if (title && _proj.get("nodes").length) { |
|
47 $(".Rk-Save-Button").removeClass("disabled"); |
|
48 } else { |
|
49 $(".Rk-Save-Button").addClass("disabled"); |
|
50 } |
|
51 if (title) { |
|
52 $(".Rk-PadTitle").css("border-color","#333333"); |
|
53 } |
|
54 if (!_saveWarn) { |
|
55 _saveWarn = true; |
|
56 $(window).on("beforeunload", _onLeave); |
|
57 } |
|
58 } |
|
59 _load(); |
|
60 _proj.on("add:nodes add:edges add:users change", function(_model) { |
|
61 _model.on("change remove", function(_model) { |
|
62 _checkLeave(); |
|
63 }); |
|
64 _checkLeave(); |
|
65 }); |
|
66 $(".Rk-Save-Button").click(function() { |
|
67 if ($(this).hasClass("disabled")) { |
|
68 if (!_proj.get("title")) { |
|
69 $(".Rk-PadTitle").css("border-color","#ff0000"); |
|
70 } |
|
71 } else { |
|
72 _save(); |
|
73 } |
|
74 }); |
|
75 } |