1 | // UI
|
2 | (function() {
|
3 | function resize() {
|
4 | $('.viewport')
|
5 | .height($(window).height() - $('.header').height())
|
6 | .css('opacity', 1)
|
7 | return resize;
|
8 | }
|
9 | $(window).on('resize', resize(true));
|
10 |
|
11 | var navLinks = ['help', 'about', 'account']
|
12 | $.each(navLinks, function(_, className) {
|
13 | $('div.header')
|
14 | .on('click', 'a.' + className, function() {
|
15 | var viewport = $('.viewport')
|
16 | if (viewport.hasClass('viewport-' + className)) {
|
17 | viewport.removeClass('viewport-sidebar')
|
18 | $.each(navLinks, function(_, sibling) {
|
19 | viewport.removeClass('viewport-' + sibling)
|
20 | })
|
21 | return
|
22 | }
|
23 | $.each(navLinks, function(_, sibling) {
|
24 | viewport.removeClass('viewport-' + sibling)
|
25 | })
|
26 | viewport.addClass('viewport-sidebar')
|
27 | .addClass('viewport-' + className)
|
28 | })
|
29 | })
|
30 |
|
31 | switch (location.hash) {
|
32 | case '#help':
|
33 | $('a.help').click()
|
34 | break
|
35 | case '#about':
|
36 | $('a.about').click()
|
37 | break
|
38 | case '#account':
|
39 | $('a.account').click()
|
40 | break
|
41 | }
|
42 |
|
43 | // debug
|
44 | $('.template').css('background-color', Random.color())
|
45 | $('.result').css('background-color', Random.color())
|
46 | // $('.sidebar .help').css('background-color', Random.color())
|
47 | // $('.sidebar .about').css('background-color', Random.color())
|
48 | // $('.sidebar .content').append('<p>' + Random.paragraph() + '</p>')
|
49 | // $('.sidebar .content').append('<p>' + Random.paragraph() + '</p>')
|
50 |
|
51 | })();
|
52 |
|
53 | // Save, Tidy
|
54 | (function() {
|
55 | var tabSize = 4;
|
56 |
|
57 | // Init
|
58 | if (false && location.pathname.length > 1) {
|
59 | var id = location.pathname.slice(1)
|
60 | $.getJSON('/mock/item/' + id, function(data) {
|
61 | tplEditor.setValue(
|
62 | JSON.stringify(data.tpl, null, tabSize) || ''
|
63 | )
|
64 | $('.viewport textarea[name=tpl]').val(
|
65 | JSON.stringify(data.tpl, null, tabSize) || ''
|
66 | )
|
67 | })
|
68 | }
|
69 |
|
70 | // Save, Tidy
|
71 | $('div.header')
|
72 | .on('click', 'a.save', function(event) {
|
73 | if (!tplEditor.getValue()) return
|
74 |
|
75 | $.ajax({
|
76 | url: '/mock/save',
|
77 | data: {
|
78 | tpl: tplEditor.getValue()
|
79 | },
|
80 | success: function(data) {
|
81 | location.pathname = '/' + data.id
|
82 | }
|
83 | })
|
84 | })
|
85 | .on('click', 'a.tidy', function(event) {
|
86 | stringify(tplEditor.getValue())
|
87 | })
|
88 |
|
89 | function stringify(tpl) {
|
90 | tpl = js_beautify(tpl)
|
91 | // tpl = tpl.replace(/^([\r\n]*)/i, '')
|
92 | // .replace(/([\r\n]*$)/i, '')
|
93 | // tpl = new Function('return ' + tpl)
|
94 | // tpl = tpl() || ''
|
95 | // tpl = JSON.stringify(tpl, null, tabSize)
|
96 | tplEditor.setValue(tpl)
|
97 | }
|
98 |
|
99 | function render(tpl) {
|
100 | try {
|
101 | tpl = tpl.replace(/^([\r\n]*)/i, '')
|
102 | .replace(/([\r\n]*$)/i, '')
|
103 | tpl = new Function('return ' + tpl)
|
104 | tpl = tpl()
|
105 | } catch (error) {
|
106 | tpl = error.toString()
|
107 | }
|
108 |
|
109 | var data
|
110 | try {
|
111 | data = Mock.mock(tpl) || ''
|
112 | data = JSON.stringify(data, null, tabSize)
|
113 | } catch (error) {
|
114 | data = error.toString()
|
115 | }
|
116 | $('textarea[name=data]').val(data)
|
117 | dataEditor.setValue(data)
|
118 |
|
119 | var size = (function size(data) {
|
120 | var ma = encodeURIComponent(data).match(/%[89ABab]/g);
|
121 | return data.length + (ma ? ma.length : 0)
|
122 | })(data)
|
123 | size = (size / 1024).toFixed(2)
|
124 | $('span.badge').text(size + ' kB')
|
125 | }
|
126 |
|
127 | // Editor
|
128 | var tplTextarea = $('textarea[name=tpl]')
|
129 | var tplEditor = CodeMirror
|
130 | .fromTextArea(tplTextarea.get(0), {
|
131 | tabSize: tabSize,
|
132 | tabMode: 'spaces', // or 'shift'
|
133 | indentUnit: tabSize,
|
134 | matchBrackets: true,
|
135 | lineNumbers: true,
|
136 | lineWrapping: true,
|
137 | autofocus: true,
|
138 | mode: 'javascript',
|
139 | theme: 'neat'
|
140 | })
|
141 | tplEditor.on('change', function(instance) {
|
142 | render(instance.getValue())
|
143 | })
|
144 | tplEditor.on('scroll', function(instance) {
|
145 | var scrollInfo = tplEditor.getScrollInfo()
|
146 | var percent = scrollInfo.top / (scrollInfo.height - scrollInfo.clientHeight)
|
147 |
|
148 | var dataEditorScrollInfo = dataEditor.getScrollInfo()
|
149 | var dataEditorTop = (dataEditorScrollInfo.height - dataEditorScrollInfo.clientHeight) * percent
|
150 |
|
151 | dataEditor.scrollTo(dataEditorScrollInfo.left, dataEditorTop)
|
152 | })
|
153 |
|
154 | var dataEditor = CodeMirror
|
155 | .fromTextArea($('textarea[name=data]').get(0), {
|
156 | lineNumbers: true,
|
157 | autofocus: false,
|
158 | mode: 'javascript',
|
159 | theme: 'neat'
|
160 | })
|
161 |
|
162 | // Syntax Demo
|
163 | var EXAMPLE_SYNTAX = Mock.heredoc(function() {
|
164 | /*!
|
165 | {
|
166 | 'title': 'Syntax Demo',
|
167 |
|
168 | 'string1|1-10': '★',
|
169 | 'string2|3': 'value',
|
170 |
|
171 | 'number1|+1': 100,
|
172 | 'number2|1-100': 100,
|
173 | 'number3|1-100.1-10': 1,
|
174 | 'number4|123.1-10': 1,
|
175 | 'number5|123.3': 1,
|
176 | 'number6|123.10': 1.123,
|
177 |
|
178 | 'boolean1|1': true,
|
179 | 'boolean2|1-2': true,
|
180 |
|
181 | 'object1|2-4': {
|
182 | '110000': '北京市',
|
183 | '120000': '天津市',
|
184 | '130000': '河北省',
|
185 | '140000': '山西省'
|
186 | },
|
187 | 'object2|2': {
|
188 | '310000': '上海市',
|
189 | '320000': '江苏省',
|
190 | '330000': '浙江省',
|
191 | '340000': '安徽省'
|
192 | },
|
193 |
|
194 | 'array1|1': ['AMD', 'CMD', 'KMD', 'UMD'],
|
195 | 'array2|1-10': ['Mock.js'],
|
196 | 'array3|3': ['Mock.js'],
|
197 |
|
198 | 'function': function() {
|
199 | return this.title
|
200 | }
|
201 | }
|
202 | */
|
203 | })
|
204 | var EXAMPLE_PLACEHOLDER = Mock.heredoc(function() {
|
205 | /*!
|
206 | {
|
207 | basics: {
|
208 | boolean1: '@BOOLEAN',
|
209 | boolean2: '@BOOLEAN(1, 9, true)',
|
210 |
|
211 | natural1: '@NATURAL',
|
212 | natural2: '@NATURAL(10000)',
|
213 | natural3: '@NATURAL(60, 100)',
|
214 |
|
215 | integer1: '@INTEGER',
|
216 | integer2: '@INTEGER(10000)',
|
217 | integer3: '@INTEGER(60, 100)',
|
218 |
|
219 | float1: '@FLOAT',
|
220 | float2: '@FLOAT(0)',
|
221 | float3: '@FLOAT(60, 100)',
|
222 | float4: '@FLOAT(60, 100, 3)',
|
223 | float5: '@FLOAT(60, 100, 3, 5)',
|
224 |
|
225 | character1: '@CHARACTER',
|
226 | character2: '@CHARACTER("lower")',
|
227 | character3: '@CHARACTER("upper")',
|
228 | character4: '@CHARACTER("number")',
|
229 | character5: '@CHARACTER("symbol")',
|
230 | character6: '@CHARACTER("aeiou")',
|
231 |
|
232 | string1: '@STRING',
|
233 | string2: '@STRING(5)',
|
234 | string3: '@STRING("lower",5)',
|
235 | string4: '@STRING(7, 10)',
|
236 | string5: '@STRING("aeiou", 1, 3)',
|
237 |
|
238 | range1: '@RANGE(10)',
|
239 | range2: '@RANGE(3, 7)',
|
240 | range3: '@RANGE(1, 10, 2)',
|
241 | range4: '@RANGE(1, 10, 3)',
|
242 |
|
243 | date: '@DATE',
|
244 | time: '@TIME',
|
245 |
|
246 | datetime1: '@DATETIME',
|
247 | datetime2: '@DATETIME("yyyy-MM-dd A HH:mm:ss")',
|
248 | datetime3: '@DATETIME("yyyy-MM-dd a HH:mm:ss")',
|
249 | datetime4: '@DATETIME("yy-MM-dd HH:mm:ss")',
|
250 | datetime5: '@DATETIME("y-MM-dd HH:mm:ss")',
|
251 | datetime6: '@DATETIME("y-M-d H:m:s")',
|
252 |
|
253 | now: '@NOW',
|
254 | nowYear: '@NOW("year")',
|
255 | nowMonth: '@NOW("month")',
|
256 | nowDay: '@NOW("day")',
|
257 | nowHour: '@NOW("hour")',
|
258 | nowMinute: '@NOW("minute")',
|
259 | nowSecond: '@NOW("second")',
|
260 | nowWeek: '@NOW("week")',
|
261 | nowCustom: '@NOW("yyyy-MM-dd HH:mm:ss SS")'
|
262 | },
|
263 | image: {
|
264 | image1: '@IMAGE',
|
265 | image2: '@IMAGE("100x200", "#000")',
|
266 | image3: '@IMAGE("100x200", "#000", "hello")',
|
267 | image4: '@IMAGE("100x200", "#000", "#FFF", "hello")',
|
268 | image5: '@IMAGE("100x200", "#000", "#FFF", "png", "hello")',
|
269 |
|
270 | dataImage1: '@DATAIMAGE',
|
271 | dataImage2: '@DATAIMAGE("200x100")',
|
272 | dataImage3: '@DATAIMAGE("300x100", "Hello Mock.js!")'
|
273 | },
|
274 | color: {
|
275 | color: '@COLOR',
|
276 | render: function(){
|
277 | $('.header').css('background', this.color)
|
278 | }
|
279 | },
|
280 | text: {
|
281 | title1: '@TITLE',
|
282 | title2: '@TITLE(5)',
|
283 | title3: '@TITLE(3, 5)',
|
284 |
|
285 | word1: '@WORD',
|
286 | word2: '@WORD(5)',
|
287 | word3: '@WORD(3, 5)',
|
288 |
|
289 | sentence1: '@SENTENCE',
|
290 | sentence2: '@SENTENCE(5)',
|
291 | sentence3: '@SENTENCE(3, 5)',
|
292 |
|
293 | paragraph1: '@PARAGRAPH',
|
294 | paragraph2: '@PARAGRAPH(2)',
|
295 | paragraph3: '@PARAGRAPH(1, 3)'
|
296 | },
|
297 | name: {
|
298 | first: '@FIRST',
|
299 | last: '@LAST',
|
300 | name1: '@NAME',
|
301 | name2: '@NAME(true)'
|
302 | },
|
303 | web: {
|
304 | url: '@URL',
|
305 | domain: '@DOMAIN',
|
306 | email: '@EMAIL',
|
307 | ip: '@IP',
|
308 | tld: '@TLD',
|
309 | },
|
310 | address: {
|
311 | area: '@AREA',
|
312 | region: '@REGION'
|
313 | },
|
314 | miscellaneous: {
|
315 | guid: '@GUID',
|
316 | id: '@ID',
|
317 | 'increment1|3': [
|
318 | '@INCREMENT'
|
319 | ],
|
320 | 'increment2|3': [
|
321 | '@INCREMENT(10)'
|
322 | ]
|
323 | },
|
324 | helpers: {
|
325 | capitalize1: '@CAPITALIZE()',
|
326 | capitalize2: '@CAPITALIZE("hello")',
|
327 |
|
328 | upper1: '@UPPER',
|
329 | upper2: '@UPPER("hello")',
|
330 |
|
331 | lower1: '@LOWER',
|
332 | lower2: '@LOWER("HELLO")',
|
333 |
|
334 | pick1: '@PICK',
|
335 | pick2: '@PICK("abc")',
|
336 | pick3: '@PICK(["a", "b", "c"])',
|
337 |
|
338 | shuffle1: '@SHUFFLE',
|
339 | shuffle2: '@SHUFFLE(["a", "b", "c"])'
|
340 | }
|
341 | }
|
342 | */
|
343 | })
|
344 |
|
345 | $('.viewport .help')
|
346 | .on('click', 'a.importSyntaxExample', function(event) {
|
347 | tplEditor.setValue(EXAMPLE_SYNTAX)
|
348 | })
|
349 | .on('click', 'a.importPlaceholderExample', function(event) {
|
350 | tplEditor.setValue(EXAMPLE_PLACEHOLDER)
|
351 | })
|
352 |
|
353 | tplEditor.setValue(EXAMPLE_SYNTAX)
|
354 |
|
355 | })()
|
356 |
|
357 | //
|
358 | ;
|
359 | (function() {})() |
\ | No newline at end of file |