ko.hue-bindings.js 43 KB

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