ko.hue-bindings.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  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: _options.start() ? _options.start() : 0,
  157. max: _options.end() ? _options.end() : 10,
  158. step: _options.gap() ? _options.gap() : 1,
  159. handle: _options.handle ? _options.handle : 'triangle',
  160. start: _options.min(),
  161. end: _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. init: function (element, valueAccessor, allBindingsAccessor, viewModel) {
  182. var DATE_FORMAT = "YYYY-MM-DD";
  183. var TIME_FORMAT = "HH:mm:ss";
  184. var DATETIME_FORMAT = DATE_FORMAT + " " + TIME_FORMAT;
  185. var _el = $(element);
  186. var _options = $.extend(valueAccessor(), {});
  187. var _intervalOptions = [
  188. '<option value="+200MILLISECONDS">200ms</option>',
  189. '<option value="+1SECONDS">1s</option>',
  190. '<option value="+1MINUTES">1m</option>',
  191. '<option value="+5MINUTES">5m</option>',
  192. '<option value="+10MINUTES">10m</option>',
  193. '<option value="+30MINUTES">30m</option>',
  194. '<option value="+1HOURS">1h</option>',
  195. '<option value="+3HOURS">3h</option>',
  196. '<option value="+12HOURS">12h</option>',
  197. '<option value="+1DAYS">1d</option>',
  198. '<option value="+7DAYS">7d</option>',
  199. '<option value="+1MONTHS">1M</option>',
  200. '<option value="+6MONTHS">6M</option>',
  201. '<option value="+1YEARS">1y</option>'
  202. ];
  203. function enableOptions() {
  204. var _opts = [];
  205. var _tmp = $("<div>").html(_intervalOptions.join(""))
  206. $.each(arguments, function (cnt, item) {
  207. if (_tmp.find("option[value='+" + item + "']").length > 0) {
  208. _opts.push('<option value="+' + item + '">' + _tmp.find("option[value='+" + item + "']").eq(0).text() + '</option>');
  209. }
  210. });
  211. return _opts;
  212. }
  213. function renderOptions(opts) {
  214. var _html = "";
  215. for (var i = 0; i < opts.length; i++) {
  216. _html += opts[i];
  217. }
  218. _html += '<option value="">Custom</option>';
  219. return _html;
  220. }
  221. var _tmpl = $('<div class="simpledaterangepicker">' +
  222. '<div class="facet-field-cnt picker">' +
  223. '<div class="facet-field-label facet-field-label-fixed-width">' + KO_DATERANGEPICKER_LABELS.START + '</div>' +
  224. '<div class="input-prepend input-group">' +
  225. '<span class="add-on input-group-addon"><i class="fa fa-calendar"></i></span>' +
  226. '<input type="text" class="input-small form-control start-date" />' +
  227. '</div>' +
  228. '<div class="input-prepend input-group left-margin">' +
  229. '<span class="add-on input-group-addon"><i class="fa fa-clock-o"></i></span>' +
  230. '<input type="text" class="input-mini form-control start-time" />' +
  231. '</div>' +
  232. '</div>' +
  233. '<div class="facet-field-cnt picker">' +
  234. '<div class="facet-field-label facet-field-label-fixed-width">' + KO_DATERANGEPICKER_LABELS.END + '</div>' +
  235. '<div class="input-prepend input-group">' +
  236. '<span class="add-on input-group-addon"><i class="fa fa-calendar"></i></span>' +
  237. '<input type="text" class="input-small form-control end-date" />' +
  238. '</div>' +
  239. '<div class="input-prepend input-group left-margin">' +
  240. '<span class="add-on input-group-addon"><i class="fa fa-clock-o"></i></span>' +
  241. '<input type="text" class="input-mini form-control end-time" />' +
  242. '</div>' +
  243. '</div>' +
  244. '<div class="facet-field-cnt picker">' +
  245. '<div class="facet-field-label facet-field-label-fixed-width">' + KO_DATERANGEPICKER_LABELS.INTERVAL + '</div>' +
  246. '<select class="input-small interval-select" style="margin-right: 6px">' +
  247. renderOptions(_intervalOptions) +
  248. '</select>' +
  249. '<input class="input interval hide" type="text" value="" />' +
  250. '</div>' +
  251. '<div class="facet-field-cnt picker">' +
  252. '<div class="facet-field-label facet-field-label-fixed-width"></div>' +
  253. '<div class="facet-field-switch"><a href="javascript:void(0)"><i class="fa fa-calendar-o"></i> ' + KO_DATERANGEPICKER_LABELS.CUSTOM_FORMAT + '</a></div>' +
  254. '</div>' +
  255. '<div class="facet-field-cnt custom">' +
  256. '<div class="facet-field-label facet-field-label-fixed-width">' + KO_DATERANGEPICKER_LABELS.START + '</div>' +
  257. '<div class="input-prepend input-group">' +
  258. '<span class="add-on input-group-addon"><i class="fa fa-calendar"></i></span>' +
  259. '<input type="text" class="input-large form-control start-date-custom" />' +
  260. '</div>' +
  261. '</div>' +
  262. '<div class="facet-field-cnt custom">' +
  263. '<div class="facet-field-label facet-field-label-fixed-width">' + KO_DATERANGEPICKER_LABELS.END + '</div>' +
  264. '<div class="input-prepend input-group">' +
  265. '<span class="add-on input-group-addon"><i class="fa fa-calendar"></i></span>' +
  266. '<input type="text" class="input-large form-control end-date-custom" />' +
  267. '</div>' +
  268. '</div>' +
  269. '<div class="facet-field-cnt custom">' +
  270. '<div class="facet-field-label facet-field-label-fixed-width">' + KO_DATERANGEPICKER_LABELS.INTERVAL + '</div>' +
  271. '<div class="input-prepend input-group">' +
  272. '<span class="add-on input-group-addon"><i class="fa fa-repeat"></i></span>' +
  273. '<input type="text" class="input-large form-control interval-custom" />' +
  274. '</div>' +
  275. '</div>' +
  276. '<div class="facet-field-cnt custom">' +
  277. '<div class="facet-field-label facet-field-label-fixed-width"></div>' +
  278. '<div class="facet-field-switch"><a href="javascript:void(0)"><i class="fa fa-calendar"></i> ' + KO_DATERANGEPICKER_LABELS.DATE_PICKERS + '</a></div>' +
  279. '</div>' +
  280. '</div>'
  281. );
  282. _tmpl.insertAfter(_el);
  283. var _minMoment = moment(_options.min());
  284. var _maxMoment = moment(_options.max());
  285. var _startMoment = moment(_options.start());
  286. var _endMoment = moment(_options.end());
  287. if (_minMoment.isValid()) {
  288. _tmpl.find(".facet-field-cnt.custom").hide();
  289. _tmpl.find(".facet-field-cnt.picker").show();
  290. _tmpl.find(".start-date").val(_minMoment.format(DATE_FORMAT));
  291. _tmpl.find(".start-time").val(_minMoment.format(TIME_FORMAT));
  292. _tmpl.find(".end-date").val(_maxMoment.format(DATE_FORMAT));
  293. _tmpl.find(".end-time").val(_maxMoment.format(TIME_FORMAT));
  294. _tmpl.find(".interval").val(_options.gap());
  295. _tmpl.find(".interval-custom").val(_options.gap());
  296. }
  297. else {
  298. _tmpl.find(".facet-field-cnt.custom").show();
  299. _tmpl.find(".facet-field-cnt.picker").hide();
  300. _tmpl.find(".start-date-custom").val(_options.min())
  301. _tmpl.find(".end-date-custom").val(_options.max())
  302. _tmpl.find(".interval-custom").val(_options.gap())
  303. }
  304. _tmpl.find(".start-date").datepicker({
  305. format: DATE_FORMAT.toLowerCase()
  306. }).on("changeDate", function () {
  307. rangeHandler(true);
  308. });
  309. _tmpl.find(".start-time").timepicker({
  310. minuteStep: 1,
  311. showSeconds: true,
  312. showMeridian: false,
  313. defaultTime: false
  314. });
  315. _tmpl.find(".end-date").datepicker({
  316. format: DATE_FORMAT.toLowerCase()
  317. }).on("changeDate", function () {
  318. rangeHandler(false);
  319. });
  320. _tmpl.find(".end-time").timepicker({
  321. minuteStep: 1,
  322. showSeconds: true,
  323. showMeridian: false,
  324. defaultTime: false
  325. });
  326. _tmpl.find(".start-time").on("change", function () {
  327. // the timepicker plugin doesn't have a change event handler
  328. // so we need to wait a bit to handle in with the default field event
  329. window.setTimeout(function () {
  330. rangeHandler(true)
  331. }, 200);
  332. });
  333. _tmpl.find(".end-time").on("change", function () {
  334. window.setTimeout(function () {
  335. rangeHandler(false)
  336. }, 200);
  337. });
  338. if (_minMoment.isValid()) {
  339. rangeHandler(true);
  340. }
  341. _tmpl.find(".facet-field-cnt.picker .facet-field-switch a").on("click", function () {
  342. _tmpl.find(".facet-field-cnt.custom").show();
  343. _tmpl.find(".facet-field-cnt.picker").hide();
  344. });
  345. _tmpl.find(".facet-field-cnt.custom .facet-field-switch a").on("click", function () {
  346. _tmpl.find(".facet-field-cnt.custom").hide();
  347. _tmpl.find(".facet-field-cnt.picker").show();
  348. });
  349. _tmpl.find(".start-date-custom").on("change", function () {
  350. _options.min(_tmpl.find(".start-date-custom").val());
  351. _options.start(_options.min());
  352. });
  353. _tmpl.find(".end-date-custom").on("change", function () {
  354. _options.max(_tmpl.find(".end-date-custom").val());
  355. _options.end(_options.max());
  356. });
  357. _tmpl.find(".interval-custom").on("change", function () {
  358. _options.gap(_tmpl.find(".interval-custom").val());
  359. });
  360. function matchIntervals() {
  361. if (_tmpl.find(".interval-select option[value='" + _options.gap() + "']").length > 0) {
  362. _tmpl.find(".interval-select").val(_options.gap());
  363. _tmpl.find(".interval").hide();
  364. }
  365. else {
  366. _tmpl.find(".interval-select").val("");
  367. _tmpl.find(".interval").show();
  368. }
  369. }
  370. _tmpl.find(".interval-select").on("change", function () {
  371. if (_tmpl.find(".interval-select").val() == "") {
  372. _tmpl.find(".interval").show();
  373. }
  374. else {
  375. _tmpl.find(".interval").hide();
  376. _options.gap(_tmpl.find(".interval-select").val());
  377. _tmpl.find(".interval").val(_options.gap());
  378. }
  379. });
  380. _tmpl.find(".interval").on("change", function () {
  381. if (_tmpl.find(".interval-select").val() == "") {
  382. _options.gap(_tmpl.find(".interval").val());
  383. _tmpl.find(".interval-custom").val(_options.gap());
  384. }
  385. });
  386. function rangeHandler(isStart) {
  387. var startDate = moment(_tmpl.find(".start-date").val() + " " + _tmpl.find(".start-time").val(), DATETIME_FORMAT);
  388. var endDate = moment(_tmpl.find(".end-date").val() + " " + _tmpl.find(".end-time").val(), DATETIME_FORMAT);
  389. if (startDate.valueOf() > endDate.valueOf()) {
  390. if (isStart) {
  391. _tmpl.find(".end-date").val(startDate.format(DATE_FORMAT));
  392. _tmpl.find(".end-date").datepicker('setValue', startDate.format(DATE_FORMAT));
  393. _tmpl.find(".end-date").data("original-val", _tmpl.find(".end-date").val());
  394. _tmpl.find(".end-time").val(startDate.format(TIME_FORMAT));
  395. }
  396. else {
  397. if (_tmpl.find(".end-date").val() == _tmpl.find(".start-date").val()) {
  398. _tmpl.find(".end-time").val(startDate.format(TIME_FORMAT));
  399. _tmpl.find(".end-time").data("timepicker").setValues(startDate.format(TIME_FORMAT));
  400. }
  401. else {
  402. _tmpl.find(".end-date").val(_tmpl.find(".end-date").data("original-val"));
  403. _tmpl.find(".end-date").datepicker("setValue", _tmpl.find(".end-date").data("original-val"));
  404. }
  405. // non-sticky error notification
  406. $.jHueNotify.notify({
  407. level: "ERROR",
  408. message: "The end cannot be before the starting moment"
  409. });
  410. }
  411. }
  412. else {
  413. _tmpl.find(".end-date").data("original-val", _tmpl.find(".end-date").val());
  414. _tmpl.find(".start-date").datepicker("hide");
  415. _tmpl.find(".end-date").datepicker("hide");
  416. }
  417. var _calculatedStartDate = moment(_tmpl.find(".start-date").val() + " " + _tmpl.find(".start-time").val(), DATETIME_FORMAT).utc();
  418. var _calculatedEndDate = moment(_tmpl.find(".end-date").val() + " " + _tmpl.find(".end-time").val(), DATETIME_FORMAT).utc();
  419. _options.min(_calculatedStartDate.format("YYYY-MM-DD[T]HH:mm:ss[Z]"));
  420. _options.start(_options.min());
  421. _options.max(_calculatedEndDate.format("YYYY-MM-DD[T]HH:mm:ss[Z]"));
  422. _options.end(_options.max());
  423. _tmpl.find(".start-date-custom").val(_options.min());
  424. _tmpl.find(".end-date-custom").val(_options.max());
  425. var _opts = [];
  426. // hide not useful options from interval
  427. if (_calculatedEndDate.diff(_calculatedStartDate, 'minutes') > 1 && _calculatedEndDate.diff(_calculatedStartDate, 'minutes') <= 60) {
  428. _opts = enableOptions("200MILLISECONDS", "1SECONDS", "1MINUTES", "5MINUTES", "10MINUTES", "30MINUTES");
  429. }
  430. if (_calculatedEndDate.diff(_calculatedStartDate, 'hours') > 1 && _calculatedEndDate.diff(_calculatedStartDate, 'hours') <= 12) {
  431. _opts = enableOptions("5MINUTES", "10MINUTES", "30MINUTES", "1HOURS", "3HOURS");
  432. }
  433. if (_calculatedEndDate.diff(_calculatedStartDate, 'hours') > 12 && _calculatedEndDate.diff(_calculatedStartDate, 'hours') < 36) {
  434. _opts = enableOptions("10MINUTES", "30MINUTES", "1HOURS", "3HOURS", "12HOURS");
  435. }
  436. if (_calculatedEndDate.diff(_calculatedStartDate, 'days') > 1 && _calculatedEndDate.diff(_calculatedStartDate, 'days') <= 7) {
  437. _opts = enableOptions("30MINUTES", "1HOURS", "3HOURS", "12HOURS", "1DAYS");
  438. }
  439. if (_calculatedEndDate.diff(_calculatedStartDate, 'days') > 7 && _calculatedEndDate.diff(_calculatedStartDate, 'days') <= 14) {
  440. _opts = enableOptions("3HOURS", "12HOURS", "1DAYS");
  441. }
  442. if (_calculatedEndDate.diff(_calculatedStartDate, 'days') > 14 && _calculatedEndDate.diff(_calculatedStartDate, 'days') <= 31) {
  443. _opts = enableOptions("12HOURS", "1DAYS", "7DAYS");
  444. }
  445. if (_calculatedEndDate.diff(_calculatedStartDate, 'months') > 1) {
  446. _opts = enableOptions("1DAYS", "7DAYS", "1MONTHS");
  447. }
  448. if (_calculatedEndDate.diff(_calculatedStartDate, 'months') > 6) {
  449. _opts = enableOptions("1DAYS", "7DAYS", "1MONTHS", "6MONTHS");
  450. }
  451. if (_calculatedEndDate.diff(_calculatedStartDate, 'months') > 12) {
  452. _opts = enableOptions("7DAYS", "1MONTHS", "6MONTHS", "1YEARS");
  453. }
  454. $(".interval-select").html(renderOptions(_opts));
  455. matchIntervals();
  456. }
  457. }
  458. }
  459. ko.bindingHandlers.augmenthtml = {
  460. render: function (element, valueAccessor, allBindingsAccessor, viewModel) {
  461. var _val = ko.unwrap(valueAccessor());
  462. var _enc = $("<span>").html(_val);
  463. if (_enc.find("style").length > 0) {
  464. var parser = new less.Parser();
  465. $(_enc.find("style")).each(function (cnt, item) {
  466. var _less = "#result-container {" + $(item).text() + "}";
  467. try {
  468. parser.parse(_less, function (err, tree) {
  469. $(item).text(tree.toCSS());
  470. });
  471. }
  472. catch (e) {
  473. }
  474. });
  475. $(element).html(_enc.html());
  476. }
  477. else {
  478. $(element).html(_val);
  479. }
  480. },
  481. init: function (element, valueAccessor, allBindingsAccessor, viewModel) {
  482. ko.bindingHandlers.augmenthtml.render(element, valueAccessor, allBindingsAccessor, viewModel);
  483. },
  484. update: function (element, valueAccessor, allBindingsAccessor) {
  485. ko.bindingHandlers.augmenthtml.render(element, valueAccessor, allBindingsAccessor, viewModel);
  486. }
  487. }
  488. ko.bindingHandlers.clearable = {
  489. init: function (element, valueAccessor, allBindingsAccessor, viewModel) {
  490. var _el = $(element);
  491. function tog(v) {
  492. return v ? "addClass" : "removeClass";
  493. }
  494. _el.addClass("clearable");
  495. _el.on("input", function () {
  496. _el[tog(this.value)]("x");
  497. valueAccessor()(_el.val());
  498. }).on("mousemove", function (e) {
  499. _el[tog(this.offsetWidth - 18 < e.clientX - this.getBoundingClientRect().left)]("onX");
  500. }).on("click", function (e) {
  501. if (this.offsetWidth - 18 < e.clientX - this.getBoundingClientRect().left) {
  502. _el.removeClass("x onX").val("");
  503. valueAccessor()("");
  504. }
  505. });
  506. },
  507. update: function (element, valueAccessor, allBindingsAccessor) {
  508. $(element).val(ko.unwrap(valueAccessor()));
  509. }
  510. }
  511. ko.bindingHandlers.spinedit = {
  512. init: function (element, valueAccessor, allBindingsAccessor, viewModel) {
  513. $(element).spinedit({
  514. minimum: 0,
  515. maximum: 10000,
  516. step: 5,
  517. value: ko.unwrap(valueAccessor()),
  518. numberOfDecimals: 0
  519. });
  520. $(element).on("valueChanged", function (e) {
  521. valueAccessor()(e.value);
  522. });
  523. },
  524. update: function (element, valueAccessor, allBindingsAccessor) {
  525. $(element).spinedit("setValue", ko.unwrap(valueAccessor()));
  526. }
  527. }
  528. ko.bindingHandlers.codemirror = {
  529. init: function (element, valueAccessor, allBindingsAccessor, viewModel) {
  530. var options = $.extend(valueAccessor(), {});
  531. var editor = CodeMirror.fromTextArea(element, options);
  532. element.editor = editor;
  533. editor.setValue(options.data());
  534. editor.refresh();
  535. var wrapperElement = $(editor.getWrapperElement());
  536. $(document).on("refreshCodemirror", function () {
  537. editor.setSize("100%", 300);
  538. editor.refresh();
  539. });
  540. $(document).on("addFieldToSource", function (e, field) {
  541. if ($(element).data("template")) {
  542. editor.replaceSelection("{{" + field.name() + "}}");
  543. }
  544. });
  545. $(document).on("addFunctionToSource", function (e, fn) {
  546. if ($(element).data("template")) {
  547. editor.replaceSelection(fn);
  548. }
  549. });
  550. $(".chosen-select").chosen({
  551. disable_search_threshold: 10,
  552. width: "75%"
  553. });
  554. $('.chosen-select').trigger('chosen:updated');
  555. var sourceDelay = -1;
  556. editor.on("change", function (cm) {
  557. clearTimeout(sourceDelay);
  558. var _cm = cm;
  559. sourceDelay = setTimeout(function () {
  560. valueAccessor().data(_cm.getValue());
  561. if ($(".widget-html-pill").parent().hasClass("active")) {
  562. $("[contenteditable=true]").html(stripHtmlFromFunctions(valueAccessor().data()));
  563. }
  564. }, 100);
  565. });
  566. ko.utils.domNodeDisposal.addDisposeCallback(element, function () {
  567. wrapperElement.remove();
  568. });
  569. },
  570. update: function (element, valueAccessor, allBindingsAccessor) {
  571. var editor = element.editor;
  572. editor.refresh();
  573. }
  574. };