Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lazy ajax calls for virtual area (Complete) #2071

Closed
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 35 additions & 0 deletions lib/modules/apostrophe-areas/public/js/user.js
Expand Up @@ -323,6 +323,41 @@ apos.define('apostrophe-areas', {
return editors;
};

// Loads the virtual areas inside a modal.
self.loadVirtualArea = function () {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be plural.

var $virtualAreas = $('[data-virtual-area]');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No global jquery searches, should be scoped via $el.find() which should be an argument.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually this whole thing should be a method of apostrophe-modal since that's where the action is. Then you can enable/disable the save operation neatly rather than adding and removing classes globally.

var virtualAreaLength = $virtualAreas.length;
var virtualAreaLoadedCount = 0;
// Do nothing if there are no virtual areas
if ($virtualAreas.length > 0) {
// Disable the modal controls till every area is loaded. Clicking on Save fails without
// loading the virtual area.
apos.modalSupport.toggleModalControls(true);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be a method of modals, which will be easy to call as self.toggleControls if this entire algorithm moves into a method of modals.

$virtualAreas.each(function (index, element) {
var $fieldset = $(element);
var areaAttribute = $fieldset.attr('data-virtual-area');
if (!areaAttribute) return;
var area = JSON.parse(areaAttribute);
// Remove the data-attribute after extracting data.
$fieldset.removeAttr('data-virtual-area');
// Call the Virtual Area Route.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe that's apos.areas.action, you don't have to parse it out. A little surprised it's there in JSON, I wouldn't depend on it.

$.jsonCall(area.action,
{ dataType: 'html' },
{ items: area.items, options: area.options }, function (data) {
var $editView = apos.schemas.findSafe($fieldset, '[data-' + area.name + '-edit-view]');
$editView.append(data);
// Make sure slideshows, videos, etc. get their JS
apos.emit('enhance', $editView);
virtualAreaLoadedCount++;
// Enalbe the modal controls once all the virtual areas load.
if (virtualAreaLength === virtualAreaLoadedCount) {
apos.modalSupport.toggleModalControls(false);
}
});
});
}
};

// Gives all area editors a chance to save changes,
// if they need to, before invoking the callback.
//
Expand Down
10 changes: 10 additions & 0 deletions lib/modules/apostrophe-modal/public/js/modal.js
Expand Up @@ -412,6 +412,7 @@ apos.define('apostrophe-modal', {
});
self.resizeContentHeight();
self.focusFirstFormElement();
apos.areas.loadVirtualArea();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pass in self.$el so you can use find() with a suitable scope.

self.shown = true;
};

Expand Down Expand Up @@ -1064,5 +1065,14 @@ apos.modalSupport = {
if (topModal.is('[data-modal]')) {
topModal.trigger('aposModalCancel');
}
},
// Function to disable/enable modal controls. In the disabled state
// controls won't be clickable.
toggleModalControls: function (disable) {
if (disable) {
$('.apos-modal-controls').children().addClass('apos-container-disabled');
} else {
$('.apos-modal-controls').children().removeClass('apos-container-disabled');
}
}
};
2 changes: 2 additions & 0 deletions lib/modules/apostrophe-pages/public/js/editor.js
Expand Up @@ -206,6 +206,8 @@ apos.define('apostrophe-pages-editor', {
apos.emit('enhance', self.$el.find('[data-page-schema-fields]'));
self.addTypeChangeHandler();
self.type = type;
// When we are populating fields during afterShow or during an AJAX Refresh we need to load the virtual areas again.
apos.areas.loadVirtualArea();
});
};
}
Expand Down
2 changes: 2 additions & 0 deletions lib/modules/apostrophe-schemas/public/js/array-modal.js
Expand Up @@ -168,6 +168,8 @@ apos.define('apostrophe-array-editor-modal', {
if (err) {
apos.utils.error(err);
}
// Load the virtual areas if they are present.
apos.areas.loadVirtualArea();
self.refresh();
if (self.errorPath && (self.errorPath.length > 1)) {
apos.schemas.returnToError($form, self.field.schema, self.errorPath.slice(1), self.error, function(err) {
Expand Down
18 changes: 9 additions & 9 deletions lib/modules/apostrophe-schemas/public/js/user.js
Expand Up @@ -302,15 +302,15 @@ apos.define('apostrophe-schemas', {
items = area;
}
var $fieldset = self.findFieldset($el, name);
$.jsonCall(apos.areas.options.action + '/edit-virtual-area',
{ dataType: 'html' },
{ items: items, options: options }, function(data) {
var $editView = self.findSafe($fieldset, '[data-' + name + '-edit-view]');
$editView.append(data);
// Make sure slideshows, videos, etc. get their JS
apos.emit('enhance', $editView);
return callback(null);
});
var virtualArea = {
action: apos.areas.options.action + '/edit-virtual-area',
items: items,
options: options,
name: name
};
// Pass it on as a data attribute. Will be consumed inside the modal.
$fieldset.attr('data-virtual-area', JSON.stringify(virtualArea));
return callback(null);
};

self.getSingleton = function($el, name) {
Expand Down
9 changes: 9 additions & 0 deletions lib/modules/apostrophe-ui/public/css/components/fields.less
Expand Up @@ -308,3 +308,12 @@
.apos-browse {
margin-bottom: 16px;
}

// Disabled Field
// Wanted to use .apos-disabled but it's been used elsewhere
.apos-container-disabled {
.apos-button-label {
color: #ccc !important;
}
pointer-events: none !important;
}