| author | rougeronj |
| Mon, 05 Jan 2015 10:06:39 +0100 | |
| changeset 359 | 9fc64b41466c |
| parent 357 | 70e577b0cdc6 |
| child 368 | 3abc79964948 |
| permissions | -rw-r--r-- |
| 52 | 1 |
/* Saves the Full JSON once */ |
2 |
||
|
320
154b121a43f1
enhance save on click (snapshot mode) and update django app
cavaliet
parents:
293
diff
changeset
|
3 |
Rkns.jsonIOSaveOnClick = function(_renkan, _opts) { |
| 52 | 4 |
var _proj = _renkan.project, |
5 |
_saveWarn = false, |
|
6 |
_onLeave = function() { |
|
7 |
return "Project not saved"; |
|
8 |
}; |
|
|
293
fba23fde14ba
Correct jshint errors and force it on build
ymh <ymh.work@gmail.com>
parents:
190
diff
changeset
|
9 |
if (typeof _opts.http_method === "undefined") { |
| 52 | 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) { |
|
| 105 | 23 |
_proj.set(_data, {validate: true}); |
|
357
70e577b0cdc6
add save_status var to specify the status of the renkan (saved/to-save/saving)
rougeronj
parents:
320
diff
changeset
|
24 |
_proj.set({save_status:0}); |
| 116 | 25 |
_renkan.renderer.autoScale(); |
| 52 | 26 |
} |
27 |
}); |
|
|
170
603ffa4c6fa5
correct ";" and "," in javascripts
ymh <ymh.work@gmail.com>
parents:
116
diff
changeset
|
28 |
}; |
| 52 | 29 |
var _save = function() { |
30 |
_proj.set("saved_at", new Date()); |
|
31 |
var _data = _proj.toJSON(); |
|
32 |
Rkns.$.ajax({ |
|
33 |
type: _opts.http_method, |
|
34 |
url: _opts.url, |
|
35 |
contentType: "application/json", |
|
36 |
data: JSON.stringify(_data), |
|
|
357
70e577b0cdc6
add save_status var to specify the status of the renkan (saved/to-save/saving)
rougeronj
parents:
320
diff
changeset
|
37 |
beforeSend: function(){ |
|
70e577b0cdc6
add save_status var to specify the status of the renkan (saved/to-save/saving)
rougeronj
parents:
320
diff
changeset
|
38 |
_proj.set({save_status:2}); |
|
70e577b0cdc6
add save_status var to specify the status of the renkan (saved/to-save/saving)
rougeronj
parents:
320
diff
changeset
|
39 |
}, |
| 52 | 40 |
success: function(data, textStatus, jqXHR) { |
41 |
$(window).off("beforeunload", _onLeave); |
|
42 |
_saveWarn = false; |
|
|
357
70e577b0cdc6
add save_status var to specify the status of the renkan (saved/to-save/saving)
rougeronj
parents:
320
diff
changeset
|
43 |
_proj.set({save_status:0}); |
|
320
154b121a43f1
enhance save on click (snapshot mode) and update django app
cavaliet
parents:
293
diff
changeset
|
44 |
//document.location.hash = "#id=" + data.id; |
|
154b121a43f1
enhance save on click (snapshot mode) and update django app
cavaliet
parents:
293
diff
changeset
|
45 |
//$(".Rk-Notifications").text("Saved as "+document.location.href).fadeIn().delay(2000).fadeOut(); |
| 52 | 46 |
} |
47 |
}); |
|
|
170
603ffa4c6fa5
correct ";" and "," in javascripts
ymh <ymh.work@gmail.com>
parents:
116
diff
changeset
|
48 |
}; |
| 52 | 49 |
var _checkLeave = function() { |
|
357
70e577b0cdc6
add save_status var to specify the status of the renkan (saved/to-save/saving)
rougeronj
parents:
320
diff
changeset
|
50 |
_proj.set({save_status:1}); |
|
70e577b0cdc6
add save_status var to specify the status of the renkan (saved/to-save/saving)
rougeronj
parents:
320
diff
changeset
|
51 |
|
| 52 | 52 |
var title = _proj.get("title"); |
53 |
if (title && _proj.get("nodes").length) { |
|
54 |
$(".Rk-Save-Button").removeClass("disabled"); |
|
55 |
} else { |
|
56 |
$(".Rk-Save-Button").addClass("disabled"); |
|
57 |
} |
|
58 |
if (title) { |
|
59 |
$(".Rk-PadTitle").css("border-color","#333333"); |
|
60 |
} |
|
61 |
if (!_saveWarn) { |
|
62 |
_saveWarn = true; |
|
63 |
$(window).on("beforeunload", _onLeave); |
|
64 |
} |
|
|
170
603ffa4c6fa5
correct ";" and "," in javascripts
ymh <ymh.work@gmail.com>
parents:
116
diff
changeset
|
65 |
}; |
| 52 | 66 |
_load(); |
67 |
_proj.on("add:nodes add:edges add:users change", function(_model) { |
|
|
357
70e577b0cdc6
add save_status var to specify the status of the renkan (saved/to-save/saving)
rougeronj
parents:
320
diff
changeset
|
68 |
_model.on("change remove", function(_model) { |
| 359 | 69 |
if(!(_model.changedAttributes.length === 1 && _model.hasChanged('save_status'))) { |
|
357
70e577b0cdc6
add save_status var to specify the status of the renkan (saved/to-save/saving)
rougeronj
parents:
320
diff
changeset
|
70 |
_checkLeave(); |
|
70e577b0cdc6
add save_status var to specify the status of the renkan (saved/to-save/saving)
rougeronj
parents:
320
diff
changeset
|
71 |
} |
|
70e577b0cdc6
add save_status var to specify the status of the renkan (saved/to-save/saving)
rougeronj
parents:
320
diff
changeset
|
72 |
}); |
| 359 | 73 |
if(!(_proj.changedAttributes.length === 1 && _proj.hasChanged('save_status'))) { |
|
357
70e577b0cdc6
add save_status var to specify the status of the renkan (saved/to-save/saving)
rougeronj
parents:
320
diff
changeset
|
74 |
_checkLeave(); |
|
70e577b0cdc6
add save_status var to specify the status of the renkan (saved/to-save/saving)
rougeronj
parents:
320
diff
changeset
|
75 |
} |
| 52 | 76 |
}); |
| 190 | 77 |
_renkan.renderer.save = function() { |
78 |
if ($(".Rk-Save-Button").hasClass("disabled")) { |
|
| 52 | 79 |
if (!_proj.get("title")) { |
80 |
$(".Rk-PadTitle").css("border-color","#ff0000"); |
|
81 |
} |
|
82 |
} else { |
|
83 |
_save(); |
|
84 |
} |
|
|
293
fba23fde14ba
Correct jshint errors and force it on build
ymh <ymh.work@gmail.com>
parents:
190
diff
changeset
|
85 |
}; |
|
170
603ffa4c6fa5
correct ";" and "," in javascripts
ymh <ymh.work@gmail.com>
parents:
116
diff
changeset
|
86 |
}; |