common.ko.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. if (options.type == "action" && viewModel.availableActions().indexOf(options.update) == -1) {
  31. viewModel.availableActions.push(options.update);
  32. }
  33. }
  34. $(element)
  35. .select2(options)
  36. .on("change", function (e) {
  37. if (typeof e.val != "undefined" && typeof valueAccessor().update != "undefined") {
  38. valueAccessor().update(e.val);
  39. }
  40. })
  41. .on("select2-open", function () {
  42. $(".select2-input").off("keyup").data("type", options.type).on("keyup", function (e) {
  43. if (e.keyCode === 13) {
  44. var _newVal = $(this).val();
  45. var _type = $(this).data("type");
  46. if (_type == "user") {
  47. viewModel.availableHadoopUsers.push({
  48. username: _newVal
  49. });
  50. }
  51. if (_type == "group") {
  52. viewModel.availableHadoopGroups.push({
  53. name: _newVal
  54. });
  55. }
  56. if (_type == "action") {
  57. viewModel.availableActions.push(_newVal);
  58. }
  59. $(element).select2("val", _newVal, true);
  60. $(element).select2("close");
  61. }
  62. });
  63. })
  64. },
  65. update: function (element, valueAccessor, allBindingsAccessor, vm) {
  66. if (typeof valueAccessor().update != "undefined") {
  67. $(element).select2("val", valueAccessor().update());
  68. }
  69. }
  70. };