server/src/assets/js/mtdc-save.js
changeset 90 00c9bb0f6f37
equal deleted inserted replaced
89:4196849b15d3 90:00c9bb0f6f37
       
     1 /* globals Rkns */
       
     2 /* Saves the Full JSON at each modification */
       
     3 
       
     4 Rkns.mtdcJsonIO = function(_renkan, _opts) {
       
     5     var _proj = _renkan.project;
       
     6     if (typeof _opts.http_method === 'undefined') {
       
     7         _opts.http_method = 'PUT';
       
     8     }
       
     9     var _load = function() {
       
    10         _proj.set({
       
    11             loadingStatus : true
       
    12         });
       
    13         Rkns.$.getJSON(_opts.url, function(_data) {
       
    14             _renkan.dataloader.load(_data);
       
    15             _renkan.setCurrentUser(_opts.user_id, _opts.user_name);
       
    16             _proj.set({
       
    17                 loadingStatus : false
       
    18             });
       
    19             _proj.set({
       
    20                 saveStatus : 0
       
    21             });
       
    22             _proj.on('add:nodes add:edges add:users add:views', function(_model) {
       
    23                 _model.on('change remove', function() {
       
    24                     _thrSave();
       
    25                 });
       
    26                 _thrSave();
       
    27             });
       
    28             _proj.on('change', function() {
       
    29                 if (!(_proj.changedAttributes.length === 1 && _proj
       
    30                       .hasChanged('saveStatus'))) {
       
    31                     _thrSave();
       
    32                 }
       
    33             });
       
    34             _proj.get('nodes').each(function(_node) {
       
    35                 _node.on('change remove', function() {
       
    36                     _thrSave();
       
    37                 });
       
    38             });
       
    39             _proj.get('edges').each(function(_edge) {
       
    40                 _edge.on('change remove', function() {
       
    41                     _thrSave();
       
    42                 });
       
    43             });
       
    44             _proj.get('users').each(function(_user) {
       
    45                 _user.on('change remove', function() {
       
    46                     _thrSave();
       
    47                 });
       
    48             });
       
    49 
       
    50         }).fail(function(jqXHR, textStatus) {
       
    51             _showErrorModal(jqXHR, textStatus);
       
    52         });
       
    53     };
       
    54     var _save = function() {
       
    55         _proj.set({
       
    56             saveStatus : 2
       
    57         });
       
    58         var _data = _proj.toJSON();
       
    59         if (!_renkan.read_only) {
       
    60             Rkns.$.ajax({
       
    61                 type : _opts.http_method,
       
    62                 url : _opts.url,
       
    63                 contentType : 'application/json',
       
    64                 data : JSON.stringify(_data),
       
    65                 success : function(data) {
       
    66                     _proj.set({
       
    67                         saveStatus : 0,
       
    68                         updated : data.updated
       
    69                     });
       
    70                 },
       
    71                 error: function (jqXHR, textStatus, errorThrown) {
       
    72                     _showErrorModal(jqXHR, textStatus, errorThrown);
       
    73                 }
       
    74             });
       
    75         }
       
    76 
       
    77     };
       
    78     var _thrSave = Rkns._.debounce(_save, 1000);
       
    79 
       
    80 
       
    81     Rkns.$('#renkanErrorReload').on('click', function(){
       
    82         location.reload();
       
    83     });
       
    84 
       
    85     var errorModal = document.getElementById('renkanErrorModal');
       
    86     var _showErrorModal = function(jqXHR) {
       
    87         errorModal.style.display = 'block';
       
    88         switch(jqXHR.status){
       
    89         case 400:
       
    90             var jsonResp = JSON.parse(jqXHR.responseText);
       
    91             if(jsonResp.hasOwnProperty('validation_timestamp')){
       
    92                 document.getElementById('400Error_timestamp').style.display = 'block';
       
    93             }
       
    94             else {
       
    95                 Rkns.$('#400Error_other').append(jqXHR.responseText);
       
    96                 document.getElementById('400Error_other').style.display = 'block';
       
    97             }
       
    98             break;
       
    99         case 403:
       
   100             document.getElementById('403Error').style.display = 'block';
       
   101             break;
       
   102         case 404:
       
   103             document.getElementById('404Error').style.display = 'block';
       
   104             break;
       
   105         case 500:
       
   106             Rkns.$('#500Error').append(jqXHR.responseText);
       
   107             document.getElementById('500Error').style.display = 'block';
       
   108             break;
       
   109         case 503:
       
   110             document.getElementById('503Error').style.display = 'block';
       
   111             break;
       
   112         }
       
   113     };
       
   114 
       
   115     _load();
       
   116 };