common.ko.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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: opt
  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 _isArray = options.update instanceof Array;
  59. var _newVal = $(this).val();
  60. var _type = $(this).data("type");
  61. if ($.trim(_newVal) != "") {
  62. if (_type == "user") {
  63. viewModel.availableHadoopUsers.push({
  64. username: _newVal
  65. });
  66. }
  67. if (_type == "group") {
  68. viewModel.availableHadoopGroups.push({
  69. name: _newVal
  70. });
  71. }
  72. if (_type == "action") {
  73. viewModel.availableActions.push(_newVal);
  74. }
  75. if (_type == "scope") {
  76. viewModel.availablePrivileges.push(_newVal);
  77. }
  78. if (_type == "role") {
  79. var _r = new Role(viewModel, { name: _newVal });
  80. viewModel.tempRoles.push(_r);
  81. viewModel.roles.push(_r);
  82. }
  83. if (_isArray){
  84. var _vals = $(element).select2("val");
  85. _vals.push(_newVal);
  86. $(element).select2("val", _vals, true);
  87. }
  88. else {
  89. $(element).select2("val", _newVal, true);
  90. }
  91. $(element).select2("close");
  92. }
  93. }
  94. });
  95. })
  96. },
  97. update: function (element, valueAccessor, allBindingsAccessor, vm) {
  98. if (typeof valueAccessor().update != "undefined") {
  99. $(element).select2("val", valueAccessor().update());
  100. }
  101. if (typeof valueAccessor().readonly != "undefined") {
  102. $(element).select2("readonly", valueAccessor().readonly);
  103. if (typeof valueAccessor().readonlySetTo != "undefined") {
  104. valueAccessor().readonlySetTo();
  105. }
  106. }
  107. }
  108. };
  109. ko.bindingHandlers.hivechooser = {
  110. init: function(element, valueAccessor, allBindingsAccessor, vm) {
  111. var self = $(element);
  112. self.val(valueAccessor()());
  113. function setPathFromAutocomplete(path){
  114. self.val(path);
  115. valueAccessor()(path);
  116. self.blur();
  117. }
  118. self.on("blur", function () {
  119. valueAccessor()(self.val());
  120. });
  121. self.jHueHiveAutocomplete({
  122. skipColumns: true,
  123. showOnFocus: true,
  124. home: "/",
  125. onPathChange: function (path) {
  126. setPathFromAutocomplete(path);
  127. },
  128. onEnter: function (el) {
  129. setPathFromAutocomplete(el.val());
  130. },
  131. onBlur: function () {
  132. if (self.val().lastIndexOf(".") == self.val().length - 1){
  133. self.val(self.val().substr(0, self.val().length - 1));
  134. }
  135. valueAccessor()(self.val());
  136. }
  137. });
  138. }
  139. }
  140. ko.bindingHandlers.filechooser = {
  141. init: function(element, valueAccessor, allBindingsAccessor, vm) {
  142. var self = $(element);
  143. self.val(valueAccessor()());
  144. self.on("blur", function () {
  145. valueAccessor()(self.val());
  146. });
  147. self.after(getFileBrowseButton(self, true, valueAccessor));
  148. }
  149. };
  150. function getFileBrowseButton(inputElement, selectFolder, valueAccessor) {
  151. return $("<button>").addClass("btn").addClass("fileChooserBtn").text("..").click(function (e) {
  152. e.preventDefault();
  153. // check if it's a relative path
  154. callFileChooser();
  155. function callFileChooser() {
  156. var _initialPath = $.trim(inputElement.val()) != "" ? inputElement.val() : "/";
  157. if (_initialPath.indexOf("hdfs://") > -1){
  158. _initialPath = _initialPath.substring(7);
  159. }
  160. $("#filechooser").jHueFileChooser({
  161. suppressErrors: true,
  162. selectFolder: (selectFolder) ? true : false,
  163. onFolderChoose: function (filePath) {
  164. handleChoice(filePath);
  165. if (selectFolder) {
  166. $("#chooseFile").modal("hide");
  167. }
  168. },
  169. onFileChoose: function (filePath) {
  170. handleChoice(filePath);
  171. $("#chooseFile").modal("hide");
  172. },
  173. createFolder: false,
  174. uploadFile: false,
  175. initialPath: _initialPath,
  176. errorRedirectPath: "",
  177. forceRefresh: true
  178. });
  179. $("#chooseFile").modal("show");
  180. }
  181. function handleChoice(filePath) {
  182. inputElement.val("hdfs://" + filePath);
  183. inputElement.change();
  184. valueAccessor()(inputElement.val());
  185. }
  186. });
  187. }
  188. ko.bindingHandlers.tooltip = {
  189. update: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
  190. var options = ko.utils.unwrapObservable(valueAccessor());
  191. var self = $(element);
  192. self.tooltip(options);
  193. }
  194. };