common.ko.js 7.2 KB

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