ko.hue-bindings.js 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264
  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="hover" 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. '&nbsp;&nbsp;<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, valueAccessor, allBindings, viewModel, bindingContext){
  686. var $element = $(element);
  687. var options = ko.unwrap(valueAccessor());
  688. if (typeof options === 'object')
  689. $element.chosen(options);
  690. else
  691. $element.chosen();
  692. ['options', 'selectedOptions', 'value'].forEach(function(propName){
  693. if (allBindings.has(propName)){
  694. var prop = allBindings.get(propName);
  695. if (ko.isObservable(prop)){
  696. prop.subscribe(function(){
  697. $element.trigger('chosen:updated');
  698. });
  699. }
  700. }
  701. });
  702. }
  703. }
  704. ko.bindingHandlers.tooltip = {
  705. init: function (element, valueAccessor) {
  706. var local = ko.utils.unwrapObservable(valueAccessor()),
  707. options = {};
  708. ko.utils.extend(options, local);
  709. $(element).tooltip(options);
  710. ko.utils.domNodeDisposal.addDisposeCallback(element, function () {
  711. $(element).tooltip("destroy");
  712. });
  713. },
  714. update: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
  715. var options = ko.utils.unwrapObservable(valueAccessor());
  716. var self = $(element);
  717. self.tooltip(options);
  718. }
  719. };
  720. ko.bindingHandlers.typeahead = {
  721. init: function (element, valueAccessor) {
  722. var binding = this;
  723. var elem = $(element);
  724. var valueAccessor = valueAccessor();
  725. var _options = {
  726. source: function () {
  727. var _source = ko.utils.unwrapObservable(valueAccessor.source);
  728. if (valueAccessor.extraKeywords) {
  729. _source = _source.concat(valueAccessor.extraKeywords.split(" "))
  730. }
  731. if (valueAccessor.sourceSuffix && _source) {
  732. var _tmp = [];
  733. _source.forEach(function(item){
  734. _tmp.push(item + valueAccessor.sourceSuffix);
  735. });
  736. _source = _tmp;
  737. }
  738. return _source;
  739. },
  740. onselect: function (val) {
  741. if (typeof valueAccessor.target == "function") {
  742. valueAccessor.target(val);
  743. }
  744. else {
  745. valueAccessor.target = val;
  746. }
  747. }
  748. }
  749. function extractor(query, extractorSeparator) {
  750. var result = /([^ ]+)$/.exec(query);
  751. if (extractorSeparator){
  752. result = new RegExp("([^\\" + extractorSeparator + "]+)$").exec(query);
  753. }
  754. if (result && result[1])
  755. return result[1].trim();
  756. return "";
  757. }
  758. if (valueAccessor.multipleValues) {
  759. _options.updater = function (item) {
  760. var _val = this.$element.val();
  761. var _separator = (valueAccessor.multipleValuesSeparator || ":");
  762. if (valueAccessor.extraKeywords && valueAccessor.extraKeywords.split(" ").indexOf(item) > -1) {
  763. _separator = "";
  764. }
  765. if (_val.indexOf((valueAccessor.multipleValuesExtractor || " ")) > -1) {
  766. return _val.substring(0, _val.lastIndexOf((valueAccessor.multipleValuesExtractor || " "))) + (valueAccessor.multipleValuesExtractor || " ") + item + _separator;
  767. }
  768. else {
  769. return item + _separator;
  770. }
  771. }
  772. _options.matcher = function (item) {
  773. var _tquery = extractor(this.query, valueAccessor.multipleValuesExtractor);
  774. if (!_tquery) return false;
  775. return ~item.toLowerCase().indexOf(_tquery.toLowerCase());
  776. },
  777. _options.highlighter = function (item) {
  778. var _query = extractor(this.query, valueAccessor.multipleValuesExtractor).replace(/[\-\[\]{}()*+?.:\\\^$|#\s]/g, '\\$&');
  779. return item.replace(new RegExp('(' + _query + ')', 'ig'), function ($1, match) {
  780. return '<strong>' + match + '</strong>'
  781. });
  782. }
  783. }
  784. if (valueAccessor.completeSolrRanges) {
  785. elem.on("keyup", function (e) {
  786. if (e.keyCode != 8 && e.which != 8 && elem.val() && (elem.val().slice(-1) == "[" || elem.val().slice(-1) == "{")) {
  787. var _index = elem.val().length;
  788. elem.val(elem.val() + " TO " + (elem.val().slice(-1) == "[" ? "]" : "}"));
  789. if (element.createTextRange) {
  790. var range = element.createTextRange();
  791. range.move("character", _index);
  792. range.select();
  793. } else if (element.selectionStart != null) {
  794. element.focus();
  795. element.setSelectionRange(_index, _index);
  796. }
  797. }
  798. });
  799. }
  800. if (valueAccessor.triggerOnFocus) {
  801. _options.minLength = 0;
  802. }
  803. elem.typeahead(_options);
  804. if (valueAccessor.triggerOnFocus) {
  805. elem.on('focus', function () {
  806. elem.trigger("keyup");
  807. });
  808. }
  809. elem.blur(function () {
  810. if (typeof valueAccessor.target == "function") {
  811. valueAccessor.target(elem.val());
  812. }
  813. else {
  814. valueAccessor.target = elem.val();
  815. }
  816. });
  817. },
  818. update: function (element, valueAccessor) {
  819. var elem = $(element);
  820. var value = valueAccessor();
  821. if (typeof value.target == "function") {
  822. elem.val(value.target());
  823. }
  824. else {
  825. elem.val(value.target);
  826. }
  827. }
  828. };
  829. ko.bindingHandlers.select2 = {
  830. init: function (element, valueAccessor, allBindingsAccessor, vm) {
  831. var options = ko.toJS(valueAccessor()) || {};
  832. if (typeof valueAccessor().update != "undefined") {
  833. if (options.type == "user" && viewModel.selectableHadoopUsers().indexOf(options.update) == -1) {
  834. viewModel.availableHadoopUsers.push({
  835. username: options.update
  836. });
  837. }
  838. if (options.type == "group") {
  839. if (options.update instanceof Array) {
  840. options.update.forEach(function (opt) {
  841. if (viewModel.selectableHadoopGroups().indexOf(opt) == -1) {
  842. viewModel.availableHadoopGroups.push({
  843. name: opt
  844. });
  845. }
  846. });
  847. }
  848. else if (viewModel.selectableHadoopGroups().indexOf(options.update) == -1) {
  849. viewModel.availableHadoopGroups.push({
  850. name: options.update
  851. });
  852. }
  853. }
  854. if (options.type == "action" && viewModel.availableActions().indexOf(options.update) == -1) {
  855. viewModel.availableActions.push(options.update);
  856. }
  857. if (options.type == "scope" && viewModel.availablePrivileges().indexOf(options.update) == -1) {
  858. viewModel.availablePrivileges.push(options.update);
  859. }
  860. if (options.type == "parameter" && options.update != "") {
  861. var _found = false;
  862. allBindingsAccessor().options().forEach(function(opt){
  863. if (opt[allBindingsAccessor().optionsValue]() == options.update){
  864. _found = true;
  865. }
  866. });
  867. if (!_found){
  868. allBindingsAccessor().options.push({
  869. name: ko.observable(options.update),
  870. value: ko.observable(options.update)
  871. });
  872. }
  873. }
  874. }
  875. $(element)
  876. .select2(options)
  877. .on("change", function (e) {
  878. if (typeof e.val != "undefined" && typeof valueAccessor().update != "undefined") {
  879. valueAccessor().update(e.val);
  880. }
  881. })
  882. .on("select2-focus", function (e) {
  883. if (typeof options.onFocus != "undefined") {
  884. options.onFocus();
  885. }
  886. })
  887. .on("select2-blur", function (e) {
  888. if (typeof options.onBlur != "undefined") {
  889. options.onBlur();
  890. }
  891. })
  892. .on("select2-open", function () {
  893. $(".select2-input").off("keyup").data("type", options.type).on("keyup", function (e) {
  894. if (e.keyCode === 13) {
  895. var _isArray = options.update instanceof Array;
  896. var _newVal = $(this).val();
  897. var _type = $(this).data("type");
  898. if ($.trim(_newVal) != "") {
  899. if (_type == "user") {
  900. viewModel.availableHadoopUsers.push({
  901. username: _newVal
  902. });
  903. }
  904. if (_type == "group") {
  905. viewModel.availableHadoopGroups.push({
  906. name: _newVal
  907. });
  908. }
  909. if (_type == "action") {
  910. viewModel.availableActions.push(_newVal);
  911. }
  912. if (_type == "scope") {
  913. viewModel.availablePrivileges.push(_newVal);
  914. }
  915. if (_type == "role") {
  916. var _r = new Role(viewModel, { name: _newVal });
  917. viewModel.tempRoles.push(_r);
  918. viewModel.roles.push(_r);
  919. }
  920. if (_type == "parameter") {
  921. var _found = false;
  922. allBindingsAccessor().options().forEach(function(opt){
  923. if (opt[allBindingsAccessor().optionsValue]() == _newVal){
  924. _found = true;
  925. }
  926. });
  927. if (!_found){
  928. allBindingsAccessor().options.push({
  929. name: ko.observable(_newVal),
  930. value: ko.observable(_newVal)
  931. });
  932. }
  933. }
  934. if (_isArray) {
  935. var _vals = $(element).select2("val");
  936. _vals.push(_newVal);
  937. $(element).select2("val", _vals, true);
  938. }
  939. else {
  940. $(element).select2("val", _newVal, true);
  941. }
  942. $(element).select2("close");
  943. }
  944. }
  945. });
  946. })
  947. },
  948. update: function (element, valueAccessor, allBindingsAccessor, vm) {
  949. if (typeof allBindingsAccessor().visible != "undefined"){
  950. if ((typeof allBindingsAccessor().visible == "boolean" && allBindingsAccessor().visible) || (typeof allBindingsAccessor().visible == "function" && allBindingsAccessor().visible())) {
  951. $(element).select2("container").show();
  952. }
  953. else {
  954. $(element).select2("container").hide();
  955. }
  956. }
  957. if (typeof valueAccessor().update != "undefined") {
  958. $(element).select2("val", valueAccessor().update());
  959. }
  960. if (typeof valueAccessor().readonly != "undefined") {
  961. $(element).select2("readonly", valueAccessor().readonly);
  962. if (typeof valueAccessor().readonlySetTo != "undefined") {
  963. valueAccessor().readonlySetTo();
  964. }
  965. }
  966. }
  967. };
  968. ko.bindingHandlers.hivechooser = {
  969. init: function (element, valueAccessor, allBindingsAccessor, vm) {
  970. var self = $(element);
  971. self.val(valueAccessor()());
  972. function setPathFromAutocomplete(path) {
  973. self.val(path);
  974. valueAccessor()(path);
  975. self.blur();
  976. }
  977. self.on("blur", function () {
  978. valueAccessor()(self.val());
  979. });
  980. self.jHueHiveAutocomplete({
  981. skipColumns: true,
  982. showOnFocus: true,
  983. home: "/",
  984. onPathChange: function (path) {
  985. setPathFromAutocomplete(path);
  986. },
  987. onEnter: function (el) {
  988. setPathFromAutocomplete(el.val());
  989. },
  990. onBlur: function () {
  991. if (self.val().lastIndexOf(".") == self.val().length - 1) {
  992. self.val(self.val().substr(0, self.val().length - 1));
  993. }
  994. valueAccessor()(self.val());
  995. }
  996. });
  997. }
  998. }
  999. ko.bindingHandlers.hdfsAutocomplete = {
  1000. init: function (element, valueAccessor, allBindingsAccessor, vm) {
  1001. var stripHashes = function (str) {
  1002. return str.replace(/#/gi, encodeURIComponent("#"));
  1003. };
  1004. var self = $(element);
  1005. self.attr("autocomplete", "off");
  1006. self.jHueHdfsAutocomplete({});
  1007. }
  1008. };
  1009. ko.bindingHandlers.filechooser = {
  1010. init: function (element, valueAccessor, allBindingsAccessor, vm) {
  1011. var self = $(element);
  1012. self.attr("autocomplete", "off");
  1013. if (typeof valueAccessor() == "function" || typeof valueAccessor().value == "function") {
  1014. self.val(valueAccessor().value ? valueAccessor().value(): valueAccessor()());
  1015. self.data("fullPath", self.val());
  1016. self.attr("data-original-title", self.val());
  1017. if (valueAccessor().displayJustLastBit){
  1018. var _val = self.val();
  1019. self.val(_val.split("/")[_val.split("/").length - 1]);
  1020. }
  1021. self.on("blur", function () {
  1022. if (valueAccessor().value){
  1023. if (valueAccessor().displayJustLastBit){
  1024. var _val = self.data("fullPath");
  1025. valueAccessor().value(_val.substr(0, _val.lastIndexOf("/")) + "/" + self.val());
  1026. }
  1027. else {
  1028. valueAccessor().value(self.val());
  1029. }
  1030. self.data("fullPath", valueAccessor().value());
  1031. }
  1032. else {
  1033. valueAccessor()(self.val());
  1034. self.data("fullPath", valueAccessor()());
  1035. }
  1036. self.attr("data-original-title", self.data("fullPath"));
  1037. });
  1038. }
  1039. else {
  1040. self.val(valueAccessor());
  1041. self.on("blur", function () {
  1042. valueAccessor(self.val());
  1043. });
  1044. }
  1045. self.after(getFileBrowseButton(self, true, valueAccessor, true, allBindingsAccessor));
  1046. }
  1047. };
  1048. function getFileBrowseButton(inputElement, selectFolder, valueAccessor, stripHdfsPrefix, allBindingsAccessor) {
  1049. var _btn = $("<button>").addClass("btn").addClass("fileChooserBtn").text("..").click(function (e) {
  1050. e.preventDefault();
  1051. $("html").addClass("modal-open");
  1052. // check if it's a relative path
  1053. callFileChooser();
  1054. function callFileChooser() {
  1055. var _initialPath = $.trim(inputElement.val()) != "" ? inputElement.val() : "/";
  1056. if ((allBindingsAccessor && allBindingsAccessor().filechooserOptions && allBindingsAccessor().filechooserOptions.skipInitialPathIfEmpty && inputElement.val() == "") || (allBindingsAccessor && allBindingsAccessor().filechooserPrefixSeparator)){
  1057. _initialPath = "";
  1058. }
  1059. if (inputElement.data("fullPath")){
  1060. _initialPath = inputElement.data("fullPath");
  1061. }
  1062. if (_initialPath.indexOf("hdfs://") > -1) {
  1063. _initialPath = _initialPath.substring(7);
  1064. }
  1065. $("#filechooser").jHueFileChooser({
  1066. suppressErrors: true,
  1067. selectFolder: (selectFolder) ? true : false,
  1068. onFolderChoose: function (filePath) {
  1069. handleChoice(filePath, stripHdfsPrefix);
  1070. if (selectFolder) {
  1071. $("#chooseFile").modal("hide");
  1072. }
  1073. },
  1074. onFileChoose: function (filePath) {
  1075. handleChoice(filePath, stripHdfsPrefix);
  1076. $("#chooseFile").modal("hide");
  1077. },
  1078. createFolder: allBindingsAccessor && allBindingsAccessor().filechooserOptions && allBindingsAccessor().filechooserOptions.createFolder,
  1079. uploadFile: allBindingsAccessor && allBindingsAccessor().filechooserOptions && allBindingsAccessor().filechooserOptions.uploadFile,
  1080. initialPath: _initialPath,
  1081. errorRedirectPath: "",
  1082. forceRefresh: true,
  1083. showExtraHome: allBindingsAccessor && allBindingsAccessor().filechooserOptions && allBindingsAccessor().filechooserOptions.showExtraHome,
  1084. extraHomeProperties: allBindingsAccessor && allBindingsAccessor().filechooserOptions && allBindingsAccessor().filechooserOptions.extraHomeProperties ? allBindingsAccessor().filechooserOptions.extraHomeProperties : {},
  1085. filterExtensions: allBindingsAccessor && allBindingsAccessor().filechooserFilter ? allBindingsAccessor().filechooserFilter : ""
  1086. });
  1087. $("#chooseFile").modal("show");
  1088. $("#chooseFile").on("hidden", function(){
  1089. $("html").removeClass("modal-open");
  1090. });
  1091. }
  1092. function handleChoice(filePath, stripHdfsPrefix) {
  1093. if (allBindingsAccessor && allBindingsAccessor().filechooserPrefixSeparator){
  1094. filePath = inputElement.val().split(allBindingsAccessor().filechooserPrefixSeparator)[0] + '=' + filePath;
  1095. }
  1096. if (stripHdfsPrefix){
  1097. inputElement.val(filePath);
  1098. }
  1099. else {
  1100. inputElement.val("hdfs://" + filePath);
  1101. }
  1102. inputElement.change();
  1103. if (valueAccessor){
  1104. if (typeof valueAccessor() == "function" || typeof valueAccessor().value == "function") {
  1105. if (valueAccessor().value){
  1106. valueAccessor().value(inputElement.val());
  1107. if (valueAccessor().displayJustLastBit){
  1108. inputElement.data("fullPath", inputElement.val());
  1109. inputElement.attr("data-original-title", inputElement.val());
  1110. var _val = inputElement.val();
  1111. inputElement.val(_val.split("/")[_val.split("/").length - 1])
  1112. }
  1113. }
  1114. else {
  1115. valueAccessor()(inputElement.val());
  1116. }
  1117. }
  1118. else {
  1119. valueAccessor(inputElement.val());
  1120. }
  1121. }
  1122. }
  1123. });
  1124. if (allBindingsAccessor && allBindingsAccessor().filechooserDisabled){
  1125. _btn.addClass("disabled").attr("disabled", "disabled");
  1126. }
  1127. return _btn;
  1128. }
  1129. ko.bindingHandlers.datepicker = {
  1130. init: function(element, valueAccessor, allBindings, viewModel, bindingContext){
  1131. var DATE_FORMAT = "YYYY-MM-DD";
  1132. var TIME_FORMAT = "HH:mm:ss";
  1133. var DATETIME_FORMAT = DATE_FORMAT + " " + TIME_FORMAT;
  1134. var _el = $(element);
  1135. var options = ko.unwrap(valueAccessor());
  1136. _el.datepicker({
  1137. format: DATE_FORMAT.toLowerCase()
  1138. }).on("changeDate", function () {
  1139. allBindings().value(_el.val());
  1140. });
  1141. }
  1142. }
  1143. ko.bindingHandlers.timepicker = {
  1144. init: function(element, valueAccessor, allBindings, viewModel, bindingContext){
  1145. var DATE_FORMAT = "YYYY-MM-DD";
  1146. var TIME_FORMAT = "HH:mm:ss";
  1147. var DATETIME_FORMAT = DATE_FORMAT + " " + TIME_FORMAT;
  1148. var _el = $(element);
  1149. var options = ko.unwrap(valueAccessor());
  1150. _el.timepicker({
  1151. minuteStep: 1,
  1152. showSeconds: true,
  1153. showMeridian: false,
  1154. defaultTime: false
  1155. });
  1156. }
  1157. }