ko.hue-bindings.js 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179
  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. ko.bindingHandlers.slideVisible = {
  17. init: function (element, valueAccessor) {
  18. var value = valueAccessor();
  19. $(element).toggle(ko.unwrap(value));
  20. },
  21. update: function (element, valueAccessor) {
  22. var value = valueAccessor();
  23. ko.unwrap(value) ? $(element).slideDown(100) : $(element).slideUp(100);
  24. }
  25. };
  26. ko.bindingHandlers.fadeVisible = {
  27. init: function (element, valueAccessor) {
  28. var value = valueAccessor();
  29. $(element).toggle(ko.unwrap(value));
  30. },
  31. update: function (element, valueAccessor) {
  32. var value = valueAccessor();
  33. $(element).stop();
  34. ko.unwrap(value) ? $(element).fadeIn() : $(element).hide();
  35. }
  36. };
  37. ko.extenders.numeric = function (target, precision) {
  38. var result = ko.computed({
  39. read: target,
  40. write: function (newValue) {
  41. var current = target(),
  42. roundingMultiplier = Math.pow(10, precision),
  43. newValueAsNum = isNaN(newValue) ? 0 : parseFloat(+newValue),
  44. valueToWrite = Math.round(newValueAsNum * roundingMultiplier) / roundingMultiplier;
  45. if (valueToWrite !== current) {
  46. target(valueToWrite);
  47. } else {
  48. if (newValue !== current) {
  49. target.notifySubscribers(valueToWrite);
  50. }
  51. }
  52. }
  53. }).extend({ notify: 'always' });
  54. result(target());
  55. return result;
  56. };
  57. ko.bindingHandlers.freshereditor = {
  58. init: function (element, valueAccessor, allBindingsAccessor, viewModel) {
  59. var _el = $(element);
  60. var options = $.extend(valueAccessor(), {});
  61. _el.html(options.data());
  62. _el.freshereditor({
  63. excludes: ['strikethrough', 'removeFormat', 'insertorderedlist', 'justifyfull', 'insertheading1', 'insertheading2', 'superscript', 'subscript']
  64. });
  65. _el.freshereditor("edit", true);
  66. _el.on("mouseup", function () {
  67. storeSelection();
  68. updateValues();
  69. });
  70. var sourceDelay = -1;
  71. _el.on("keyup", function () {
  72. clearTimeout(sourceDelay);
  73. storeSelection();
  74. sourceDelay = setTimeout(function () {
  75. updateValues();
  76. }, 100);
  77. });
  78. $(".chosen-select").chosen({
  79. disable_search_threshold: 10,
  80. width: "75%"
  81. });
  82. $(document).on("addFieldToVisual", function (e, field) {
  83. _el.focus();
  84. pasteHtmlAtCaret("{{" + field.name() + "}}");
  85. });
  86. $(document).on("addFunctionToVisual", function (e, fn) {
  87. _el.focus();
  88. pasteHtmlAtCaret(fn);
  89. });
  90. function updateValues() {
  91. $("[data-template]")[0].editor.setValue(stripHtmlFromFunctions(_el.html()));
  92. valueAccessor().data(_el.html());
  93. }
  94. function storeSelection() {
  95. if (window.getSelection) {
  96. // IE9 and non-IE
  97. sel = window.getSelection();
  98. if (sel.getRangeAt && sel.rangeCount) {
  99. range = sel.getRangeAt(0);
  100. _el.data("range", range);
  101. }
  102. }
  103. else if (document.selection && document.selection.type != "Control") {
  104. // IE < 9
  105. _el.data("selection", document.selection);
  106. }
  107. }
  108. function pasteHtmlAtCaret(html) {
  109. var sel, range;
  110. if (window.getSelection) {
  111. // IE9 and non-IE
  112. sel = window.getSelection();
  113. if (sel.getRangeAt && sel.rangeCount) {
  114. if (_el.data("range")) {
  115. range = _el.data("range");
  116. }
  117. else {
  118. range = sel.getRangeAt(0);
  119. }
  120. range.deleteContents();
  121. // Range.createContextualFragment() would be useful here but is
  122. // non-standard and not supported in all browsers (IE9, for one)
  123. var el = document.createElement("div");
  124. el.innerHTML = html;
  125. var frag = document.createDocumentFragment(), node, lastNode;
  126. while ((node = el.firstChild)) {
  127. lastNode = frag.appendChild(node);
  128. }
  129. range.insertNode(frag);
  130. // Preserve the selection
  131. if (lastNode) {
  132. range = range.cloneRange();
  133. range.setStartAfter(lastNode);
  134. range.collapse(true);
  135. sel.removeAllRanges();
  136. sel.addRange(range);
  137. }
  138. }
  139. } else if (document.selection && document.selection.type != "Control") {
  140. // IE < 9
  141. if (_el.data("selection")) {
  142. _el.data("selection").createRange().pasteHTML(html);
  143. }
  144. else {
  145. document.selection.createRange().pasteHTML(html);
  146. }
  147. }
  148. }
  149. }
  150. };
  151. ko.bindingHandlers.slider = {
  152. init: function (element, valueAccessor) {
  153. var _el = $(element);
  154. var _options = $.extend(valueAccessor(), {});
  155. _el.slider({
  156. min: !isNaN(parseFloat(_options.start())) ? parseFloat(_options.start()) : 0,
  157. max: !isNaN(parseFloat(_options.end())) ? parseFloat(_options.end()) : 10,
  158. step: !isNaN(parseFloat(_options.gap())) ? parseFloat(_options.gap()) : 1,
  159. handle: _options.handle ? _options.handle : 'triangle',
  160. start: parseFloat(_options.min()),
  161. end: parseFloat(_options.max()),
  162. tooltip_split: true,
  163. tooltip: 'always'
  164. });
  165. _el.on("slide", function (e) {
  166. _options.start(e.min);
  167. _options.end(e.max);
  168. _options.min(e.start);
  169. _options.max(e.end);
  170. _options.gap(e.step);
  171. });
  172. _el.on("slideStop", function (e) {
  173. viewModel.search();
  174. });
  175. },
  176. update: function (element, valueAccessor) {
  177. var _options = $.extend(valueAccessor(), {});
  178. }
  179. }
  180. ko.bindingHandlers.daterangepicker = {
  181. INTERVAL_OPTIONS: [
  182. {
  183. value: "+200MILLISECONDS",
  184. label: "200ms"
  185. },
  186. {
  187. value: "+1SECONDS",
  188. label: "1s"
  189. },
  190. {
  191. value: "+1MINUTES",
  192. label: "1m"
  193. },
  194. {
  195. value: "+5MINUTES",
  196. label: "5m"
  197. },
  198. {
  199. value: "+10MINUTES",
  200. label: "10m"
  201. },
  202. {
  203. value: "+30MINUTES",
  204. label: "30m"
  205. },
  206. {
  207. value: "+1HOURS",
  208. label: "1h"
  209. },
  210. {
  211. value: "+3HOURS",
  212. label: "3h"
  213. },
  214. {
  215. value: "+6HOURS",
  216. label: "6h"
  217. },
  218. {
  219. value: "+12HOURS",
  220. label: "12h"
  221. },
  222. {
  223. value: "+1DAYS",
  224. label: "1d"
  225. },
  226. {
  227. value: "+7DAYS",
  228. label: "7d"
  229. },
  230. {
  231. value: "+1MONTHS",
  232. label: "1M"
  233. },
  234. {
  235. value: "+6MONTHS",
  236. label: "6M"
  237. },
  238. {
  239. value: "+1YEARS",
  240. label: "1y"
  241. }
  242. ],
  243. EXTRA_INTERVAL_OPTIONS: [],
  244. init: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
  245. var DATE_FORMAT = "YYYY-MM-DD";
  246. var TIME_FORMAT = "HH:mm:ss";
  247. var DATETIME_FORMAT = DATE_FORMAT + " " + TIME_FORMAT;
  248. var _el = $(element);
  249. var _options = $.extend(valueAccessor(), {});
  250. var _intervalOptions = [];
  251. ko.bindingHandlers.daterangepicker.INTERVAL_OPTIONS.forEach(function (interval) {
  252. _intervalOptions.push('<option value="' + interval.value + '">' + interval.label + '</option>');
  253. });
  254. function enableOptions() {
  255. var _opts = [];
  256. var _tmp = $("<div>").html(_intervalOptions.join(""))
  257. $.each(arguments, function (cnt, item) {
  258. if (_tmp.find("option[value='+" + item + "']").length > 0) {
  259. _opts.push('<option value="+' + item + '">' + _tmp.find("option[value='+" + item + "']").eq(0).text() + '</option>');
  260. }
  261. });
  262. return _opts;
  263. }
  264. function renderOptions(opts) {
  265. var _html = "";
  266. for (var i = 0; i < opts.length; i++) {
  267. _html += opts[i];
  268. }
  269. return _html;
  270. }
  271. var _tmpl = $('<div class="simpledaterangepicker">' +
  272. '<div class="facet-field-cnt picker">' +
  273. '<div class="facet-field-label facet-field-label-fixed-width">' + KO_DATERANGEPICKER_LABELS.START + '</div>' +
  274. '<div class="input-prepend input-group">' +
  275. '<span class="add-on input-group-addon"><i class="fa fa-calendar"></i></span>' +
  276. '<input type="text" class="input-small form-control start-date" />' +
  277. '</div>' +
  278. '<div class="input-prepend input-group left-margin">' +
  279. '<span class="add-on input-group-addon"><i class="fa fa-clock-o"></i></span>' +
  280. '<input type="text" class="input-mini form-control start-time" />' +
  281. '</div>' +
  282. '</div>' +
  283. '<div class="facet-field-cnt picker">' +
  284. '<div class="facet-field-label facet-field-label-fixed-width">' + KO_DATERANGEPICKER_LABELS.END + '</div>' +
  285. '<div class="input-prepend input-group">' +
  286. '<span class="add-on input-group-addon"><i class="fa fa-calendar"></i></span>' +
  287. '<input type="text" class="input-small form-control end-date" />' +
  288. '</div>' +
  289. '<div class="input-prepend input-group left-margin">' +
  290. '<span class="add-on input-group-addon"><i class="fa fa-clock-o"></i></span>' +
  291. '<input type="text" class="input-mini form-control end-time" />' +
  292. '</div>' +
  293. '</div>' +
  294. '<div class="facet-field-cnt picker">' +
  295. '<div class="facet-field-label facet-field-label-fixed-width">' + KO_DATERANGEPICKER_LABELS.INTERVAL + '</div>' +
  296. '<div class="input-prepend input-group"><span class="add-on input-group-addon"><i class="fa fa-repeat"></i></span></div>&nbsp;' +
  297. '<select class="input-small interval-select" style="margin-right: 6px">' +
  298. renderOptions(_intervalOptions) +
  299. '</select>' +
  300. '<input class="input interval hide" type="hidden" value="" />' +
  301. '</div>' +
  302. '<div class="facet-field-cnt picker">' +
  303. '<div class="facet-field-label facet-field-label-fixed-width"></div>' +
  304. '<div class="facet-field-switch"><a href="javascript:void(0)"><i class="fa fa-calendar-o"></i> ' + KO_DATERANGEPICKER_LABELS.CUSTOM_FORMAT + '</a></div>' +
  305. '</div>' +
  306. '<div class="facet-field-cnt custom">' +
  307. '<div class="facet-field-label facet-field-label-fixed-width">' + KO_DATERANGEPICKER_LABELS.START + '</div>' +
  308. '<div class="input-prepend input-group">' +
  309. '<span class="add-on input-group-addon"><i class="fa fa-calendar"></i></span>' +
  310. '<input type="text" class="input-large form-control start-date-custom" />' +
  311. '</div>' +
  312. '<a class="custom-popover" href="javascript:void(0)" data-trigger="click" data-toggle="popover" data-placement="right" rel="popover" data-html="true"' +
  313. ' title="' + KO_DATERANGEPICKER_LABELS.CUSTOM_POPOVER_TITLE + '"' +
  314. ' data-content="' + KO_DATERANGEPICKER_LABELS.CUSTOM_POPOVER_CONTENT + '">' +
  315. '<i class="fa fa-question-circle"></i>' +
  316. ' </a>' +
  317. '</div>' +
  318. '<div class="facet-field-cnt custom">' +
  319. '<div class="facet-field-label facet-field-label-fixed-width">' + KO_DATERANGEPICKER_LABELS.END + '</div>' +
  320. '<div class="input-prepend input-group">' +
  321. '<span class="add-on input-group-addon"><i class="fa fa-calendar"></i></span>' +
  322. '<input type="text" class="input-large form-control end-date-custom" />' +
  323. '</div>' +
  324. '</div>' +
  325. '<div class="facet-field-cnt custom">' +
  326. '<div class="facet-field-label facet-field-label-fixed-width">' + KO_DATERANGEPICKER_LABELS.INTERVAL + '</div>' +
  327. '<div class="input-prepend input-group">' +
  328. '<span class="add-on input-group-addon"><i class="fa fa-repeat"></i></span>' +
  329. '<input type="text" class="input-large form-control interval-custom" />' +
  330. '</div>' +
  331. '</div>' +
  332. '<div class="facet-field-cnt custom">' +
  333. '<div class="facet-field-label facet-field-label-fixed-width"></div>' +
  334. '<div class="facet-field-switch"><a href="javascript:void(0)"><i class="fa fa-calendar"></i> ' + KO_DATERANGEPICKER_LABELS.DATE_PICKERS + '</a></div>' +
  335. '</div>' +
  336. '</div>'
  337. );
  338. _tmpl.insertAfter(_el);
  339. $(".custom-popover").popover();
  340. var _minMoment = moment(_options.min());
  341. var _maxMoment = moment(_options.max());
  342. if (_minMoment.isValid() && _maxMoment.isValid()) {
  343. _tmpl.find(".facet-field-cnt.custom").hide();
  344. _tmpl.find(".facet-field-cnt.picker").show();
  345. _tmpl.find(".start-date").val(_minMoment.utc().format(DATE_FORMAT));
  346. _tmpl.find(".start-time").val(_minMoment.utc().format(TIME_FORMAT));
  347. _tmpl.find(".end-date").val(_maxMoment.utc().format(DATE_FORMAT));
  348. _tmpl.find(".end-time").val(_maxMoment.utc().format(TIME_FORMAT));
  349. _tmpl.find(".interval").val(_options.gap());
  350. _tmpl.find(".interval-select").val(_options.gap());
  351. _tmpl.find(".interval-custom").val(_options.gap());
  352. if (_tmpl.find(".interval-select").val() == null || ko.bindingHandlers.daterangepicker.EXTRA_INTERVAL_OPTIONS.indexOf(_tmpl.find(".interval-select").val()) > -1) {
  353. pushIntervalValue(_options.gap());
  354. _tmpl.find(".facet-field-cnt.custom").show();
  355. _tmpl.find(".facet-field-cnt.picker").hide();
  356. }
  357. }
  358. else {
  359. _tmpl.find(".facet-field-cnt.custom").show();
  360. _tmpl.find(".facet-field-cnt.picker").hide();
  361. _tmpl.find(".start-date-custom").val(_options.min());
  362. _tmpl.find(".end-date-custom").val(_options.max());
  363. _tmpl.find(".interval-custom").val(_options.gap());
  364. pushIntervalValue(_options.gap());
  365. }
  366. if (typeof _options.relatedgap != "undefined"){
  367. pushIntervalValue(_options.relatedgap());
  368. }
  369. _tmpl.find(".start-date").datepicker({
  370. format: DATE_FORMAT.toLowerCase()
  371. }).on("changeDate", function () {
  372. rangeHandler(true);
  373. });
  374. _tmpl.find(".start-date").on("change", function () {
  375. rangeHandler(true);
  376. });
  377. _tmpl.find(".start-time").timepicker({
  378. minuteStep: 1,
  379. showSeconds: true,
  380. showMeridian: false,
  381. defaultTime: false
  382. });
  383. _tmpl.find(".end-date").datepicker({
  384. format: DATE_FORMAT.toLowerCase()
  385. }).on("changeDate", function () {
  386. rangeHandler(false);
  387. });
  388. _tmpl.find(".end-date").on("change", function () {
  389. rangeHandler(true);
  390. });
  391. _tmpl.find(".end-time").timepicker({
  392. minuteStep: 1,
  393. showSeconds: true,
  394. showMeridian: false,
  395. defaultTime: false
  396. });
  397. _tmpl.find(".start-time").on("change", function () {
  398. // the timepicker plugin doesn't have a change event handler
  399. // so we need to wait a bit to handle in with the default field event
  400. window.setTimeout(function () {
  401. rangeHandler(true)
  402. }, 200);
  403. });
  404. _tmpl.find(".end-time").on("change", function () {
  405. window.setTimeout(function () {
  406. rangeHandler(false)
  407. }, 200);
  408. });
  409. if (_minMoment.isValid() && _maxMoment.isValid()) {
  410. rangeHandler(true);
  411. }
  412. _tmpl.find(".facet-field-cnt.picker .facet-field-switch a").on("click", function () {
  413. _tmpl.find(".facet-field-cnt.custom").show();
  414. _tmpl.find(".facet-field-cnt.picker").hide();
  415. });
  416. _tmpl.find(".facet-field-cnt.custom .facet-field-switch a").on("click", function () {
  417. _tmpl.find(".facet-field-cnt.custom").hide();
  418. _tmpl.find(".facet-field-cnt.picker").show();
  419. });
  420. _tmpl.find(".start-date-custom").on("change", function () {
  421. _options.min(_tmpl.find(".start-date-custom").val());
  422. _tmpl.find(".start-date").val(moment(_options.min()).utc().format(DATE_FORMAT));
  423. _tmpl.find(".start-time").val(moment(_options.min()).utc().format(TIME_FORMAT));
  424. _options.start(_options.min());
  425. });
  426. _tmpl.find(".end-date-custom").on("change", function () {
  427. _options.max(_tmpl.find(".end-date-custom").val());
  428. _tmpl.find(".end-date").val(moment(_options.max()).utc().format(DATE_FORMAT));
  429. _tmpl.find(".end-time").val(moment(_options.max()).utc().format(TIME_FORMAT));
  430. _options.end(_options.max());
  431. });
  432. _tmpl.find(".interval-custom").on("change", function () {
  433. _options.gap(_tmpl.find(".interval-custom").val());
  434. matchIntervals(true);
  435. if (typeof _options.relatedgap != "undefined"){
  436. _options.relatedgap(_options.gap());
  437. }
  438. });
  439. function pushIntervalValue(newValue) {
  440. var _found = false;
  441. ko.bindingHandlers.daterangepicker.INTERVAL_OPTIONS.forEach(function(interval) {
  442. if (interval.value == newValue){
  443. _found = true;
  444. }
  445. });
  446. if (!_found){
  447. ko.bindingHandlers.daterangepicker.INTERVAL_OPTIONS.push({
  448. value: newValue,
  449. label: newValue
  450. });
  451. ko.bindingHandlers.daterangepicker.EXTRA_INTERVAL_OPTIONS.push(newValue);
  452. _intervalOptions.push('<option value="' + newValue + '">' + newValue + '</option>');
  453. }
  454. }
  455. function matchIntervals(fromCustom) {
  456. _tmpl.find(".interval-select").val(_options.gap());
  457. if (_tmpl.find(".interval-select").val() == null) {
  458. if (fromCustom){
  459. pushIntervalValue(_options.gap());
  460. if (bindingContext.$root.intervalOptions){
  461. bindingContext.$root.intervalOptions(ko.bindingHandlers.daterangepicker.INTERVAL_OPTIONS);
  462. }
  463. }
  464. else {
  465. _tmpl.find(".interval-select").val(_tmpl.find(".interval-select option:first").val());
  466. _options.gap(_tmpl.find(".interval-select").val());
  467. if (typeof _options.relatedgap != "undefined"){
  468. _options.relatedgap(_options.gap());
  469. }
  470. _tmpl.find(".interval-custom").val(_options.gap());
  471. }
  472. }
  473. }
  474. _tmpl.find(".interval-select").on("change", function () {
  475. _options.gap(_tmpl.find(".interval-select").val());
  476. if (typeof _options.relatedgap != "undefined"){
  477. _options.relatedgap(_options.gap());
  478. }
  479. _tmpl.find(".interval").val(_options.gap());
  480. _tmpl.find(".interval-custom").val(_options.gap());
  481. });
  482. function rangeHandler(isStart) {
  483. var startDate = moment(_tmpl.find(".start-date").val() + " " + _tmpl.find(".start-time").val(), DATETIME_FORMAT);
  484. var endDate = moment(_tmpl.find(".end-date").val() + " " + _tmpl.find(".end-time").val(), DATETIME_FORMAT);
  485. if (startDate.valueOf() > endDate.valueOf()) {
  486. if (isStart) {
  487. _tmpl.find(".end-date").val(startDate.utc().format(DATE_FORMAT));
  488. _tmpl.find(".end-date").datepicker('setValue', startDate.utc().format(DATE_FORMAT));
  489. _tmpl.find(".end-date").data("original-val", _tmpl.find(".end-date").val());
  490. _tmpl.find(".end-time").val(startDate.utc().format(TIME_FORMAT));
  491. }
  492. else {
  493. if (_tmpl.find(".end-date").val() == _tmpl.find(".start-date").val()) {
  494. _tmpl.find(".end-time").val(startDate.utc().format(TIME_FORMAT));
  495. _tmpl.find(".end-time").data("timepicker").setValues(startDate.format(TIME_FORMAT));
  496. }
  497. else {
  498. _tmpl.find(".end-date").val(_tmpl.find(".end-date").data("original-val"));
  499. _tmpl.find(".end-date").datepicker("setValue", _tmpl.find(".end-date").data("original-val"));
  500. }
  501. // non-sticky error notification
  502. $.jHueNotify.notify({
  503. level: "ERROR",
  504. message: "The end cannot be before the starting moment"
  505. });
  506. }
  507. }
  508. else {
  509. _tmpl.find(".end-date").data("original-val", _tmpl.find(".end-date").val());
  510. _tmpl.find(".start-date").datepicker("hide");
  511. _tmpl.find(".end-date").datepicker("hide");
  512. }
  513. var _calculatedStartDate = moment(_tmpl.find(".start-date").val() + " " + _tmpl.find(".start-time").val(), DATETIME_FORMAT);
  514. var _calculatedEndDate = moment(_tmpl.find(".end-date").val() + " " + _tmpl.find(".end-time").val(), DATETIME_FORMAT);
  515. _options.min(_calculatedStartDate.format("YYYY-MM-DD[T]HH:mm:ss[Z]"));
  516. _options.start(_options.min());
  517. _options.max(_calculatedEndDate.format("YYYY-MM-DD[T]HH:mm:ss[Z]"));
  518. _options.end(_options.max());
  519. _tmpl.find(".start-date-custom").val(_options.min());
  520. _tmpl.find(".end-date-custom").val(_options.max());
  521. var _opts = [];
  522. // hide not useful options from interval
  523. if (_calculatedEndDate.diff(_calculatedStartDate, 'minutes') > 1 && _calculatedEndDate.diff(_calculatedStartDate, 'minutes') <= 60) {
  524. _opts = enableOptions("200MILLISECONDS", "1SECONDS", "1MINUTES", "5MINUTES", "10MINUTES", "30MINUTES");
  525. }
  526. if (_calculatedEndDate.diff(_calculatedStartDate, 'hours') > 1 && _calculatedEndDate.diff(_calculatedStartDate, 'hours') <= 12) {
  527. _opts = enableOptions("5MINUTES", "10MINUTES", "30MINUTES", "1HOURS", "3HOURS");
  528. }
  529. if (_calculatedEndDate.diff(_calculatedStartDate, 'hours') > 12 && _calculatedEndDate.diff(_calculatedStartDate, 'hours') < 36) {
  530. _opts = enableOptions("10MINUTES", "30MINUTES", "1HOURS", "3HOURS", "6HOURS", "12HOURS");
  531. }
  532. if (_calculatedEndDate.diff(_calculatedStartDate, 'days') > 1 && _calculatedEndDate.diff(_calculatedStartDate, 'days') <= 7) {
  533. _opts = enableOptions("30MINUTES", "1HOURS", "3HOURS", "6HOURS", "12HOURS", "1DAYS");
  534. }
  535. if (_calculatedEndDate.diff(_calculatedStartDate, 'days') > 7 && _calculatedEndDate.diff(_calculatedStartDate, 'days') <= 14) {
  536. _opts = enableOptions("3HOURS", "6HOURS", "12HOURS", "1DAYS");
  537. }
  538. if (_calculatedEndDate.diff(_calculatedStartDate, 'days') > 14 && _calculatedEndDate.diff(_calculatedStartDate, 'days') <= 31) {
  539. _opts = enableOptions("12HOURS", "1DAYS", "7DAYS");
  540. }
  541. if (_calculatedEndDate.diff(_calculatedStartDate, 'months') >= 1) {
  542. _opts = enableOptions("1DAYS", "7DAYS", "1MONTHS");
  543. }
  544. if (_calculatedEndDate.diff(_calculatedStartDate, 'months') > 6) {
  545. _opts = enableOptions("1DAYS", "7DAYS", "1MONTHS", "6MONTHS");
  546. }
  547. if (_calculatedEndDate.diff(_calculatedStartDate, 'months') > 12) {
  548. _opts = enableOptions("7DAYS", "1MONTHS", "6MONTHS", "1YEARS");
  549. }
  550. $(".interval-select").html(renderOptions(_opts));
  551. matchIntervals(true);
  552. }
  553. }
  554. }
  555. ko.bindingHandlers.augmenthtml = {
  556. render: function (element, valueAccessor, allBindingsAccessor, viewModel) {
  557. var _val = ko.unwrap(valueAccessor());
  558. var _enc = $("<span>").html(_val);
  559. if (_enc.find("style").length > 0) {
  560. var parser = new less.Parser();
  561. $(_enc.find("style")).each(function (cnt, item) {
  562. var _less = "#result-container {" + $(item).text() + "}";
  563. try {
  564. parser.parse(_less, function (err, tree) {
  565. $(item).text(tree.toCSS());
  566. });
  567. }
  568. catch (e) {
  569. }
  570. });
  571. $(element).html(_enc.html());
  572. }
  573. else {
  574. $(element).html(_val);
  575. }
  576. },
  577. init: function (element, valueAccessor, allBindingsAccessor, viewModel) {
  578. ko.bindingHandlers.augmenthtml.render(element, valueAccessor, allBindingsAccessor, viewModel);
  579. },
  580. update: function (element, valueAccessor, allBindingsAccessor) {
  581. ko.bindingHandlers.augmenthtml.render(element, valueAccessor, allBindingsAccessor, viewModel);
  582. }
  583. }
  584. ko.bindingHandlers.clearable = {
  585. init: function (element, valueAccessor, allBindingsAccessor, viewModel) {
  586. var _el = $(element);
  587. function tog(v) {
  588. return v ? "addClass" : "removeClass";
  589. }
  590. _el.addClass("clearable");
  591. _el
  592. .on("input", function () {
  593. _el[tog(this.value)]("x");
  594. })
  595. .on("change", function () {
  596. valueAccessor()(_el.val());
  597. })
  598. .on("blur", function () {
  599. valueAccessor()(_el.val());
  600. })
  601. .on("mousemove", function (e) {
  602. _el[tog(this.offsetWidth - 18 < e.clientX - this.getBoundingClientRect().left)]("onX");
  603. })
  604. .on("click", function (e) {
  605. if (this.offsetWidth - 18 < e.clientX - this.getBoundingClientRect().left) {
  606. _el.removeClass("x onX").val("");
  607. valueAccessor()("");
  608. }
  609. });
  610. if (allBindingsAccessor().valueUpdate != null && allBindingsAccessor().valueUpdate == "afterkeydown") {
  611. _el.on("keyup", function () {
  612. valueAccessor()(_el.val());
  613. });
  614. }
  615. },
  616. update: function (element, valueAccessor, allBindingsAccessor) {
  617. $(element).val(ko.unwrap(valueAccessor()));
  618. }
  619. }
  620. ko.bindingHandlers.spinedit = {
  621. init: function (element, valueAccessor, allBindingsAccessor, viewModel) {
  622. $(element).spinedit({
  623. minimum: 0,
  624. maximum: 10000,
  625. step: 5,
  626. value: ko.unwrap(valueAccessor()),
  627. numberOfDecimals: 0
  628. });
  629. $(element).on("valueChanged", function (e) {
  630. valueAccessor()(e.value);
  631. });
  632. },
  633. update: function (element, valueAccessor, allBindingsAccessor) {
  634. $(element).spinedit("setValue", ko.unwrap(valueAccessor()));
  635. }
  636. }
  637. ko.bindingHandlers.codemirror = {
  638. init: function (element, valueAccessor, allBindingsAccessor, viewModel) {
  639. var options = $.extend(valueAccessor(), {});
  640. var editor = CodeMirror.fromTextArea(element, options);
  641. element.editor = editor;
  642. editor.setValue(options.data());
  643. editor.refresh();
  644. var wrapperElement = $(editor.getWrapperElement());
  645. $(document).on("refreshCodemirror", function () {
  646. editor.setSize("100%", 300);
  647. editor.refresh();
  648. });
  649. $(document).on("addFieldToSource", function (e, field) {
  650. if ($(element).data("template")) {
  651. editor.replaceSelection("{{" + field.name() + "}}");
  652. }
  653. });
  654. $(document).on("addFunctionToSource", function (e, fn) {
  655. if ($(element).data("template")) {
  656. editor.replaceSelection(fn);
  657. }
  658. });
  659. $(".chosen-select").chosen({
  660. disable_search_threshold: 10,
  661. width: "75%"
  662. });
  663. $('.chosen-select').trigger('chosen:updated');
  664. var sourceDelay = -1;
  665. editor.on("change", function (cm) {
  666. clearTimeout(sourceDelay);
  667. var _cm = cm;
  668. sourceDelay = setTimeout(function () {
  669. valueAccessor().data(_cm.getValue());
  670. if ($(".widget-html-pill").parent().hasClass("active")) {
  671. $("[contenteditable=true]").html(stripHtmlFromFunctions(valueAccessor().data()));
  672. }
  673. }, 100);
  674. });
  675. ko.utils.domNodeDisposal.addDisposeCallback(element, function () {
  676. wrapperElement.remove();
  677. });
  678. },
  679. update: function (element, valueAccessor, allBindingsAccessor) {
  680. var editor = element.editor;
  681. editor.refresh();
  682. }
  683. };
  684. ko.bindingHandlers.chosen = {
  685. init: function (element) {
  686. ko.bindingHandlers.options.init(element);
  687. $(element).chosen({disable_search_threshold: 5});
  688. },
  689. update: function (element, valueAccessor, allBindings) {
  690. ko.bindingHandlers.options.update(element, valueAccessor, allBindings);
  691. $(element).trigger('chosen:updated');
  692. }
  693. };
  694. ko.bindingHandlers.tooltip = {
  695. init: function (element, valueAccessor) {
  696. var local = ko.utils.unwrapObservable(valueAccessor()),
  697. options = {};
  698. ko.utils.extend(options, local);
  699. $(element).tooltip(options);
  700. ko.utils.domNodeDisposal.addDisposeCallback(element, function () {
  701. $(element).tooltip("destroy");
  702. });
  703. }
  704. };
  705. ko.bindingHandlers.typeahead = {
  706. init: function (element, valueAccessor) {
  707. var binding = this;
  708. var elem = $(element);
  709. var valueAccessor = valueAccessor();
  710. var _options = {
  711. source: function () {
  712. var _source = ko.utils.unwrapObservable(valueAccessor.source);
  713. if (valueAccessor.extraKeywords) {
  714. _source = _source.concat(valueAccessor.extraKeywords.split(" "))
  715. }
  716. if (valueAccessor.sourceSuffix && _source) {
  717. var _tmp = [];
  718. _source.forEach(function(item){
  719. _tmp.push(item + valueAccessor.sourceSuffix);
  720. });
  721. _source = _tmp;
  722. }
  723. return _source;
  724. },
  725. onselect: function (val) {
  726. if (typeof valueAccessor.target == "function") {
  727. valueAccessor.target(val);
  728. }
  729. else {
  730. valueAccessor.target = val;
  731. }
  732. }
  733. }
  734. function extractor(query) {
  735. var result = /([^ ]+)$/.exec(query);
  736. if (result && result[1])
  737. return result[1].trim();
  738. return "";
  739. }
  740. if (valueAccessor.multipleValues) {
  741. _options.updater = function (item) {
  742. var _val = this.$element.val();
  743. var _separator = (valueAccessor.multipleValuesSeparator || ":");
  744. if (valueAccessor.extraKeywords && valueAccessor.extraKeywords.split(" ").indexOf(item) > -1) {
  745. _separator = "";
  746. }
  747. if (_val.indexOf(" ") > -1) {
  748. return _val.substring(0, _val.lastIndexOf(" ")) + " " + item + _separator;
  749. }
  750. else {
  751. return item + _separator;
  752. }
  753. }
  754. _options.matcher = function (item) {
  755. var _tquery = extractor(this.query);
  756. if (!_tquery) return false;
  757. return ~item.toLowerCase().indexOf(_tquery.toLowerCase());
  758. },
  759. _options.highlighter = function (item) {
  760. var _query = extractor(this.query).replace(/[\-\[\]{}()*+?.:\\\^$|#\s]/g, '\\$&');
  761. return item.replace(new RegExp('(' + _query + ')', 'ig'), function ($1, match) {
  762. return '<strong>' + match + '</strong>'
  763. });
  764. }
  765. }
  766. if (valueAccessor.completeSolrRanges) {
  767. elem.on("keyup", function (e) {
  768. if (e.keyCode != 8 && e.which != 8 && elem.val() && (elem.val().slice(-1) == "[" || elem.val().slice(-1) == "{")) {
  769. var _index = elem.val().length;
  770. elem.val(elem.val() + " TO " + (elem.val().slice(-1) == "[" ? "]" : "}"));
  771. if (element.createTextRange) {
  772. var range = element.createTextRange();
  773. range.move("character", _index);
  774. range.select();
  775. } else if (element.selectionStart != null) {
  776. element.focus();
  777. element.setSelectionRange(_index, _index);
  778. }
  779. }
  780. });
  781. }
  782. if (valueAccessor.triggerOnFocus) {
  783. _options.minLength = 0;
  784. }
  785. elem.typeahead(_options);
  786. if (valueAccessor.triggerOnFocus) {
  787. elem.on('focus', function () {
  788. elem.trigger("keyup");
  789. });
  790. }
  791. elem.blur(function () {
  792. if (typeof valueAccessor.target == "function") {
  793. valueAccessor.target(elem.val());
  794. }
  795. else {
  796. valueAccessor.target = elem.val();
  797. }
  798. });
  799. },
  800. update: function (element, valueAccessor) {
  801. var elem = $(element);
  802. var value = valueAccessor();
  803. if (typeof value.target == "function") {
  804. elem.val(value.target());
  805. }
  806. else {
  807. elem.val(value.target);
  808. }
  809. }
  810. };
  811. ko.bindingHandlers.select2 = {
  812. init: function (element, valueAccessor, allBindingsAccessor, vm) {
  813. var options = ko.toJS(valueAccessor()) || {};
  814. if (typeof valueAccessor().update != "undefined") {
  815. if (options.type == "user" && viewModel.selectableHadoopUsers().indexOf(options.update) == -1) {
  816. viewModel.availableHadoopUsers.push({
  817. username: options.update
  818. });
  819. }
  820. if (options.type == "group") {
  821. if (options.update instanceof Array) {
  822. options.update.forEach(function (opt) {
  823. if (viewModel.selectableHadoopGroups().indexOf(opt) == -1) {
  824. viewModel.availableHadoopGroups.push({
  825. name: opt
  826. });
  827. }
  828. });
  829. }
  830. else if (viewModel.selectableHadoopGroups().indexOf(options.update) == -1) {
  831. viewModel.availableHadoopGroups.push({
  832. name: options.update
  833. });
  834. }
  835. }
  836. if (options.type == "action" && viewModel.availableActions().indexOf(options.update) == -1) {
  837. viewModel.availableActions.push(options.update);
  838. }
  839. if (options.type == "scope" && viewModel.availablePrivileges().indexOf(options.update) == -1) {
  840. viewModel.availablePrivileges.push(options.update);
  841. }
  842. }
  843. $(element)
  844. .select2(options)
  845. .on("change", function (e) {
  846. if (typeof e.val != "undefined" && typeof valueAccessor().update != "undefined") {
  847. valueAccessor().update(e.val);
  848. }
  849. })
  850. .on("select2-focus", function (e) {
  851. if (typeof options.onFocus != "undefined") {
  852. options.onFocus();
  853. }
  854. })
  855. .on("select2-blur", function (e) {
  856. if (typeof options.onBlur != "undefined") {
  857. options.onBlur();
  858. }
  859. })
  860. .on("select2-open", function () {
  861. $(".select2-input").off("keyup").data("type", options.type).on("keyup", function (e) {
  862. if (e.keyCode === 13) {
  863. var _isArray = options.update instanceof Array;
  864. var _newVal = $(this).val();
  865. var _type = $(this).data("type");
  866. if ($.trim(_newVal) != "") {
  867. if (_type == "user") {
  868. viewModel.availableHadoopUsers.push({
  869. username: _newVal
  870. });
  871. }
  872. if (_type == "group") {
  873. viewModel.availableHadoopGroups.push({
  874. name: _newVal
  875. });
  876. }
  877. if (_type == "action") {
  878. viewModel.availableActions.push(_newVal);
  879. }
  880. if (_type == "scope") {
  881. viewModel.availablePrivileges.push(_newVal);
  882. }
  883. if (_type == "role") {
  884. var _r = new Role(viewModel, { name: _newVal });
  885. viewModel.tempRoles.push(_r);
  886. viewModel.roles.push(_r);
  887. }
  888. if (_isArray) {
  889. var _vals = $(element).select2("val");
  890. _vals.push(_newVal);
  891. $(element).select2("val", _vals, true);
  892. }
  893. else {
  894. $(element).select2("val", _newVal, true);
  895. }
  896. $(element).select2("close");
  897. }
  898. }
  899. });
  900. })
  901. },
  902. update: function (element, valueAccessor, allBindingsAccessor, vm) {
  903. if (typeof valueAccessor().update != "undefined") {
  904. $(element).select2("val", valueAccessor().update());
  905. }
  906. if (typeof valueAccessor().readonly != "undefined") {
  907. $(element).select2("readonly", valueAccessor().readonly);
  908. if (typeof valueAccessor().readonlySetTo != "undefined") {
  909. valueAccessor().readonlySetTo();
  910. }
  911. }
  912. }
  913. };
  914. ko.bindingHandlers.hivechooser = {
  915. init: function (element, valueAccessor, allBindingsAccessor, vm) {
  916. var self = $(element);
  917. self.val(valueAccessor()());
  918. function setPathFromAutocomplete(path) {
  919. self.val(path);
  920. valueAccessor()(path);
  921. self.blur();
  922. }
  923. self.on("blur", function () {
  924. valueAccessor()(self.val());
  925. });
  926. self.jHueHiveAutocomplete({
  927. skipColumns: true,
  928. showOnFocus: true,
  929. home: "/",
  930. onPathChange: function (path) {
  931. setPathFromAutocomplete(path);
  932. },
  933. onEnter: function (el) {
  934. setPathFromAutocomplete(el.val());
  935. },
  936. onBlur: function () {
  937. if (self.val().lastIndexOf(".") == self.val().length - 1) {
  938. self.val(self.val().substr(0, self.val().length - 1));
  939. }
  940. valueAccessor()(self.val());
  941. }
  942. });
  943. }
  944. }
  945. ko.bindingHandlers.hdfsAutocomplete = {
  946. init: function (element, valueAccessor, allBindingsAccessor, vm) {
  947. var stripHashes = function (str) {
  948. return str.replace(/#/gi, encodeURIComponent("#"));
  949. };
  950. var self = $(element);
  951. self.attr("autocomplete", "off");
  952. self.jHueHdfsAutocomplete({});
  953. }
  954. };
  955. ko.bindingHandlers.filechooser = {
  956. init: function (element, valueAccessor, allBindingsAccessor, vm) {
  957. var self = $(element);
  958. self.attr("autocomplete", "off");
  959. if (typeof valueAccessor() == "function" || typeof valueAccessor().value == "function") {
  960. self.val(valueAccessor().value ? valueAccessor().value(): valueAccessor()());
  961. self.data("fullPath", self.val());
  962. self.attr("data-original-title", self.val());
  963. if (valueAccessor().displayJustLastBit){
  964. var _val = self.val();
  965. self.val(_val.split("/")[_val.split("/").length - 1]);
  966. }
  967. self.on("blur", function () {
  968. if (valueAccessor().value){
  969. if (valueAccessor().displayJustLastBit){
  970. var _val = self.data("fullPath");
  971. valueAccessor().value(_val.substr(0, _val.lastIndexOf("/")) + "/" + self.val());
  972. }
  973. else {
  974. valueAccessor().value(self.val());
  975. }
  976. self.data("fullPath", valueAccessor().value());
  977. }
  978. else {
  979. valueAccessor()(self.val());
  980. self.data("fullPath", valueAccessor()());
  981. }
  982. self.attr("data-original-title", self.data("fullPath"));
  983. });
  984. }
  985. else {
  986. self.val(valueAccessor());
  987. self.on("blur", function () {
  988. valueAccessor(self.val());
  989. });
  990. }
  991. self.after(getFileBrowseButton(self, true, valueAccessor, true, allBindingsAccessor));
  992. }
  993. };
  994. function getFileBrowseButton(inputElement, selectFolder, valueAccessor, stripHdfsPrefix, allBindingsAccessor) {
  995. var _btn = $("<button>").addClass("btn").addClass("fileChooserBtn").text("..").click(function (e) {
  996. e.preventDefault();
  997. $("html").addClass("modal-open");
  998. // check if it's a relative path
  999. callFileChooser();
  1000. function callFileChooser() {
  1001. var _initialPath = $.trim(inputElement.val()) != "" ? inputElement.val() : "/";
  1002. if ((allBindingsAccessor().filechooserOptions && allBindingsAccessor().filechooserOptions.skipInitialPathIfEmpty && inputElement.val() == "") || allBindingsAccessor().filechooserPrefixSeparator){
  1003. _initialPath = "";
  1004. }
  1005. if (inputElement.data("fullPath")){
  1006. _initialPath = inputElement.data("fullPath");
  1007. }
  1008. if (_initialPath.indexOf("hdfs://") > -1) {
  1009. _initialPath = _initialPath.substring(7);
  1010. }
  1011. $("#filechooser").jHueFileChooser({
  1012. suppressErrors: true,
  1013. selectFolder: (selectFolder) ? true : false,
  1014. onFolderChoose: function (filePath) {
  1015. handleChoice(filePath, stripHdfsPrefix);
  1016. if (selectFolder) {
  1017. $("#chooseFile").modal("hide");
  1018. }
  1019. },
  1020. onFileChoose: function (filePath) {
  1021. handleChoice(filePath, stripHdfsPrefix);
  1022. $("#chooseFile").modal("hide");
  1023. },
  1024. createFolder: allBindingsAccessor().filechooserOptions && allBindingsAccessor().filechooserOptions.createFolder,
  1025. uploadFile: allBindingsAccessor().filechooserOptions && allBindingsAccessor().filechooserOptions.uploadFile,
  1026. initialPath: _initialPath,
  1027. errorRedirectPath: "",
  1028. forceRefresh: true,
  1029. showExtraHome: allBindingsAccessor().filechooserOptions && allBindingsAccessor().filechooserOptions.showExtraHome,
  1030. extraHomeProperties: allBindingsAccessor().filechooserOptions && allBindingsAccessor().filechooserOptions.extraHomeProperties ? allBindingsAccessor().filechooserOptions.extraHomeProperties : {},
  1031. filterExtensions: allBindingsAccessor().filechooserFilter ? allBindingsAccessor().filechooserFilter : ""
  1032. });
  1033. $("#chooseFile").modal("show");
  1034. $("#chooseFile").on("hidden", function(){
  1035. $("html").removeClass("modal-open");
  1036. });
  1037. }
  1038. function handleChoice(filePath, stripHdfsPrefix) {
  1039. if (allBindingsAccessor().filechooserPrefixSeparator){
  1040. filePath = inputElement.val().split(allBindingsAccessor().filechooserPrefixSeparator)[0] + '=' + filePath;
  1041. }
  1042. if (stripHdfsPrefix){
  1043. inputElement.val(filePath);
  1044. }
  1045. else {
  1046. inputElement.val("hdfs://" + filePath);
  1047. }
  1048. inputElement.change();
  1049. if (typeof valueAccessor() == "function" || typeof valueAccessor().value == "function") {
  1050. if (valueAccessor().value){
  1051. valueAccessor().value(inputElement.val());
  1052. if (valueAccessor().displayJustLastBit){
  1053. inputElement.data("fullPath", inputElement.val());
  1054. inputElement.attr("data-original-title", inputElement.val());
  1055. var _val = inputElement.val();
  1056. inputElement.val(_val.split("/")[_val.split("/").length - 1])
  1057. }
  1058. }
  1059. else {
  1060. valueAccessor()(inputElement.val());
  1061. }
  1062. }
  1063. else {
  1064. valueAccessor(inputElement.val());
  1065. }
  1066. }
  1067. });
  1068. if (allBindingsAccessor().filechooserDisabled){
  1069. _btn.addClass("disabled").attr("disabled", "disabled");
  1070. }
  1071. return _btn;
  1072. }
  1073. ko.bindingHandlers.tooltip = {
  1074. update: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
  1075. var options = ko.utils.unwrapObservable(valueAccessor());
  1076. var self = $(element);
  1077. self.tooltip(options);
  1078. }
  1079. };