Project

General

Profile

1
$(function() {
2
    // code editor
3
    altair_code_editor.init();
4
});
5

    
6
var $codeEditor =  $("#codeEditor"),
7
    file_list_class = '.file_list';
8

    
9
altair_code_editor = {
10
    init: function() {
11
        altair_code_editor.copy_list_sidebar();
12

    
13
        altair_code_editor.init_code_editor();
14
        altair_code_editor.distraction_free_mode();
15
        altair_code_editor.change_theme();
16
    },
17
    init_code_editor: function() {
18
        cEditor = CodeMirror.fromTextArea(document.getElementById("codeEditor"), {
19
            lineNumbers: true,
20
            mode: 'text/html',
21
            matchTags: {
22
                bothTags: true
23
            },
24
            extraKeys: {
25
                "F11": function(cm) {
26
                    cm.setOption("fullScreen", !cm.getOption("fullScreen"));
27
                },
28
                "Esc": function(cm) {
29
                    if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false);
30
                }
31
            }
32
        });
33

    
34
        // open file from server and set content of Code Editor
35
        function loadFile(file,mode,callback) {
36
            // show preloader
37
            altair_helpers.content_preloader_show('regular',null,'.md-card-single');
38
            $codeEditor.next('.CodeMirror').velocity({
39
                opacity: 0
40
            }, {
41
                duration: 400,
42
                complete: function () {
43
                    $.ajax({
44
                        url : file,
45
                        dataType: "text",
46
                        success : function (data) {
47
                            if(mode == 'html') {
48
                                cEditor.setOption("mode","text/html");
49
                                cEditor.setOption("matchTags",{
50
                                    bothTags: true
51
                                });
52
                            }
53
                            if(mode == 'js') {
54
                                cEditor.setOption("mode","text/javascript");
55
                                cEditor.setOption("matchBrackets",true);
56
                            }
57
                            if(mode == 'php') {
58
                                cEditor.setOption("mode","application/x-httpd-php");
59
                                cEditor.setOption("matchTags",false);
60
                                cEditor.setOption("matchBrackets",true);
61
                            }
62
                            if(mode == 'json') {
63
                                cEditor.setOption("mode","application/json");
64
                            }
65
                            // set new content
66
                            cEditor.setValue(data);
67
                            // callback
68
                            if (typeof callback == "function") callback();
69
                            setTimeout(function() {
70
                                // hide preloader
71
                                altair_helpers.content_preloader_hide();
72
                            },280);
73
                            // show code editor
74
                            $codeEditor.next('.CodeMirror').velocity('reverse');
75
                        }
76
                    });
77
                }
78
            });
79

    
80
        }
81

    
82
        $(file_list_class).find('a').on('click',function(e) {
83
            e.preventDefault();
84

    
85

    
86
            var $this = $(this),
87
                this_file = $this.attr('data-editor-file'),
88
                this_mode = $this.attr('data-editor-mode');
89

    
90
            // check if file is already loaded into code editor
91
            if($this.closest('li').hasClass('md-list-item-active')) {
92
                return;
93
            }
94

    
95
            var callback = function() {
96
                $this.closest('li')
97
                    .addClass('md-list-item-active')
98
                    .siblings('li').removeClass('md-list-item-active');
99
            };
100

    
101
            loadFile('data/codemirror/file_content.php?file='+this_file,this_mode,callback);
102

    
103
        });
104

    
105
    },
106
    distraction_free_mode: function() {
107
        $codeEditor
108
            .next('.CodeMirror').append('<button class="uk-modal-close uk-close uk-close-alt" type="button" id="code_editor_close"></button>')
109
            // close fullscreen
110
            .on('click','#code_editor_close',function() {
111
                cEditor.setOption("fullScreen", false);
112
            });
113

    
114
        $('#code_editor_fullscreen').on('click', function(e) {
115
            e.preventDefault();
116
            cEditor.setOption("fullScreen", true);
117
        });
118
    },
119
    change_theme: function() {
120
        // select theme
121
        function selectTheme(theme) {
122
            cEditor.setOption("theme", theme);
123
        }
124

    
125
        $("#code_editor_theme").find('a').click(function(e) {
126
            e.preventDefault();
127
            var thisTheme = $(this).attr('data-editor-theme');
128

    
129
            selectTheme(thisTheme);
130

    
131
            $(this)
132
                .closest('li').addClass('uk-active')
133
                .siblings('li').removeClass('uk-active');
134

    
135
        });
136
    },
137
    copy_list_sidebar: function() {
138
        // hide secondary sidebar toggle for large screens
139
        $sidebar_secondary_toggle.addClass('uk-hidden-large');
140

    
141
        var file_list_sidebar = $(file_list_class).clone();
142

    
143
        file_list_sidebar.attr('id','file_list_sidebar');
144

    
145
        $sidebar_secondary
146
            .find('.sidebar_secondary_wrapper').html(file_list_sidebar);
147

    
148
    }
149
};
(79-79/114)