common.ko.js 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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") {
  26. if (options.update instanceof Array) {
  27. options.update.forEach(function(opt){
  28. if (viewModel.selectableHadoopGroups().indexOf(opt) == -1){
  29. viewModel.availableHadoopGroups.push({
  30. name: options.update
  31. });
  32. }
  33. });
  34. }
  35. else if (viewModel.selectableHadoopGroups().indexOf(options.update) == -1){
  36. viewModel.availableHadoopGroups.push({
  37. name: options.update
  38. });
  39. }
  40. }
  41. if (options.type == "action" && viewModel.availableActions().indexOf(options.update) == -1) {
  42. viewModel.availableActions.push(options.update);
  43. }
  44. if (options.type == "scope" && viewModel.availablePrivileges().indexOf(options.update) == -1) {
  45. viewModel.availablePrivileges.push(options.update);
  46. }
  47. }
  48. $(element)
  49. .select2(options)
  50. .on("change", function (e) {
  51. if (typeof e.val != "undefined" && typeof valueAccessor().update != "undefined") {
  52. valueAccessor().update(e.val);
  53. }
  54. })
  55. .on("select2-open", function () {
  56. $(".select2-input").off("keyup").data("type", options.type).on("keyup", function (e) {
  57. if (e.keyCode === 13) {
  58. var _newVal = $(this).val();
  59. var _type = $(this).data("type");
  60. if (_type == "user") {
  61. viewModel.availableHadoopUsers.push({
  62. username: _newVal
  63. });
  64. }
  65. if (_type == "group") {
  66. viewModel.availableHadoopGroups.push({
  67. name: _newVal
  68. });
  69. }
  70. if (_type == "action") {
  71. viewModel.availableActions.push(_newVal);
  72. }
  73. if (_type == "scope") {
  74. viewModel.availablePrivileges.push(_newVal);
  75. }
  76. $(element).select2("val", _newVal, true);
  77. $(element).select2("close");
  78. }
  79. });
  80. })
  81. },
  82. update: function (element, valueAccessor, allBindingsAccessor, vm) {
  83. if (typeof valueAccessor().update != "undefined") {
  84. $(element).select2("val", valueAccessor().update());
  85. }
  86. }
  87. };