common.ko.js 3.9 KB

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