This repository has been archived by the owner on Apr 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinit.js
executable file
·179 lines (164 loc) · 5.61 KB
/
init.js
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
* Copyright (c) Codiad & Andr3as, distributed
* as-is and without warranty under the MIT License.
* See [root]/license.md for more information. This information must remain intact.
*/
(function(global, $){
var codiad = global.codiad,
scripts = document.getElementsByTagName('script'),
path = scripts[scripts.length-1].src.split('?')[0],
curpath = path.split('/').slice(0, -1).join('/')+'/';
$(function() {
codiad.CodeDate.init();
});
codiad.CodeDate = {
path: curpath,
init: function() {
var _this = this;
this.loadSettings();
$.getScript(this.path+"moment.js");
amplify.subscribe('active.onOpen', function(path){
_this.addKeyBindings();
});
amplify.subscribe('settings.dialog.save', function(){
if ($('#dateForm').length > 0) {
if ($('#dateForm').length > 0) {
codiad.CodeDate.closeDialog();
}
}
});
},
//////////////////////////////////////////////////////////
//
// Add key bindings
//
//////////////////////////////////////////////////////////
addKeyBindings: function(){
if (codiad.editor.getActive() !== null) {
var _this = this;
var _commandManager = codiad.editor.getActive().commands;
//clear Interval
window.clearInterval(this.bind);
_commandManager.addCommand({
name: 'CodeDate',
bindKey: {
"win": "Ctrl-5",
"mac": "Command-5"
},
exec: function () {
_this.insert(0);
}
});
_commandManager.addCommand({
name: 'CodeDate1',
bindKey: {
"win": "Ctrl-Shift-5",
"mac": "Command-Shift-5"
},
exec: function () {
_this.insert(1);
}
});
}
},
//////////////////////////////////////////////////////////
//
// Show Dialog to edit settings
//
//////////////////////////////////////////////////////////
showDialog: function() {
codiad.modal.load(400, this.path+"dialog.php");
},
//////////////////////////////////////////////////////////
//
// Enter the current settings into the input fields
//
//////////////////////////////////////////////////////////
showSettings: function() {
$('#firstDate').val(this.settings[0]);
$('#secondDate').val(this.settings[1]);
},
//////////////////////////////////////////////////////////
//
// Close settings dialog and save new settings
//
//////////////////////////////////////////////////////////
closeDialog: function() {
var first = $('#firstDate').val();
var second = $('#secondDate').val();
if (first !== "" && second !== "") {
this.saveSettings([first, second]);
}
},
//////////////////////////////////////////////////////////
//
// Save settings given as array in settings
//
// Parameters:
//
// settings - {Array} - Array of formattings
//
//////////////////////////////////////////////////////////
saveSettings: function(settings) {
var _this = this;
settings = JSON.stringify(settings);
$.post(this.path+"controller.php?action=saveSettings", {"settings": settings}, function(data){
data = JSON.parse(data);
if (data.status == "error") {
codiad.message.error(data.message);
} else {
codiad.message.success(data.message);
}
_this.loadSettings();
});
},
//////////////////////////////////////////////////////////
//
// Load setting from server
//
//////////////////////////////////////////////////////////
loadSettings: function() {
var _this = this;
$.getJSON(this.path+"settings.json", function(data){
_this.settings = data;
});
},
//////////////////////////////////////////////////////////
//
// Insert date/time
//
// Parameters:
//
// number - {Integer} - Index of formatting in settings array
//
//////////////////////////////////////////////////////////
insert: function(number) {
var dateStr = this.parse(this.settings[number]);
codiad.editor.insertText(dateStr);
},
//////////////////////////////////////////////////////////
//
// Parse current date/time by given formatting
//
// Parameters:
//
// string - {String} - date/time format
//
//////////////////////////////////////////////////////////
parse: function(string) {
return moment().format(string);
},
//////////////////////////////////////////////////////////
//
// Show a preview for given formats
//
//////////////////////////////////////////////////////////
preview: function() {
var first = $('#firstDate').val();
var second = $('#secondDate').val();
first = this.parse(first);
second = this.parse(second);
alert("1: "+first+"\n2: "+second);
}
};
})(this, jQuery);