Merge branch '8784-dir-listings'
[arvados.git] / apps / workbench / app / assets / javascripts / editable.js
index 8eea1693e8721efcf401c4c2c3c55bb0205180d1..939506c2ecc56e18486673af42d913b2ff039a7d 100644 (file)
@@ -1,3 +1,7 @@
+// Copyright (C) The Arvados Authors. All rights reserved.
+//
+// SPDX-License-Identifier: AGPL-3.0
+
 $.fn.editable.defaults.ajaxOptions = {type: 'post', dataType: 'json'};
 $.fn.editable.defaults.send = 'always';
 
@@ -9,6 +13,10 @@ $.fn.editable.defaults.send = 'always';
 // too narrow, when the popup box will just move to do the right thing.
 //$.fn.editable.defaults.mode = 'inline';
 
+$.fn.editable.defaults.success = function (response, newValue) {
+    $(document).trigger('editable:success', [this, response, newValue]);
+};
+
 $.fn.editable.defaults.params = function (params) {
     var a = {};
     var key = params.pk.key;
@@ -37,10 +45,9 @@ $.fn.editable.defaults.validate = function (value) {
 
 $(document).
     on('ready ajax:complete', function() {
-        $('#editable-submit').click(function() {
-            console.log($(this));
-        });
         $('.editable').
+            not('.editable-done-setup').
+            addClass('editable-done-setup').
             editable({
                 success: function(response, newValue) {
                     // If we just created a new object, stash its UUID
@@ -52,7 +59,25 @@ $(document).
                     if (response.href) {
                         $(this).editable('option', 'url', response.href);
                     }
+                    if ($(this).attr('data-name')) {
+                        var textileAttr = $(this).attr('data-name') + 'Textile';
+                        if (response[textileAttr]) {
+                            $(this).attr('data-textile', response[textileAttr]);
+                        }
+                    }
                     return;
+                },
+                error: function(response, newValue) {
+                    var errlist = response.responseJSON.errors;
+                    var errmsg;
+                    if (Array.isArray(errlist)) {
+                        errmsg = errlist.join();
+                    } else {
+                        errmsg = ("The server returned an error when making " +
+                                  "this update (status " + response.status +
+                                  ": " + errlist + ").");
+                    }
+                    return errmsg;
                 }
             }).
             on('hidden', function(e, reason) {
@@ -60,6 +85,10 @@ $(document).
                 // information if it appears elsewhere on the page.
                 if (reason != 'save') return;
                 var html = $(this).html();
+                if( $(this).attr('data-textile') ) {
+                    html = $(this).attr('data-textile');
+                    $(this).html(html);
+                }
                 var uuid = $(this).attr('data-object-uuid');
                 var attr = $(this).attr('data-name');
                 var edited = this;
@@ -71,6 +100,15 @@ $(document).
                       });
                 }
             });
+    }).
+    on('ready ajax:complete', function() {
+        $("[data-toggle~='x-editable']").
+            not('.editable-done-setup').
+            addClass('editable-done-setup').
+            click(function(e) {
+                e.stopPropagation();
+                $($(this).attr('data-toggle-selector')).editable('toggle');
+            });
     });
 
 $.fn.editabletypes.text.defaults.tpl = '<input type="text" name="editable-text">'