File size: 5,415 Bytes
16a0ca6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
{% extends "base.html" %}

{% block title %}Create Script - PyRunner{% endblock %}

{% block content %}
<div class="flex">
    {% include "cpanel/_sidebar.html" %}

    <div class="flex-1 p-8">
        <!-- Header -->
        <div class="mb-8">
            <nav class="flex items-center space-x-2 text-sm text-code-muted mb-4">
                <a href="{% url 'cpanel:script_list' %}" class="hover:text-code-accent">Scripts</a>
                <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5l7 7-7 7"/>
                </svg>
                <span class="text-code-text">Create</span>
            </nav>
            <h1 class="text-2xl font-bold text-code-text">Create New Script</h1>
            <p class="text-code-muted">Write and configure a new Python script</p>
        </div>

        <!-- Form with Two-Column Layout -->
        <form method="post">
            {% csrf_token %}

            <div class="grid grid-cols-1 lg:grid-cols-12 gap-6">
                <!-- Main Column: Name, Description, Code Editor -->
                <div class="lg:col-span-7">
                    {% include "cpanel/scripts/_form_main.html" %}
                </div>

                <!-- Sidebar: Properties and Settings -->
                <div class="lg:col-span-5">
                    {% include "cpanel/scripts/_form_sidebar.html" %}
                </div>
            </div>
        </form>
    </div>
</div>
{% endblock %}

{% block extra_scripts %}
<script src="https://cdn.jsdelivr.net/npm/monaco-editor@0.45.0/min/vs/loader.js"></script>
<script>
// Collapsible sections toggle
function toggleSection(sectionId) {
    const content = document.getElementById(sectionId + '-content');
    const chevron = document.getElementById(sectionId + '-chevron');

    if (content && chevron) {
        content.classList.toggle('hidden');
        chevron.classList.toggle('rotate-180');
    }
}

// Schedule options toggle
function toggleScheduleOptions(mode) {
    const intervalOptions = document.getElementById('interval-options');
    const dailyOptions = document.getElementById('daily-options');

    if (intervalOptions) {
        intervalOptions.classList.toggle('hidden', mode !== 'interval');
    }
    if (dailyOptions) {
        dailyOptions.classList.toggle('hidden', mode !== 'daily');
    }

    // Update radio button styles
    document.querySelectorAll('input[name="run_mode"]').forEach(radio => {
        const label = radio.closest('label');
        const dot = label.querySelector('span > span');

        if (radio.checked) {
            label.classList.add('border-code-accent', 'bg-code-accent/10');
            label.classList.remove('border-code-border');
            label.querySelector('span').classList.add('border-code-accent');
            label.querySelector('span').classList.remove('border-code-border');
            if (!dot) {
                const indicator = label.querySelector('span.w-4');
                indicator.innerHTML = '<span class="w-2 h-2 rounded-full bg-code-accent"></span>';
            }
        } else {
            label.classList.remove('border-code-accent', 'bg-code-accent/10');
            label.classList.add('border-code-border');
            label.querySelector('span').classList.remove('border-code-accent');
            label.querySelector('span').classList.add('border-code-border');
            const indicator = label.querySelector('span.w-4');
            indicator.innerHTML = '';
        }
    });
}

// Monaco Editor initialization
require.config({ paths: { vs: 'https://cdn.jsdelivr.net/npm/monaco-editor@0.45.0/min/vs' } });

require(['vs/editor/editor.main'], function () {
    const textarea = document.getElementById('id_code');
    const container = document.getElementById('code-editor-container');

    if (!textarea || !container) {
        console.error('Editor elements not found');
        return;
    }

    textarea.style.display = 'none';

    const editor = monaco.editor.create(container, {
        value: textarea.value,
        language: 'python',
        theme: 'vs-dark',
        fontSize: 14,
        fontFamily: "'JetBrains Mono', 'Fira Code', 'Consolas', monospace",
        minimap: { enabled: false },
        scrollBeyondLastLine: false,
        lineNumbers: 'on',
        tabSize: 4,
        insertSpaces: true,
        automaticLayout: true
    });

    editor.onDidChangeModelContent(function () {
        textarea.value = editor.getValue();
    });
});

// Tag selection handling
document.querySelectorAll('.tag-label').forEach(label => {
    label.addEventListener('click', function(e) {
        if (e.target.tagName === 'INPUT') return;
        e.preventDefault();  // Prevent browser's default toggle (input is inside label)
        const checkbox = this.querySelector('input[type="checkbox"]');
        checkbox.checked = !checkbox.checked;
        updateTagStyle(this, checkbox.checked);
    });
});

function updateTagStyle(label, isChecked) {
    if (isChecked) {
        label.classList.add('border-code-accent', 'bg-code-accent/20');
        label.classList.remove('border-code-border', 'bg-code-bg');
    } else {
        label.classList.remove('border-code-accent', 'bg-code-accent/20');
        label.classList.add('border-code-border', 'bg-code-bg');
    }
}
</script>
{% endblock %}