Message Boards

A few fragment questions

thumbnail
Christoph Rabel, modified 3 Years ago.

A few fragment questions

Liferay Legend Posts: 1554 Join Date: 9/24/09 Recent Posts
Let's say, I create a simple fragment. (pseudocode, I didn't test it, just to show the idea)
<table id="123"   data-lfr-editable-id="sometable" data-lfr-editable-type="rich-text">something</table>
&lt;#if ISEDITMODE&gt;
&nbsp;&nbsp; <button onclick="addRow()">AddRow</button>
<!--#if-->
function(fragmentElement, configuration) {
&nbsp;&nbsp;&nbsp; addRow() { 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fragmentElement.append(" ...") 
&nbsp;&nbsp; };
};

----
The idea is to create a fragment and allow the editor to change it by clicking a button. In this case, add a row to a table. The mechanism, once it works, would be really useful. Please note that I actually do not look for a solution for tables. I am interested in a general mechanism.
Questions:
1) How do I determine if I am in EDIT mode? (In Freemarker and/or in Javascript)
2) How do I trigger a save event?
I mean, when I do that, the change is simply lost if I don't click inside of the element and "trigger it manually". Or let me rephrase: How can I implement addRow so that my change is persisted?
3) Is there a builtin way to do something like that?
thumbnail
Olaf Kock, modified 3 Years ago.

RE: A few fragment questions

Liferay Legend Posts: 6403 Join Date: 9/23/08 Recent Posts
1) is probably simple: Detect <body class="has-edit-mode-menu ...">
2 and 3 seem to be harder, looking at the requests that go back and forth when editing a fragment: It seems that you'd have to implement dynamic creation of new fragments through JS (utilizing the API used on SiteBuilder/PageFragments). But then, it's likely that these dynamically created fragments (with 2, 3, 4 or 15 dynamically created elements) would also appear in the list of fragments as soon as they're in.
It might be easier to go with a variation that utilizes the configuration of a plugin - less WYSIWYG, but might have an option to go into a direction that you're thinking of.
Disclaimer: I'm saying this on the basis of the 7.2 features, I haven't yet looked at new features in 7.3 fragments and content pages.
thumbnail
Christoph Rabel, modified 3 Years ago.

RE: A few fragment questions

Liferay Legend Posts: 1554 Join Date: 9/24/09 Recent Posts
1) I believe so too and I probably could have found out myself, but I decided to be lazy and ask it here ;)
2/3) Just to be clear, I do not want to create new fragments, I want to edit the content of editable elements inside of a fragment using Javascript.
Liferay doesn't care, what "Some content" is, so I should be able to replace it, as long as I do not mess with the editable tag itself.
<lfr-editable>Some content</lfr-editable>
I checked the request myself and I believe, I could construct the post request to the backend myself or maybe I could simulate the right kind of "click" to trigger it, but these things would be hackish and quite fragile . I'd rather just call an API and tell it "hey, I have changed, persist me".
thumbnail
Olaf Kock, modified 3 Years ago.

RE: A few fragment questions

Liferay Legend Posts: 6403 Join Date: 9/23/08 Recent Posts
I wasn't sure if fragments can be updated on the fly, without appearing in the list of fragments, but I guess that's possible. But unless directly supported it'll be really messy with a potential upgrade nightmare...
Consider classic structured WebContent articles, where you can have repeating elements... Not as WYSIWYG, but a lot cleaner and easier to migrate later
thumbnail
Christoph Rabel, modified 3 Years ago.

RE: A few fragment questions

Liferay Legend Posts: 1554 Join Date: 9/24/09 Recent Posts
Again: I think, you misunderstand me. I don't want to update the fragments.
Let me try it with an analogy (It doesn't fit perfectly, but which analogy does?)
Fragments are similar to Java classes. When I place them on a page, an instance is created. Now, Fragments can contain editable fields, they would be the variables. I do not want to mess with the Java class, I want to update the variables of the instance.
That's actually not difficult using Javascript. The tricky part is tell the system that the variable was changed and the object has to be persisted.
---
This shouldn't be an upgrade issue at all. The "variable" already can contain "anything". So, Liferay shouldn't care what the content is. Sure, I can mess up and insert illformed html, but otherwise I should be fine.
And Webcontent just doesn't cut it for the things I have in mind. I need the drag&drop features and the flexible layouts. What I want to do actually needs at least 7.3.2, can't be done before that  ...
thumbnail
Olaf Kock, modified 3 Years ago.

RE: A few fragment questions

Liferay Legend Posts: 6403 Join Date: 9/23/08 Recent Posts
I think I know what you mean, but used unprecise words in my answer. I agree with the ideas you stated, and thinking of it: You're right - no upgrade issues to expect I believe (now).
Maybe I should first look at the current 7.3.2 implementation before judging if it'll be straightforward or not. Your idea definitely sounds interesting
thumbnail
Olaf Kock, modified 3 Years ago.

RE: A few fragment questions

Liferay Legend Posts: 6403 Join Date: 9/23/08 Recent Posts
There's this paragraph "LPS-113660 Support drop zones in fragments so that page creators can add any fragment or widget inside" (and the following ones) in Jamie's announcement... Does that come close to being useful for your requirement, only that you'd like to have it more structured with a button instead of a dropzone?
Or is that what you were aware of all the time already, which led you to the requirement of requiring 7.3.2 anyway, in order to implement your envisioned solution?
thumbnail
Christoph Rabel, modified 3 Years ago.

RE: A few fragment questions

Liferay Legend Posts: 1554 Join Date: 9/24/09 Recent Posts
Yes, that's the feature I was referring to when I said, 7.3.2 is necessary. It is a really great feature and lays the groundwork for what I want to achieve. I can basically build anything with that. Create a frame (or several) and put the pieces into it. Superflexible.
But there is a limit.
In theory I could create a table fragment with a dropzone,  a row fragment with a dropzone and a fields fragment. The editor drags a table on the page, drags a row into the table, then drags the fields into the rows. The caveat: For a 5x5 table, the editor would have to drag 25 fields into the table, which is kinda tedious.
There are certainly other ways too, but I see a lot of possibilies to enhance the experience of the editor with my idea. In the end, AlloyEditor does something similar, it adds convenience buttons to make a text bold or add a link to it. I probably could implement this as an editor plugin (with emphasis on probably), but I would rather find a more direct, specific to that fragment, approach.