client/js/save-once.js
author rougeronj
Tue, 22 Sep 2015 15:14:10 +0200
changeset 543 5f7bebdcfc0d
parent 516 7d1d44b53af5
permissions -rw-r--r--
Improve the way we init the view. The data loader send a "loaded" event, hooked by the scene.py and initializing the backbone.history and the view. We don't use redraw_active in save-once and full-json, because it was making the view initialization dependent of these file which are externals. Small fix to hide the "set saved view" button when there is only one view.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
52
e0f6f3c31150 Added colors !
veltr
parents:
diff changeset
     1
/* Saves the Full JSON once */
e0f6f3c31150 Added colors !
veltr
parents:
diff changeset
     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
e0f6f3c31150 Added colors !
veltr
parents:
diff changeset
     4
    var _proj = _renkan.project,
e0f6f3c31150 Added colors !
veltr
parents:
diff changeset
     5
        _saveWarn = false,
e0f6f3c31150 Added colors !
veltr
parents:
diff changeset
     6
        _onLeave = function() {
e0f6f3c31150 Added colors !
veltr
parents:
diff changeset
     7
            return "Project not saved";
e0f6f3c31150 Added colors !
veltr
parents:
diff changeset
     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
e0f6f3c31150 Added colors !
veltr
parents:
diff changeset
    10
        _opts.http_method = 'POST';
e0f6f3c31150 Added colors !
veltr
parents:
diff changeset
    11
    }
