Эх сурвалжийг харах

[search] Fixed ko.editable to update enabled/disabled status

Enrico Berti 11 жил өмнө
parent
commit
a5e7f67

+ 1 - 1
apps/search/src/search/templates/admin_collection_facets.mako

@@ -35,7 +35,7 @@ ${ commonheader(_('Search'), "search", user, "29px") | n,unicode }
 
     <link href="/static/ext/css/bootstrap-editable.css" rel="stylesheet">
     <script src="/static/ext/js/bootstrap-editable.min.js"></script>
-    <script src="/static/ext/js/knockout.x-editable.js"></script>
+    <script src="/static/js/ko.editable.js"></script>
 
     <form method="POST" class="form-horizontal" data-bind="submit: submit">
       <div class="well">

+ 1 - 1
apps/search/src/search/templates/admin_collection_sorting.mako

@@ -37,7 +37,7 @@ ${ commonheader(_('Search'), "search", user, "29px") | n,unicode }
 
     <link href="/static/ext/css/bootstrap-editable.css" rel="stylesheet">
     <script src="/static/ext/js/bootstrap-editable.min.js"></script>
-    <script src="/static/ext/js/knockout.x-editable.js"></script>
+    <script src="/static/js/ko.editable.js"></script>
 
     <form method="POST" class="form-horizontal" data-bind="submit: submit">
       <div class="well">

+ 2 - 2
apps/search/src/search/templates/search2.mako

@@ -264,7 +264,7 @@ ${ commonheader(_('Search'), "search", user, "80px") | n,unicode }
         &nbsp;
       </span>
       <span data-bind="with: $root.collection.getFacetById(id())">
-        <span data-bind="editable: label"></span>
+        <span data-bind="editable: label, editableOptions: {enabled: $root.isEditing()}"></span>
       </span>
       <!-- ko ifnot: $root.collection.getFacetById(id()) -->
         <span data-bind="text: name"></span>
@@ -800,7 +800,7 @@ ${ commonheader(_('Search'), "search", user, "80px") | n,unicode }
 <script src="/static/ext/js/knockout-min.js" type="text/javascript" charset="utf-8"></script>
 <script src="/static/ext/js/knockout.mapping-2.3.2.js" type="text/javascript" charset="utf-8"></script>
 <script src="/static/ext/js/bootstrap-editable.min.js"></script>
-<script src="/static/ext/js/knockout.x-editable.js"></script>
+<script src="/static/js/ko.editable.js"></script>
 <script src="/search/static/js/shortcut.js" type="text/javascript" charset="utf-8"></script>
 <script src="/search/static/js/freshereditor.min.js" type="text/javascript" charset="utf-8"></script>
 <script src="/static/ext/js/codemirror-3.11.js"></script>

+ 1 - 1
apps/sqoop/src/sqoop/templates/app.mako

@@ -639,7 +639,7 @@ ${ commonheader(None, "sqoop", user) | n,unicode }
 <script src="/static/ext/js/knockout-min.js" type="text/javascript" charset="utf-8"></script>
 <script src="/static/ext/js/knockout.mapping-2.3.2.js" type="text/javascript" charset="utf-8"></script>
 <script src="/static/ext/js/bootstrap-editable.min.js"></script>
-<script src="/static/ext/js/knockout.x-editable.js"></script>
+<script src="/static/js/ko.editable.js"></script>
 <script src="/static/js/jquery.hdfsautocomplete.js" type="text/javascript" charset="utf-8"></script>
 <script src="/sqoop/static/js/cclass.js" type="text/javascript" charset="utf-8"></script>
 <script src="/sqoop/static/js/koify.js" type="text/javascript" charset="utf-8"></script>

+ 0 - 116
desktop/core/static/ext/js/knockout.x-editable.js

