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
|
/* globals $ */
var smapi = smapi || {};
/**
* Manages the logic for line range selections.
* @param {int} maxLines The maximum number of lines in the content.
*/
smapi.LineNumberRange = function (maxLines) {
var self = this;
/**
* @var {int} minLine The first line in the selection, or null if no lines selected.
*/
self.minLine = null;
/**
* @var {int} maxLine The last line in the selection, or null if no lines selected.
*/
self.maxLine = null;
/**
* Parse line numbers from a URL hash.
* @param {string} hash the URL hash to parse.
*/
self.parseFromUrlHash = function (hash) {
self.minLine = null;
self.maxLine = null;
// parse hash
var hashParts = hash.match(/^#L(\d+)(?:-L(\d+))?$/);
if (!hashParts || hashParts.length <= 1)
return;
// extract min/max lines
self.minLine = parseInt(hashParts[1]);
self.maxLine = parseInt(hashParts[2]) || self.minLine;
};
/**
* Generate a URL hash for the current line range.
* @returns {string} The generated URL hash.
*/
self.buildHash = function() {
if (!self.minLine)
return "";
else if (self.minLine === self.maxLine)
return "#L" + self.minLine;
else
return "#L" + self.minLine + "-L" + self.maxLine;
}
/**
* Get a list of all selected lines.
* @returns {Array<int>} The selected line numbers.
*/
self.getLinesSelected = function() {
// format
if (!self.minLine)
return [];
var lines = [];
for (var i = self.minLine; i <= self.maxLine; i++)
lines.push(i);
return lines;
};
return self;
};
/**
* UI logic for the JSON validator page.
* @param {any} sectionUrl The base JSON validator page URL.
* @param {any} pasteID The Pastebin paste ID for the content being viewed, if any.
*/
smapi.jsonValidator = function (sectionUrl, pasteID) {
/**
* The original content element.
*/
var originalContent = $("#raw-content").clone();
/**
* The currently highlighted lines.
*/
var selection = new smapi.LineNumberRange();
/**
* Rebuild the syntax-highlighted element.
*/
var formatCode = function () {
// reset if needed
$(".sunlight-container").replaceWith(originalContent.clone());
// apply default highlighting
Sunlight.highlightAll({
lineHighlight: selection.getLinesSelected()
});
// fix line links
$(".sunlight-line-number-margin a").each(function() {
var link = $(this);
var lineNumber = parseInt(link.text());
link
.attr("id", "L" + lineNumber)
.attr("href", "#L" + lineNumber)
.removeAttr("name")
.data("line-number", lineNumber);
});
};
/**
* Scroll the page so the selected range is visible.
*/
var scrollToRange = function() {
if (!selection.minLine)
return;
var targetLine = Math.max(1, selection.minLine - 5);
$("#L" + targetLine).get(0).scrollIntoView();
};
/**
* Initialize the JSON validator page.
*/
var init = function () {
// set initial code formatting
selection.parseFromUrlHash(location.hash);
formatCode();
scrollToRange();
// update code formatting on hash change
$(window).on("hashchange", function() {
selection.parseFromUrlHash(location.hash);
formatCode();
scrollToRange();
});
// change format
$("#output #format").on("change", function() {
var schemaName = $(this).val();
location.href = new URL(schemaName + "/" + pasteID, sectionUrl).toString();
});
// upload form
var input = $("#input");
if (input.length) {
// disable submit if it's empty
var toggleSubmit = function () {
var hasText = !!input.val().trim();
submit.prop("disabled", !hasText);
};
input.on("input", toggleSubmit);
toggleSubmit();
// drag & drop file
input.on({
'dragover dragenter': function (e) {
e.preventDefault();
e.stopPropagation();
},
'drop': function (e) {
var dataTransfer = e.originalEvent.dataTransfer;
if (dataTransfer && dataTransfer.files.length) {
e.preventDefault();
e.stopPropagation();
var file = dataTransfer.files[0];
var reader = new FileReader();
reader.onload = $.proxy(function (file, $input, event) {
$input.val(event.target.result);
toggleSubmit();
}, this, file, $("#input"));
reader.readAsText(file);
}
}
});
}
};
init();
};
|