e0f6f3c31150 Added colors !
veltr
parents:
diff changeset
    12
    var _load = function() {
e0f6f3c31150 Added colors !
veltr
parents:
diff changeset
    13
        var getdata = {},
e0f6f3c31150 Added colors !
veltr
parents:
diff changeset
    14
            rx = /id=([^&#?=]+)/,
e0f6f3c31150 Added colors !
veltr
parents:
diff changeset
    15
            matches = document.location.hash.match(rx);
e0f6f3c31150 Added colors !
veltr
parents:
diff changeset
    16
        if (matches) {
e0f6f3c31150 Added colors !
veltr
parents:
diff changeset
    17
            getdata.id = matches[1];
e0f6f3c31150 Added colors !
veltr
parents:
diff changeset
    18
        }
e0f6f3c31150 Added colors !
veltr
parents:
diff changeset
    19
        Rkns.$.ajax({
e0f6f3c31150 Added colors !
veltr
parents:
diff changeset
    20
            url: _opts.url,
e0f6f3c31150 Added colors !
veltr
parents:
diff changeset
    21
            data: getdata,
368
3abc79964948 Close #33 - Add loading status var to set up a loading bar while the renkan is loading
rougeronj
parents: 359
diff changeset
    22
            beforeSend: function(){
443
4c7ab16e5845 best coding convention for loadingstatus and savingstatus, rempve loadingatatus form export
ymh <ymh.work@gmail.com>
parents: 368
diff changeset
    23
            	_proj.set({loadingStatus:true});
368
3abc79964948 Close #33 - Add loading status var to set up a loading bar while the renkan is loading
rougeronj
parents: 359
diff changeset
    24
            },
52
e0f6f3c31150 Added colors !
veltr
parents:
diff changeset
    25
            success: function(_data) {
444
19f0b7803aed add schema version + dataloaders to ensure data migrations + small correction for php server exmple + a readme for the php server example
ymh <ymh.work@gmail.com>
parents: 443
diff changeset
    26
                _renkan.dataloader.load(_data);
443
4c7ab16e5845 best coding convention for loadingstatus and savingstatus, rempve loadingatatus form export
ymh <ymh.work@gmail.com>
parents: 368
diff changeset
    27
                _proj.set({loadingStatus:false});
4c7ab16e5845 best coding convention for loadingstatus and savingstatus, rempve loadingatatus form export
ymh <ymh.work@gmail.com>
parents: 368
diff changeset
    28
                _proj.set({saveStatus:0});
52
e0f6f3c31150 Added colors !
veltr
parents:
diff changeset
    29
            }
e0f6f3c31150 Added colors !
veltr
parents:
diff changeset
    30
        });
170
603ffa4c6fa5 correct ";" and "," in javascripts
ymh <ymh.work@gmail.com>
parents: 116
diff changeset
    31
    };
52
e0f6f3c31150 Added colors !
veltr
parents:
diff changeset
    32
    var _save = function() {
e0f6f3c31150 Added colors !
veltr
parents:
diff changeset
    33
        _proj.set("saved_at", new Date());
e0f6f3c31150 Added colors !
veltr
parents:
diff changeset
    34
        var _data = _proj.toJSON();
e0f6f3c31150 Added colors !
veltr
parents:
diff changeset
    35
        Rkns.$.ajax({
e0f6f3c31150 Added colors !
veltr
parents:
diff changeset
    36
            type: _opts.http_method,
e0f6f3c31150 Added colors !
veltr
parents:
diff changeset
    37
            url: _opts.url,
e0f6f3c31150 Added colors !
veltr
parents:
diff changeset
    38
            contentType: "application/json",
e0f6f3c31150 Added colors !
veltr
parents:
diff changeset
    39
            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
    40
            beforeSend: function(){
443
4c7ab16e5845 best coding convention for loadingstatus and savingstatus, rempve loadingatatus form export
ymh <ymh.work@gmail.com>
parents: 368
diff changeset
    41
            	_proj.set({saveStatus:2});
357
70e577b0cdc6 add save_status var to specify the status of the renkan (saved/to-save/saving)
rougeronj
parents: 320
diff changeset
    42
            },
52
e0f6f3c31150 Added colors !
veltr
parents:
diff changeset
    43
            success: function(data, textStatus, jqXHR) {
e0f6f3c31150 Added colors !
veltr
parents:
diff changeset
    44
                $(window).off("beforeunload", _onLeave);
e0f6f3c31150 Added colors !
veltr
parents:
diff changeset
    45
                _saveWarn = false;
443
4c7ab16e5845 best coding convention for loadingstatus and savingstatus, rempve loadingatatus form export
ymh <ymh.work@gmail.com>
parents: 368
diff changeset
    46
                _proj.set({saveStatus:0});
320
154b121a43f1 enhance save on click (snapshot mode) and update django app
cavaliet
parents: 293
diff changeset
    47
                //document.location.hash = "#id=" + data.id;
154b121a43f1 enhance save on click (snapshot mode) and update django app
cavaliet
parents: 293
diff changeset
    48
                //$(".Rk-Notifications").text("Saved as "+document.location.href).fadeIn().delay(2000).fadeOut();
52
e0f6f3c31150 Added colors !
veltr
parents:
diff changeset
    49
            }
e0f6f3c31150 Added colors !
veltr
parents:
diff changeset
    50
        });
170
603ffa4c6fa5 correct ";" and "," in javascripts
ymh <ymh.work@gmail.com>
parents: 116
diff changeset
    51
    };
52
e0f6f3c31150 Added colors !
veltr
parents:
diff changeset
    52
    var _checkLeave = function() {
443
4c7ab16e5845 best coding convention for loadingstatus and savingstatus, rempve loadingatatus form export
ymh <ymh.work@gmail.com>
parents: 368
diff changeset
    53
    	_proj.set({saveStatus:1});
4c7ab16e5845 best coding convention for loadingstatus and savingstatus, rempve loadingatatus form export
ymh <ymh.work@gmail.com>
parents: 368
diff changeset
    54
52
e0f6f3c31150 Added colors !
veltr
parents:
diff changeset
    55
        var title = _proj.get("title");
e0f6f3c31150 Added colors !
veltr
parents:
diff changeset
    56
        if (title && _proj.get("nodes").length) {
e0f6f3c31150 Added colors !
veltr
parents:
diff changeset
    57
            $(".Rk-Save-Button").removeClass("disabled");
e0f6f3c31150 Added colors !
veltr
parents:
diff changeset
    58
        } else {
e0f6f3c31150 Added colors !
veltr
parents:
diff changeset
    59
            $(".Rk-Save-Button").addClass("disabled");
e0f6f3c31150 Added colors !
veltr
parents:
diff changeset
    60
        }
e0f6f3c31150 Added colors !
veltr
parents:
diff changeset
    61
        if (title) {
e0f6f3c31150 Added colors !
veltr
parents:
diff changeset
    62
            $(".Rk-PadTitle").css("border-color","#333333");
e0f6f3c31150 Added colors !
veltr
parents:
diff changeset
    63
        }
e0f6f3c31150 Added colors !
veltr
parents:
diff changeset
    64
        if (!_saveWarn) {
e0f6f3c31150 Added colors !
veltr
parents:
diff changeset
    65
            _saveWarn = true;
e0f6f3c31150 Added colors !
veltr
parents:
diff changeset
    66
            $(window).on("beforeunload", _onLeave);
e0f6f3c31150 Added colors !
veltr
parents:
diff changeset
    67
        }
170
603ffa4c6fa5 correct ";" and "," in javascripts
ymh <ymh.work@gmail.com>
parents: 116
diff changeset
    68
    };
52
e0f6f3c31150 Added colors !
veltr
parents:
diff changeset
    69
    _load();
e0f6f3c31150 Added colors !
veltr
parents:
diff changeset
    70
    _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
    71
	    _model.on("change remove", function(_model) {
443
4c7ab16e5845 best coding convention for loadingstatus and savingstatus, rempve loadingatatus form export
ymh <ymh.work@gmail.com>
parents: 368
diff changeset
    72
	    	if(!(_model.changedAttributes.length === 1 && _model.hasChanged('saveStatus'))) {
357
70e577b0cdc6 add save_status var to specify the status of the renkan (saved/to-save/saving)
rougeronj
parents: 320
diff changeset
    73
	    		_checkLeave();
70e577b0cdc6 add save_status var to specify the status of the renkan (saved/to-save/saving)
rougeronj
parents: 320
diff changeset
    74
	    	}
70e577b0cdc6 add save_status var to specify the status of the renkan (saved/to-save/saving)
rougeronj
parents: 320
diff changeset
    75
	    });
443
4c7ab16e5845 best coding convention for loadingstatus and savingstatus, rempve loadingatatus form export
ymh <ymh.work@gmail.com>
parents: 368
diff changeset
    76
		if(!(_proj.changedAttributes.length === 1 && _proj.hasChanged('saveStatus'))) {
357
70e577b0cdc6 add save_status var to specify the status of the renkan (saved/to-save/saving)
rougeronj
parents: 320
diff changeset
    77
		    _checkLeave();
70e577b0cdc6 add save_status var to specify the status of the renkan (saved/to-save/saving)
rougeronj
parents: 320
diff changeset
    78
    	}
52
e0f6f3c31150 Added colors !
veltr
parents:
diff changeset
    79
    });
190
a9040a7c47d9 Some changes for standalone app compatibility
veltr
parents: 170
diff changeset
    80
    _renkan.renderer.save = function() {
a9040a7c47d9 Some changes for standalone app compatibility
veltr
parents: 170
diff changeset
    81
        if ($(".Rk-Save-Button").hasClass("disabled")) {
52
e0f6f3c31150 Added colors !
veltr
parents:
diff changeset
    82
            if (!_proj.get("title")) {
e0f6f3c31150 Added colors !
veltr
parents:
diff changeset
    83
                $(".Rk-PadTitle").css("border-color","#ff0000");
e0f6f3c31150 Added colors !
veltr
parents:
diff changeset
    84
            }
e0f6f3c31150 Added colors !
veltr
parents:
diff changeset
    85
        } else {
e0f6f3c31150 Added colors !
veltr
parents:
diff changeset
    86
            _save();
e0f6f3c31150 Added colors !
veltr
parents:
diff changeset
    87
        }
293
fba23fde14ba Correct jshint errors and force it on build
ymh <ymh.work@gmail.com>
parents: 190
diff changeset
    88
    };
170
603ffa4c6fa5 correct ";" and "," in javascripts
ymh <ymh.work@gmail.com>
parents: 116
diff changeset
    89
};