@@ -1,116 +0,0 @@
-(function(factory) {
-    if (typeof define === "function" && define.amd) {
-        // AMD anonymous module
-        define(["knockout", "jquery"], factory);
-    } else {
-        // No module loader (plain <script> tag) - put directly in global namespace
-        factory(window.ko, window.jQuery);
-    }
-})(function(ko, $) {
-	ko.bindingHandlers.editable = {
-		init: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
-			var $element = $(element),
-				value = valueAccessor(),
-				allBindings = allBindingsAccessor(),
-				editableOptions = allBindings.editableOptions || {};
-
-			editableOptions.value = ko.utils.unwrapObservable(value);
-
-			if (!editableOptions.name) {
-				$.each(bindingContext.$data, function (k, v) {
-					if (v == value) {
-						editableOptions.name = k;
-						return false;
-					}
-				});
-			}
-			
-			//wrap calls to knockout.validation
-			if (!editableOptions.validate && value.isValid) {
-				editableOptions.validate = function (testValue) {
-					//have to set to new value, then call validate, then reset to original value
-					//not pretty, but works
-					var initalValue = value();
-					value(testValue);
-					var res = value.isValid() ? null : ko.utils.unwrapObservable(value.error);
-					value(initalValue);
-					return res;
-				}
-			}
-
-			if ((editableOptions.type === 'select' || editableOptions.type === 'checklist'|| editableOptions.type === 'typeahead') && !editableOptions.source && editableOptions.options) {
-				if (editableOptions.optionsCaption)
-					editableOptions.prepend = editableOptions.optionsCaption;
-
-				//taken directly from ko.bindingHandlers['options']
-				function applyToObject(object, predicate, defaultValue) {
-					var predicateType = typeof predicate;
-					if (predicateType == "function")    // Given a function; run it against the data value
-						return predicate(object);
-					else if (predicateType == "string") // Given a string; treat it as a property name on the data value
-						return object[predicate];
-					else                                // Given no optionsText arg; use the data value itself
-						return defaultValue;
-				}
-
-				editableOptions.source = function() {
-					return ko.utils.arrayMap(editableOptions.options(), function (item) {
-						var optionValue = applyToObject(item, editableOptions.optionsValue, item);
-						var optionText = applyToObject(item, editableOptions.optionsText, optionText);
-
-						return {
-							value: ko.utils.unwrapObservable(optionValue),
-							text: ko.utils.unwrapObservable(optionText)
-						};
-					});
-				}
-			}
-
-			if (editableOptions.visible && ko.isObservable(editableOptions.visible)) {
-				editableOptions.toggle = 'manual';
-			}
-			
-			//create editable
-			var $editable = $element.editable(editableOptions);
-
-			//update observable on save
-			if (ko.isObservable(value)) {
-				$editable.on('save.ko', function (e, params) {
-					value(params.newValue);
-				})
-			};
-
-			if (editableOptions.save) {
-				$editable.on('save', editableOptions.save);
-			}
-			
-			//setup observable to fire only when editable changes, not when options change
-			//http://www.knockmeout.net/2012/06/knockoutjs-performance-gotcha-3-all-bindings.html
-			ko.computed({
-				read: function () {
-					var val = ko.utils.unwrapObservable(valueAccessor());
-					if (val === null) val = '';
-					$editable.editable('setValue', val, true)
-				},
-				owner: this,
-				disposeWhenNodeIsRemoved: element
-			});
-	
-			if (editableOptions.visible && ko.isObservable(editableOptions.visible)) {
-				ko.computed({
-					read: function () {
-						var val = ko.utils.unwrapObservable(editableOptions.visible());
-						if (val)
-							$editable.editable('show');
-					},
-					owner: this,
-					disposeWhenNodeIsRemoved: element
-				});
-
-				$editable.on('hidden.ko', function (e, params) {
-					editableOptions.visible(false);
-				});
-			}
-		}
-	};
-});

+ 143 - 0
desktop/core/static/js/ko.editable.js

