common.ko.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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.select2 = {
  17. init: function (element, valueAccessor, allBindingsAccessor, vm) {
  18. var options = ko.toJS(valueAccessor()) || {};
  19. if (typeof valueAccessor().update != "undefined") {
  20. if (options.type == "user" && viewModel.selectableHadoopUsers().indexOf(options.update) == -1) {
  21. viewModel.availableHadoopUsers.push({
  22. username: options.update
  23. });
  24. }
  25. if (options.type == "group" && viewModel.selectableHadoopGroups().indexOf(options.update) == -1) {
  26. viewModel.availableHadoopGroups.push({
  27. name: options.update
  28. });
  29. }
  30. }
  31. $(element)
  32. .select2(options)
  33. .on("change", function (e) {
  34. if (typeof e.val != "undefined" && typeof valueAccessor().update != "undefined") {
  35. valueAccessor().update(e.val);
  36. }
  37. })
  38. .on("select2-open", function () {
  39. $(".select2-input").off("keyup").data("type", options.type).on("keyup", function (e) {
  40. if (e.keyCode === 13) {
  41. var _newVal = $(this).val();
  42. var _type = $(this).data("type");
  43. if (_type == "user") {
  44. viewModel.availableHadoopUsers.push({
  45. username: _newVal
  46. });
  47. }
  48. if (_type == "group") {
  49. viewModel.availableHadoopGroups.push({
  50. name: _newVal
  51. });
  52. }
  53. $(element).select2("val", _newVal, true);
  54. $(element).select2("close");
  55. }
  56. });
  57. })
  58. },
  59. update: function (element, valueAccessor, allBindingsAccessor, vm) {
  60. if (typeof valueAccessor().update != "undefined") {
  61. $(element).select2("val", valueAccessor().update());
  62. }
  63. }
  64. };