|
2 | 2 |
|
3 | 3 | var smapi = smapi || {}; |
4 | 4 |
|
5 | | -/** |
6 | | - * Manages the logic for line range selections. |
7 | | - * @param {int} maxLines The maximum number of lines in the content. |
8 | | - */ |
9 | | -smapi.LineNumberRange = function (maxLines) { |
10 | | - var self = this; |
11 | | - |
12 | | - /** |
13 | | - * @var {int} minLine The first line in the selection, or null if no lines selected. |
14 | | - */ |
15 | | - self.minLine = null; |
16 | | - |
17 | | - /** |
18 | | - * @var {int} maxLine The last line in the selection, or null if no lines selected. |
19 | | - */ |
20 | | - self.maxLine = null; |
21 | | - |
22 | | - /** |
23 | | - * Parse line numbers from a URL hash. |
24 | | - * @param {string} hash the URL hash to parse. |
25 | | - */ |
26 | | - self.parseFromUrlHash = function (hash) { |
27 | | - self.minLine = null; |
28 | | - self.maxLine = null; |
29 | | - |
30 | | - // parse hash |
31 | | - var hashParts = hash.match(/^#L(\d+)(?:-L(\d+))?$/); |
32 | | - if (!hashParts || hashParts.length <= 1) |
33 | | - return; |
34 | | - |
35 | | - // extract min/max lines |
36 | | - self.minLine = parseInt(hashParts[1]); |
37 | | - self.maxLine = parseInt(hashParts[2]) || self.minLine; |
38 | | - }; |
39 | | - |
40 | | - /** |
41 | | - * Generate a URL hash for the current line range. |
42 | | - * @returns {string} The generated URL hash. |
43 | | - */ |
44 | | - self.buildHash = function () { |
45 | | - if (!self.minLine) |
46 | | - return ""; |
47 | | - else if (self.minLine === self.maxLine) |
48 | | - return "#L" + self.minLine; |
49 | | - else |
50 | | - return "#L" + self.minLine + "-L" + self.maxLine; |
51 | | - } |
52 | | - |
53 | | - /** |
54 | | - * Get a list of all selected lines. |
55 | | - * @returns {Array<int>} The selected line numbers. |
56 | | - */ |
57 | | - self.getLinesSelected = function () { |
58 | | - // format |
59 | | - if (!self.minLine) |
60 | | - return []; |
61 | | - |
62 | | - var lines = []; |
63 | | - for (var i = self.minLine; i <= self.maxLine; i++) |
64 | | - lines.push(i); |
65 | | - return lines; |
66 | | - }; |
67 | | - |
68 | | - return self; |
69 | | -}; |
70 | | - |
71 | 5 | /** |
72 | 6 | * UI logic for the JSON validator page. |
73 | 7 | * @param {string} urlFormat The URL format for a file, with $schemaName and $id placeholders. |
74 | 8 | * @param {string} fileId The file ID for the content being viewed, if any. |
75 | 9 | */ |
76 | 10 | smapi.jsonValidator = function (urlFormat, fileId) { |
77 | | - /** |
78 | | - * The original content element. |
79 | | - */ |
80 | | - var originalContent = $("#raw-content").clone(); |
81 | | - |
82 | | - /** |
83 | | - * The currently highlighted lines. |
84 | | - */ |
85 | | - var selection = new smapi.LineNumberRange(); |
86 | | - |
87 | | - /** |
88 | | - * Rebuild the syntax-highlighted element. |
89 | | - */ |
90 | | - var formatCode = function () { |
91 | | - // reset if needed |
92 | | - $(".sunlight-container").replaceWith(originalContent.clone()); |
93 | | - |
94 | | - // apply default highlighting |
95 | | - Sunlight.highlightAll({ |
96 | | - lineHighlight: selection.getLinesSelected() |
97 | | - }); |
98 | | - |
99 | | - // fix line links |
100 | | - $(".sunlight-line-number-margin a").each(function () { |
101 | | - var link = $(this); |
102 | | - var lineNumber = parseInt(link.text()); |
103 | | - link |
104 | | - .attr("id", "L" + lineNumber) |
105 | | - .attr("href", "#L" + lineNumber) |
106 | | - .removeAttr("name") |
107 | | - .data("line-number", lineNumber); |
108 | | - }); |
109 | | - }; |
110 | | - |
111 | | - /** |
112 | | - * Scroll the page so the selected range is visible. |
113 | | - */ |
114 | | - var scrollToRange = function () { |
115 | | - if (!selection.minLine) |
116 | | - return; |
117 | | - |
118 | | - var targetLine = Math.max(1, selection.minLine - 5); |
119 | | - $("#L" + targetLine).get(0).scrollIntoView(); |
120 | | - }; |
121 | | - |
122 | 11 | /** |
123 | 12 | * Initialize the JSON validator page. |
124 | 13 | */ |
125 | 14 | var init = function () { |
126 | 15 | var input = $("#input"); |
127 | 16 |
|
128 | | - // set initial code formatting |
129 | | - selection.parseFromUrlHash(location.hash); |
130 | | - formatCode(); |
131 | | - scrollToRange(); |
132 | | - |
133 | | - // update code formatting on hash change |
134 | | - $(window).on("hashchange", function () { |
135 | | - selection.parseFromUrlHash(location.hash); |
136 | | - formatCode(); |
137 | | - scrollToRange(); |
138 | | - }); |
139 | | - |
140 | 17 | // change format |
141 | 18 | $("#output #format").on("change", function () { |
142 | 19 | var schemaName = $(this).val(); |
|
0 commit comments