summaryrefslogtreecommitdiff
path: root/src/client/devtools.js
blob: 9e0bd308489e54bcf229982611bb66467b194d21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
"use strict"

define(['jquery', 'rz_core', 'FileSaver'],
function($, rz_core, file_saver) {

    var import_file_set;

    $('debug-view__save').click(function(){
        var json = rz_core.main_graph.save_to_json();
        console.log('saving to local storage ' + json.length + ' bytes');
        localStorage.setItem(key, json);
    });

    $('#debug-view__export_btn').click(function() {
        var json = rz_core.main_graph.save_to_json(),
            blob = new Blob([json], {type: 'application/json'}),
            now = new Date(),
            date_string = ('' + now.getYear() + now.getMonth() + now.getDay() + '-' +
                now.getHours() + now.getMinutes() + now.getSeconds()),
            filename = rz_config.rzdoc_cur__name + '-' + date_string + '.json';

        console.log('saving ' + json.length + ' bytes to ' + filename);
        file_saver(blob, filename);
    });

    $('#debug-view__url-copy a').click(function() {
        var json = rz_core.main_graph.save_to_json();
        // TODO use jquery BBQ $.param({json: json});
        var encoded = document.location.origin + '/?json=' + encodeURIComponent(json);
        window.prompt('Copy to clipboard: Ctrl-C, Enter (or Cmd-C for Mac)', encoded);
    });

    var confirm_import = function() {
      if (!rz_core.main_graph.empty()) {
        return confirm('Current work will be merged with the new import, are you sure?');
      }
      return true;
    }

    function import_file(file) {

        var file_reader;

        console.log(file);
        file_reader = new FileReader();
        file_reader.onload = (function(theFile) {
            return function(e) {
                var result = e.target.result;
                if (e.target.readyState === FileReader.DONE) {
                    console.log('file-import: done reading \'' + theFile.name + '\', byte count: ' + result.length);
                    rz_core.load_from_json(result);
                }
            }
        })(file);
        file_reader.readAsText(file, "text/javascript");
    }

    function init_import_handlers() {

        /*
         * step1: propagate import file selection to hidden input element
         */
        $('#devtools__import__select-files-btn').click(function(event) {

            $('#debug-view__import__input_element').click(); // trigger file selection dialog

        });

        /*
         * step2: handle file selection events, enable import-execution button
         */
        $('#debug-view__import__input_element').on('change', function(event) {

            import_file_set = event.target.files;

            if (undefined === import_file_set) {
                return;
            }

            $('#debug-view__import__file-list').children().remove();
            $.map(import_file_set, function(file){

                var e_file_path;

                e_file_path = $('<div>');
                e_file_path.addClass('debug-view__import__input_file_name')
                e_file_path.text('- ' + file.name);
                $('#debug-view__import__file-list').append(e_file_path);
            });

            $('#devtools__import__step__export').show(); // enable import exec button
        });

        /*
         * step3: prompt for confirmation & execute import
         */
        $('#devtools__import__execute-import-btn').click(function(event) {
            if (!confirm_import()) {
                return;
            }

            $.map(import_file_set, import_file);
        });
    };
    init_import_handlers();

    $('debug-view__local-storage-load').click(function(){
      if (!confirm_import()) {
          return;
      }
      var json_blob = localStorage.getItem(key)
      rz_core.load_from_json(json_blob);
    });

    return {
        
    }
});