assist.mako 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. ## Licensed to Cloudera, Inc. under one
  2. ## or more contributor license agreements. See the NOTICE file
  3. ## distributed with this work for additional information
  4. ## regarding copyright ownership. Cloudera, Inc. licenses this file
  5. ## to you under the Apache License, Version 2.0 (the
  6. ## "License"); you may not use this file except in compliance
  7. ## with the License. You may obtain a copy of the License at
  8. ##
  9. ## http://www.apache.org/licenses/LICENSE-2.0
  10. ##
  11. ## Unless required by applicable law or agreed to in writing, software
  12. ## distributed under the License is distributed on an "AS IS" BASIS,
  13. ## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. ## See the License for the specific language governing permissions and
  15. ## limitations under the License.
  16. <%!
  17. from desktop import conf
  18. from desktop.lib.i18n import smart_unicode
  19. from django.utils.translation import ugettext as _
  20. from desktop.views import _ko
  21. %>
  22. <%def name="assistPanel()">
  23. <style>
  24. .assist-entry,a {
  25. white-space: nowrap;
  26. }
  27. .assist-tables {
  28. overflow-y: hidden;
  29. overflow-x: auto;
  30. margin-left: 3px;
  31. }
  32. .assist-tables a {
  33. text-decoration: none;
  34. }
  35. .assist-tables li {
  36. list-style: none;
  37. }
  38. .assist-breadcrumb > a:hover {
  39. color: #338bb8;
  40. }
  41. .assist-tables > li {
  42. position: relative;
  43. padding-top: 2px;
  44. padding-bottom: 2px;
  45. padding-left: 4px;
  46. }
  47. .assist-tables > li.selected {
  48. background-color: #EEE;
  49. }
  50. .assist-breadcrumb {
  51. position:relative;
  52. left: 0;
  53. top: 0;
  54. right: 0;
  55. height:25px;
  56. padding-top: 0px;
  57. padding-left: 0px;
  58. }
  59. .assist-breadcrumb a {
  60. cursor: pointer;
  61. text-decoration: none;
  62. color: #737373;
  63. -webkit-transition: color 0.2s ease;
  64. -moz-transition: color 0.2s ease;
  65. -ms-transition: color 0.2s ease;
  66. transition: color 0.2s ease;
  67. }
  68. .assist-tables-counter {
  69. color: #d1d1d1;
  70. font-weight: normal;
  71. }
  72. .assist-table-link {
  73. font-size: 13px;
  74. }
  75. .assist-field-link {
  76. font-size: 12px;
  77. }
  78. .assist-table-link,
  79. .assist-table-link:focus {
  80. color: #444;
  81. }
  82. .assist-field-link,
  83. .assist-field-link:focus {
  84. white-space: nowrap;
  85. color: #737373;
  86. }
  87. .assist-actions {
  88. position:absolute;
  89. right: 0;
  90. padding-right:4px;
  91. padding-left:4px;
  92. background-color: #FFF;
  93. }
  94. .assist-tables > li.selected .assist-actions {
  95. background-color: #EEE;
  96. }
  97. .assist .nav-header {
  98. padding-top: 0 !important;
  99. margin-top: 0 !important;
  100. margin-right: 0 !important;
  101. padding-right: 4px !important;
  102. }
  103. </style>
  104. <script type="text/html" id="assist-no-database-entries">
  105. <ul class="assist-tables">
  106. <li>
  107. <span style="font-style: italic">${_('The database has no tables')}</span>
  108. </li>
  109. </ul>
  110. </script>
  111. <script type="text/html" id="assist-no-table-entries">
  112. <ul>
  113. <li>
  114. <span style="font-style: italic" class="assist-entry assist-field-link">${_('The table has no columns')}</span>
  115. </li>
  116. </ul>
  117. </script>
  118. <script type="text/html" id="assist-entry-actions">
  119. <div class="assist-actions" data-bind="css: { 'table-actions' : definition.isTable || definition.isView, 'column-actions': definition.isColumn, 'database-actions' : definition.isDatabase } " style="opacity: 0">
  120. <a class="inactive-action" href="javascript:void(0)" data-bind="visible: (definition.isTable || definition.isView) && navigationSettings.showPreview, click: showPreview"><i class="fa fa-list" title="${_('Preview Sample data')}"></i></a>
  121. <span data-bind="visible: navigationSettings.showStats, component: { name: 'table-stats', params: {
  122. statsVisible: statsVisible,
  123. sourceType: assistSource.type,
  124. snippet: assistSource.snippet,
  125. databaseName: databaseName,
  126. tableName: tableName,
  127. columnName: columnName,
  128. fieldType: definition.type,
  129. assistHelper: assistSource.assistHelper
  130. } }"></span>
  131. <a class="inactive-action" href="javascript:void(0)" data-bind="visible: navigationSettings.openItem, click: openItem"><i class="fa fa-long-arrow-right" title="${_('Open')}"></i></a>
  132. </div>
  133. </script>
  134. <script type="text/html" id="assist-entries">
  135. <!-- ko if: hasEntries() && ! loading() && filteredEntries().length == 0 -->
  136. <ul class="assist-tables">
  137. <li class="assist-entry" style="font-style: italic;">${_('No results found')}</li>
  138. </ul>
  139. <!-- /ko -->
  140. <ul data-bind="foreach: filteredEntries, css: { 'assist-tables': definition.isDatabase }, event: { 'scroll': assistSource.repositionActions }">
  141. <li data-bind="visibleOnHover: { override: statsVisible, selector: (definition.isTable || definition.isView) ? '.table-actions' : '.column-actions' }, css: { 'assist-table': (definition.isTable || definition.isView), 'assist-column': definition.isColumn }">
  142. <!-- ko template: { if: definition.isTable || definition.isView || definition.isColumn, name: 'assist-entry-actions' } --><!-- /ko -->
  143. <a class="assist-entry" data-bind="multiClick: { click: toggleOpen, dblClick: dblClick }, attr: {'title': definition.title }, css: { 'assist-field-link': ! (definition.isTable || definition.isView), 'assist-table-link': (definition.isTable || definition.isView) }" href="javascript:void(0)">
  144. <!-- ko if: definition.isTable -->
  145. <i class="fa fa-fw fa-table muted"></i>
  146. <!-- /ko -->
  147. <!-- ko if: definition.isView -->
  148. <i class="fa fa-fw fa-eye muted"></i>
  149. <!-- /ko -->
  150. <span draggable="true" data-bind="text: definition.displayName, draggableText: { text: editorText }"></span>
  151. </a>
  152. <div class="center" data-bind="visible: loading" style="display:none;">
  153. <!--[if !IE]><!--><i class="fa fa-spinner fa-spin" style="font-size: 20px; color: #BBB"></i><!--<![endif]-->
  154. <!--[if IE]><img src="${ static('desktop/art/spinner.gif') }"/><![endif]-->
  155. </div>
  156. <!-- ko template: { if: open(), name: 'assist-entries' } --><!-- /ko -->
  157. </li>
  158. </ul>
  159. <!-- ko template: { if: ! hasEntries() && ! loading() && (definition.isTable || definition.isView), name: 'assist-no-table-entries' } --><!-- /ko -->
  160. <!-- ko template: { if: ! hasEntries() && ! loading() && definition.isDatabase, name: 'assist-no-database-entries' } --><!-- /ko -->
  161. </script>
  162. <script type="text/html" id="assist-breadcrumb">
  163. <div class="assist-breadcrumb">
  164. <a data-bind="click: back">
  165. <i class="fa fa-chevron-left" style="font-size: 15px;margin-right:8px;"></i>
  166. <i data-bind="visible: selectedSource() && ! selectedSource().selectedDatabase()" style="display:none;font-size: 14px;line-height: 16px;vertical-align: top;" class="fa fa-server"></i>
  167. <i data-bind="visible: selectedSource() && selectedSource().selectedDatabase()" style="display:none;font-size: 14px;line-height: 16px;vertical-align: top;" class="fa fa-database"></i>
  168. <span style="font-size: 14px;line-height: 16px;vertical-align: top;" data-bind="text: breadcrumb"></span></a>
  169. </div>
  170. </script>
  171. <script type="text/html" id="assist-panel-template">
  172. <!-- ko template: { if: breadcrumb() !== null, name: 'assist-breadcrumb' } --><!-- /ko -->
  173. <ul class="nav nav-list" style="position:relative; border: none; padding: 0; background-color: #FFF; margin-bottom: 1px; margin-top:3px;width:100%;">
  174. <!-- ko template: { ifnot: selectedSource, name: 'assist-sources-template' } --><!-- /ko -->
  175. <!-- ko with: selectedSource -->
  176. <!-- ko template: { ifnot: selectedDatabase, name: 'assist-databases-template' }--><!-- /ko -->
  177. <!-- ko with: selectedDatabase -->
  178. <!-- ko template: { name: "assist-tables-template" } --><!-- /ko -->
  179. <!-- /ko -->
  180. <!-- /ko -->
  181. </ul>
  182. </script>
  183. <script type="text/html" id="assist-sources-template">
  184. <li class="nav-header" data-bind="visibleOnHover: { selector: '.hover-actions' }">
  185. ${_('sources')}
  186. </li>
  187. <li>
  188. <ul class="assist-tables" data-bind="foreach: sources">
  189. <li class="assist-table pointer">
  190. <a class="assist-table-link" href="javascript: void(0);" data-bind="text: name, click: function () { $parent.selectedSource($data); }"></a>
  191. </li>
  192. </ul>
  193. </li>
  194. </script>
  195. <script type="text/html" id="assist-databases-template">
  196. <li class="nav-header" data-bind="visibleOnHover: { selector: '.hover-actions' }">
  197. ${_('databases')}
  198. <div class="pull-right" data-bind="css: { 'hover-actions' : ! reloading() }">
  199. <a class="inactive-action" href="javascript:void(0)" data-bind="click: function () { huePubSub.publish('assist.refresh'); }"><i class="pointer fa fa-refresh" data-bind="css: { 'fa-spin' : reloading }" title="${_('Manually refresh the databases list')}"></i></a>
  200. </div>
  201. </li>
  202. <li data-bind="visible: ! hasErrors()" >
  203. <ul class="assist-tables" data-bind="foreach: databases">
  204. <li class="assist-table pointer" data-bind="visibleOnHover: { selector: '.database-actions' }">
  205. <!-- ko template: { name: 'assist-entry-actions' } --><!-- /ko -->
  206. <a class="assist-table-link" href="javascript: void(0);" data-bind="text: definition.name, click: function () { $parent.selectedDatabase($data) }"></a>
  207. </li>
  208. </ul>
  209. </li>
  210. <li class="center" data-bind="visible: loading" >
  211. <!--[if !IE]><!--><i class="fa fa-spinner fa-spin" style="font-size: 20px; color: #BBB"></i><!--<![endif]-->
  212. <!--[if IE]><img src="${ static('desktop/art/spinner.gif') }"/><![endif]-->
  213. </li>
  214. <li data-bind="visible: hasErrors">
  215. <span>${ _('The database list cannot be loaded.') }</span>
  216. </li>
  217. </script>
  218. <script type="text/html" id="assist-tables-template">
  219. <div data-bind="visibleOnHover: { selector: '.hover-actions', override: $parent.reloading }" style="position: relative; width:100%">
  220. <li class="nav-header" style="margin-top: 0" data-bind="visible: !$parent.loading() && !$parent.hasErrors()">
  221. ${_('tables')}
  222. <div class="pull-right hover-actions" data-bind="visible: hasEntries() && !$parent.loading() && !$parent.hasErrors()">
  223. <span class="assist-tables-counter">(<span data-bind="text: filteredEntries().length"></span>)</span>
  224. <a class="inactive-action" href="javascript:void(0)" data-bind="click: toggleSearch, css: { 'blue' : isSearchVisible }"><i class="pointer fa fa-search" title="${_('Search')}"></i></a>
  225. <a class="inactive-action" href="javascript:void(0)" data-bind="click: $parent.reload"><i class="pointer fa fa-refresh" data-bind="css: { 'fa-spin blue' : $parent.reloading }" title="${_('Manually refresh the table list')}"></i></a>
  226. </div>
  227. </li>
  228. <li data-bind="slideVisible: hasEntries() && isSearchVisible() && !$parent.loading() && !$parent.hasErrors()">
  229. <div><label class="checkbox inline-block margin-left-5"><input type="checkbox" data-bind="checked: filter.showTables" />Tables</label><label class="checkbox inline-block margin-left-10"><input type="checkbox" data-bind="checked: filter.showViews" />Views</label></div>
  230. <div><input id="searchInput" type="text" placeholder="${ _('Table name...') }" style="width:90%;" data-bind="hasFocus: editingSearch, clearable: filter.query, value: filter.query, valueUpdate: 'afterkeydown'"/></div>
  231. </li>
  232. <div class="table-container">
  233. <div class="center" data-bind="visible: loading() || $parent.loading()">
  234. <!--[if !IE]><!--><i class="fa fa-spinner fa-spin" style="font-size: 20px; color: #BBB"></i><!--<![endif]-->
  235. <!--[if IE]><img src="${ static('desktop/art/spinner.gif') }"/><![endif]-->
  236. </div>
  237. <!-- ko template: { ifnot: loading() || $parent.loading(), name: 'assist-entries' } --><!-- /ko -->
  238. </div>
  239. </div>
  240. <div id="assistQuickLook" class="modal hide fade">
  241. <div class="modal-header">
  242. <a href="#" class="close" data-dismiss="modal">&times;</a>
  243. <a class="tableLink pull-right" href="#" target="_blank" style="margin-right: 20px;margin-top:6px">
  244. <i class="fa fa-external-link"></i> ${ _('View in Metastore Browser') }
  245. </a>
  246. <h3>${_('Data sample for')} <span class="tableName"></span></h3>
  247. </div>
  248. <div class="modal-body" style="min-height: 100px">
  249. <!-- ko if: assistSource.loadingSamples -->
  250. <div class="loader">
  251. <!--[if !IE]><!--><i class="fa fa-spinner fa-spin" style="font-size: 30px; color: #DDD"></i><!--<![endif]-->
  252. <!--[if IE]><img src="${ static('desktop/art/spinner.gif') }"/><![endif]-->
  253. </div>
  254. <!-- /ko -->
  255. <!-- ko ifnot: assistSource.loadingSamples -->
  256. <div style="overflow: auto">
  257. <!-- ko with: assistSource.samples -->
  258. <!-- ko if: rows.length == 0 -->
  259. <div class="alert">${ _('The selected table has no data.') }</div>
  260. <!-- /ko -->
  261. <!-- ko if: rows.length > 0 -->
  262. <table class="table table-striped table-condensed">
  263. <tr>
  264. <th style="width: 10px"></th>
  265. <!-- ko foreach: headers -->
  266. <th data-bind="text: $data"></th>
  267. <!-- /ko -->
  268. </tr>
  269. <tbody>
  270. <!-- ko foreach: rows -->
  271. <tr>
  272. <td data-bind="text: $index()+1"></td>
  273. <!-- ko foreach: $data -->
  274. <td style="white-space: pre;" data-bind="text: $data"></td>
  275. <!-- /ko -->
  276. </tr>
  277. <!-- /ko -->
  278. </tbody>
  279. </table>
  280. <!-- /ko -->
  281. <!-- /ko -->
  282. </div>
  283. <!-- /ko -->
  284. </div>
  285. <div class="modal-footer">
  286. <button class="btn btn-primary disable-feedback" data-dismiss="modal">${_('Ok')}</button>
  287. </div>
  288. </div>
  289. </script>
  290. <script type="text/javascript" charset="utf-8">
  291. (function (factory) {
  292. if(typeof require === "function") {
  293. define('assistPanel', ['knockout', 'desktop/js/assist/assistSource', 'desktop/js/assist/assistHelper', 'tableStats'], factory);
  294. } else {
  295. factory(ko, AssistSource, AssistHelper);
  296. }
  297. }(function (ko, AssistSource, AssistHelper) {
  298. /**
  299. * @param {Object} params
  300. * @param {Object[]} params.sourceTypes - All the available SQL source types
  301. * @param {string} params.sourceTypes[].name - Example: Hive SQL
  302. * @param {string} params.sourceTypes[].type - Example: hive
  303. * @param {string} [params.activeSourceType] - Example: hive
  304. * @param {string} params.user
  305. * @param {Object} params.navigationSettings - enable/disable the links
  306. * @param {boolean} params.navigationSettings.openItem - Example: true
  307. * @param {boolean} params.navigationSettings.showPreview - Example: true
  308. * @param {boolean} params.navigationSettings.showStats - Example: true
  309. * @constructor
  310. */
  311. function AssistPanel (params) {
  312. var self = this;
  313. var i18n = {
  314. errorLoadingDatabases: "${ _('There was a problem loading the databases') }",
  315. errorLoadingTablePreview: "${ _('There was a problem loading the table preview.') }"
  316. };
  317. var assistHelper = new AssistHelper(i18n, params.user);
  318. self.sources = ko.observableArray();
  319. var sourceIndex = {};
  320. $.each(params.sourceTypes, function (idx, sourceType) {
  321. sourceIndex[sourceType.type] = new AssistSource({
  322. assistHelper: assistHelper,
  323. i18n: i18n,
  324. type: sourceType.type,
  325. name: sourceType.name,
  326. navigationSettings: params.navigationSettings
  327. });
  328. self.sources.push(sourceIndex[sourceType.type]);
  329. });
  330. self.selectedSource = ko.observable(null);
  331. var setDatabaseWhenLoaded = function (databaseName) {
  332. if (self.selectedSource().loaded()) {
  333. self.selectedSource().setDatabase(databaseName);
  334. } else {
  335. var subscription = self.selectedSource().loaded.subscribe(function (newValue) {
  336. if (newValue) {
  337. self.selectedSource().setDatabase(databaseName);
  338. subscription.dispose();
  339. }
  340. });
  341. if (! self.selectedSource().loaded() && ! self.selectedSource().loading()) {
  342. self.selectedSource().initDatabases();
  343. }
  344. }
  345. };
  346. huePubSub.subscribe("assist.set.database", function (databaseDef) {
  347. if (! databaseDef.source || ! sourceIndex[databaseDef.source]) {
  348. return;
  349. }
  350. self.selectedSource(sourceIndex[databaseDef.source]);
  351. setDatabaseWhenLoaded(databaseDef.name);
  352. });
  353. huePubSub.subscribe("assist.get.database", function (source) {
  354. if (sourceIndex[source] && sourceIndex[source].selectedDatabase()) {
  355. huePubSub.publish("assist.database.set", {
  356. source: source,
  357. name: sourceIndex[source].selectedDatabase().databaseName
  358. });
  359. } else {
  360. huePubSub.publish("assist.database.set", {
  361. source: source,
  362. name: null
  363. });
  364. }
  365. });
  366. huePubSub.publish("assist.ready");
  367. self.selectedSource.subscribe(function (newSource) {
  368. if (newSource) {
  369. newSource.initDatabases();
  370. $.totalStorage("hue.assist.lastSelectedSource." + self.user, newSource.type);
  371. } else {
  372. $.totalStorage("hue.assist.lastSelectedSource." + self.user, null);
  373. }
  374. });
  375. var storageSourceType = $.totalStorage("hue.assist.lastSelectedSource." + self.user);
  376. if (! self.selectedSource()) {
  377. if (params.activeSourceType) {
  378. self.selectedSource(sourceIndex[params.activeSourceType]);
  379. setDatabaseWhenLoaded();
  380. } else if (storageSourceType && sourceIndex[storageSourceType]) {
  381. self.selectedSource(sourceIndex[storageSourceType]);
  382. setDatabaseWhenLoaded();
  383. }
  384. }
  385. self.breadcrumb = ko.computed(function () {
  386. if (self.selectedSource()) {
  387. if (self.selectedSource().selectedDatabase()) {
  388. return self.selectedSource().selectedDatabase().definition.name;
  389. }
  390. return self.selectedSource().name;
  391. }
  392. return null;
  393. });
  394. }
  395. AssistPanel.prototype.back = function () {
  396. var self = this;
  397. if (self.selectedSource() && self.selectedSource().selectedDatabase()) {
  398. self.selectedSource().selectedDatabase(null)
  399. } else if (self.selectedSource()) {
  400. self.selectedSource(null);
  401. }
  402. };
  403. ko.components.register('assist-panel', {
  404. viewModel: AssistPanel,
  405. template: { element: 'assist-panel-template' }
  406. });
  407. }));
  408. </script>
  409. </%def>