@@ -0,0 +1,143 @@
+// Licensed to Cloudera, Inc. under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  Cloudera, Inc. licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+// based on the original https://github.com/brianchance/knockout-x-editable
+
+(function (factory) {
+  if (typeof define === "function" && define.amd) {
+    // AMD anonymous module
+    define(["knockout", "jquery"], factory);
+  } else {
+    // No module loader (plain <script> tag) - put directly in global namespace
+    factory(window.ko, window.jQuery);
+  }
+})(function (ko, $) {
+  ko.bindingHandlers.editable = {
+    init: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
+      var $element = $(element),
+          value = valueAccessor(),
+          allBindings = allBindingsAccessor(),
+          editableOptions = allBindings.editableOptions || {};
+
+      editableOptions.value = ko.utils.unwrapObservable(value);
+
+      if (!editableOptions.name) {
+        $.each(bindingContext.$data, function (k, v) {
+          if (v == value) {
+            editableOptions.name = k;
+            return false;
+          }
+        });
+      }
+
+      //wrap calls to knockout.validation
+      if (!editableOptions.validate && value.isValid) {
+        editableOptions.validate = function (testValue) {
+          //have to set to new value, then call validate, then reset to original value
+          //not pretty, but works
+          var initalValue = value();
+          value(testValue);
+          var res = value.isValid() ? null : ko.utils.unwrapObservable(value.error);
+          value(initalValue);
+          return res;
+        }
+      }
+
+      if ((editableOptions.type === 'select' || editableOptions.type === 'checklist' || editableOptions.type === 'typeahead') && !editableOptions.source && editableOptions.options) {
+        if (editableOptions.optionsCaption)
+          editableOptions.prepend = editableOptions.optionsCaption;
+
+        //taken directly from ko.bindingHandlers['options']
+        function applyToObject(object, predicate, defaultValue) {
+          var predicateType = typeof predicate;
+          if (predicateType == "function")    // Given a function; run it against the data value
+            return predicate(object);
+          else if (predicateType == "string") // Given a string; treat it as a property name on the data value
+            return object[predicate];
+          else                                // Given no optionsText arg; use the data value itself
+            return defaultValue;
+        }
+
+        editableOptions.source = function () {
+          return ko.utils.arrayMap(editableOptions.options(), function (item) {
+            var optionValue = applyToObject(item, editableOptions.optionsValue, item);
+            var optionText = applyToObject(item, editableOptions.optionsText, optionText);
+
+            return {
+              value: ko.utils.unwrapObservable(optionValue),
+              text: ko.utils.unwrapObservable(optionText)
+            };
+          });
+        }
+      }
+
+      if (editableOptions.visible && ko.isObservable(editableOptions.visible)) {
+        editableOptions.toggle = 'manual';
+      }
+
+      //create editable
+      var $editable = $element.editable(editableOptions);
+
+      //update observable on save
+      if (ko.isObservable(value)) {
+        $editable.on('save.ko', function (e, params) {
+          value(params.newValue);
+        })
+      }
+      ;
+
+      if (editableOptions.save) {
+        $editable.on('save', editableOptions.save);
+      }
+
+      //setup observable to fire only when editable changes, not when options change
+      //http://www.knockmeout.net/2012/06/knockoutjs-performance-gotcha-3-all-bindings.html
+      ko.computed({
+        read: function () {
+          var _allBindings = ko.utils.unwrapObservable(allBindingsAccessor());
+          var _options = _allBindings.editableOptions || {};
+          if (_options.enabled != null && _options.enabled) {
+            $editable.editable('enable');
+          }
+          else {
+            $editable.editable('disable');
+          }
+          var val = ko.utils.unwrapObservable(valueAccessor());
+          if (val === null) val = '';
+          $editable.editable('setValue', val, true)
+        },
+        owner: this,
+        disposeWhenNodeIsRemoved: element
+      });
+
+      if (editableOptions.visible && ko.isObservable(editableOptions.visible)) {
+        ko.computed({
+          read: function () {
+            var val = ko.utils.unwrapObservable(editableOptions.visible());
+            if (val)
+              $editable.editable('show');
+          },
+          owner: this,
+          disposeWhenNodeIsRemoved: element
+        });
+
+        $editable.on('hidden.ko', function (e, params) {
+          editableOptions.visible(false);
+        });
+      }
+    }
+  };
+});