Passed
Push — master ( 9a2ec2...2680cb )
by Lucas
49s
created

lib/devmind-beautifier.js   A

Complexity

Total Complexity 10
Complexity/F 1.67

Size

Lines of Code 84
Function Count 6

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 0
c 1
b 0
f 0
nc 1
dl 0
loc 84
rs 10
wmc 10
mnd 2
bc 9
fnc 6
bpm 1.5
cpm 1.6666
noi 10
1
'use babel';
2
3
4
import DevmindBeautifierView from './devmind-beautifier-view';
5
import { CompositeDisposable } from 'atom';
6
7
export default {
8
9
10
  devmindBeautifierView: null,
11
  modalPanel: null,
12
  subscriptions: null,
13
14
  activate(state) {
15
    this.devmindBeautifierView = new DevmindBeautifierView(state.devmindBeautifierViewState);
16
    this.modalPanel = atom.workspace.addModalPanel({
17
      item: this.devmindBeautifierView.getElement(),
18
      visible: false
19
    });
20
21
    // Events subscribed to in atom's system can be easily cleaned up with a CompositeDisposable
22
    this.subscriptions = new CompositeDisposable();
23
24
    // Register command that toggles this view
25
    this.subscriptions.add(atom.commands.add('atom-workspace', {
26
      'devmind-beautifier:toggle': () => this.toggle()
27
    }));
28
  },
29
30
  deactivate() {
31
    this.modalPanel.destroy();
32
    this.subscriptions.dispose();
33
    this.devmindBeautifierView.destroy();
34
  },
35
36
  serialize() {
37
    return {
38
      devmindBeautifierViewState: this.devmindBeautifierView.serialize()
39
    };
40
  },
41
42
  toggle() {
43
44
    panel = this;
45
46
    editor = atom.workspace.getActiveTextEditor()
47
    text = editor.getText();
48
    fileName = editor.getTitle();
49
    ext = fileName.split('.').pop();
50
      panel.modalPanel.show()
51
      var http = new XMLHttpRequest();
52
      var url = "https://beautifier.devmind.io/format";
53
      var params = "extension="+ext+"&content="+text;
54
      http.open("POST", url, true);
55
56
  //Send the proper header information along with the request
57
  http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
58
59
  http.onreadystatechange = function() {//Call a function when the state changes.
60
      if(http.readyState == 4 && http.status == 200) {
61
62
        if(typeof http.responseText.status === "undefined")
63
        {
64
          editor.setText(http.responseText);
65
          atom.notifications.addSuccess('Now your code looks fancy');
66
          panel.modalPanel.hide()
67
        }
68
        else {
69
          atom.notifications.addInfo('File extension not supported. Yet.');
70
        }
71
      }
72
      if(http.status !== 200)
73
      {
74
        console.log(http.status)
75
          panel.modalPanel.hide()
76
          atom.notifications.addError('Something went wrong, please check your internet connection');
77
      }
78
  }
79
  http.send(params);
80
    }
81
82
83
84
85
  }
86
87
};
88