').addClass('results');
23 |
24 | // Add new contents
25 | var $panel = $('
').addClass('client');
26 | $this.append($contents,
27 | $panel.append($inputContainer.append($inputList),
28 | $('
').append($submit, $status),
29 | $resultContainer.append($resultList)));
30 |
31 | // Create data, query, and result tabs
32 | var dataTabs = [], queryTab;
33 | if (!options.url) {
34 | $contents.find('.data').each(function () {
35 | var $this = $(this), dataUri = $this.attr('href') || $this.text();
36 | dataTabs.push(appendCodeTabFromUrl(dataUri, $inputList, $inputContainer, 'data'));
37 | });
38 | $contents.find('.query').each(function () {
39 | var $this = $(this), queryUri = $this.attr('href') || $this.text();
40 | $queryTab = appendCodeTabFromUrl(queryUri, $inputList, $inputContainer, 'query');
41 | });
42 | }
43 | else {
44 | location.hash.substr(1).split('&').forEach(function (part) {
45 | var keyvalue = part.match(/^([^=]+)=(.*)/),
46 | key = keyvalue && decodeURIComponent(keyvalue[1]),
47 | value = keyvalue && decodeURIComponent(keyvalue[2]);
48 | switch (key) {
49 | case 'data':
50 | dataTabs.push(appendCodeTabFromUrl(value, $inputList, $inputContainer, 'data'));
51 | break;
52 | case 'query':
53 | $queryTab = appendCodeTabFromUrl(value, $inputList, $inputContainer, 'query');
54 | break;
55 | }
56 | });
57 | }
58 | $inputContainer.tabs();
59 | $resultContainer.tabs().hide();
60 |
61 | // Hook up submit button
62 | $submit.button()
63 | .text(labels.execute)
64 | .click(showEyeResult);
65 |
66 | function showEyeResult() {
67 | // Collect data
68 | var data = [], query = $queryTab.val();
69 | $(dataTabs).each(function () {
70 | if (!$(this).hasClass('error'))
71 | data.push($(this).val());
72 | });
73 |
74 | // Execute EYE
75 | $status.text(labels.executing).removeClass('error');
76 | executeEye({
77 | path: options.path,
78 | data: data,
79 | query: query
80 | })
81 | // Create new result tab on success
82 | .done(function (n3) {
83 | var resultName = 'result ' + (++resultCount);
84 | $status.text(labels.success.replace('$', resultName));
85 | appendCodeTab(resultName,
86 | $resultList, $resultContainer, 'result')
87 | .text(n3);
88 | $resultContainer.tabs('refresh')
89 | .tabs('option', 'active', resultCount - 1)
90 | .slideDown();
91 | })
92 | // Display error detail on failure
93 | .fail(function (reason) {
94 | $status.text(labels.failure.replace('$', reason)).addClass('error');
95 | });
96 | }
97 | });
98 | };
99 |
100 | var executeEye = eye.executeEye = function (options) {
101 | return $.Deferred(function (deferred) {
102 | $.ajax({
103 | url: options.path,
104 | traditional: true,
105 | data: {
106 | 'data': options.data,
107 | 'query': options.query
108 | },
109 | cache: true,
110 | type: 'POST',
111 | })
112 | .done(function (n3) {
113 | if (n3.error)
114 | deferred.reject(n3.error);
115 | else
116 | deferred.resolve(n3.trimRight());
117 | })
118 | .fail(function (response) {
119 | if (response.responseText)
120 | deferred.reject(response.responseText.trim());
121 | else if (response.status)
122 | deferred.reject('HTTP error ' + response.status + ' \u2013 ' + response.statusText);
123 | else
124 | deferred.reject('Unknown HTTP error. Check your connection');
125 | });
126 | }).promise();
127 | };
128 |
129 | var labels = eye.labels = {
130 | input: 'Input',
131 | results: 'Results',
132 | execute: 'Execute EYE',
133 | executing: 'Executing EYE...',
134 | success: 'EYE generated $, displayed below.',
135 | failure: 'Error executing EYE: $.'
136 | };
137 |
138 | function appendCodeTab(title, $tabList, $tabContainer, cssClass) {
139 | var tabId = 'eyetab' + (++tabCount),
140 | $tabLink = $('
').addClass(cssClass)
141 | .append($('').attr('href', '#' + tabId)
142 | .text(title)),
143 | $tab = $('