Completed
Push — master ( 96d3da...cb201a )
by Andres
28s
created

options.js ➔ ... ➔ ct.importSave   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 4
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
nop 1
1
/* globals confirm, atob, btoa, alert */
2
/**
3
 options
4
 Component that handles the options of the game.
5
6
 @namespace Components
7
 */
8
'use strict';
9
10
angular.module('game').component('options', {
11
  templateUrl: 'views/options.html',
12
  controller: ['$window', 'state', 'savegame', options],
13
  controllerAs: 'ct'
14
});
15
16
function options($window, state, savegame) {
17
  let ct = this;
18
  ct.state = state;
19
20
  ct.reset = function(ask) {
21
    let confirmation = true;
22
    if (ask) {
23
      confirmation = confirm('Are you sure you want to reset? This will permanently erase your progress.');
24
    }
25
26
    if (confirmation === true) {
27
      localStorage.removeItem('player');
28
      savegame.initSave();
29
    }
30
  };
31
32
  ct.importSave = function(player) {
33
    if (state.export) {
34
      try {
35
        state.player = JSON.parse(atob(state.export));
36
        savegame.versionControl();
37
      } catch (error) {
38
        alert('Invalid save file.');
39
      }
40
    }
41
  };
42
43
  ct.exportSave = function(player) {
44
    let exportText = btoa(JSON.stringify(player));
45
    let blob = new Blob([exportText], {
0 ignored issues
show
Bug introduced by
The variable Blob seems to be never declared. If this is a global, consider adding a /** global: Blob */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
46
      type: 'text/plain'
47
    });
48
    let url = $window.URL || $window.webkitURL;
49
    let fileUrl = url.createObjectURL(blob);
50
    let a = document.createElement('a');
51
    a.href = fileUrl;
52
    a.download = "nucleogenesis-save.txt";
53
    a.click();
54
    url.revokeObjectURL(fileUrl);
55
  };
56
}
57