HTML dynamic content is not shown in spark textarea - flex4

In a flex application, i want to display formatted tweets in a spark textarea but it is showing html elements (with tags) 'as it is' rather parsing it and converting into rich html text, i was following this example from flex official site
<s:TextArea width="400" height="100">
<s:content>This is <s:span color="#FF0000">HTML text</s:span>
in an <s:span fontWeight="bold">Spark TextArea control</s:span>.
Use the <s:span textDecoration="underline">content</s:span> property
of the <s:span color="#008800">Spark TextArea control</s:span>
to include basic HTML markup in your text, including
<s:a href="http://www.adobe.com" target="_blank">links</s:a>.
</s:content>
</s:TextArea>
but passing my data like this
<s:TextArea>
<s:content>{TwitterString.parseTweet(data.text)}</s:content>
</s:TextArea>
and the result to it for a tweet is,
<s:a href='http://t.co/a7bQnmLRGy' target='_blank'>http://t.co/a7bQnmLRGy</s:a> I'll be there
which means it has not been formatted as expected.
Any idea how to make <s:content> work for dynamic content pass to it?
Please no answers of TextConverter this would be my last resort, i would love to use <s:content> working

Check out the documentation for TextArea's content property. In particular, it states this about the content property:
This property is typed as Object because you can set it to to a String, a FlowElement, or an Array of Strings and FlowElements. In the example above, the content is a 2-element array. The first array element is the String "Hello". The second array element is a SpanElement object with the text "World" in boldface.
So the key here is that your TwitterString.parseTweet() function is returning a String. As such, the TextArea is displaying it as a String and not bothering to convert the elements found in the text.
Instead, if you wish to continue down this path, your TwitterString.parseTweet() function should return an Array. Each element in the array is either a String (represents normal text) or some sort of FlowElement object (a SpanElement, LinkElement, ParagraphElement, etc.). If you return an array that contains the structure you want, it will render your content properly. See below for a simple example, it will get very tedious for real content.
So you're really better off using TextConverter ... because by writing the code like you want, you are doing the exact same thing that TextConverter does.
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" xmlns:local="*">
<fx:Script>
<![CDATA[
import flashx.textLayout.elements.LinkElement;
import flashx.textLayout.elements.SpanElement;
[Bindable("contentChanged")]
private function getTheContent():Array
{
var temp:Array = [];
var link:LinkElement = new LinkElement();
link.href = "http://google.com";
var span:SpanElement = new SpanElement();
span.text = "Google";
link.addChild(span);
temp.push(link);
// continue building your structure...(so tedious)
return temp;
}
]]>
</fx:Script>
<s:TextArea width="400" height="100">
<s:content>{getTheContent()}</s:content>
</s:TextArea>
</s:Application>

Related

How can I create a DjRF API endpoint that returns a RML-generated PDF?

Django documentation has the following example for ReportLab.
from reportlab.pdfgen import canvas
from django.http import HttpResponse
def some_view(request):
# Create the HttpResponse object with the appropriate PDF headers.
response = HttpResponse(content_type='application/pdf')
response['Content-Disposition'] = 'attachment; filename="somefilename.pdf"'
# Create the PDF object, using the response object as its "file."
p = canvas.Canvas(response)
# Draw things on the PDF. Here's where the PDF generation happens.
# See the ReportLab documentation for the full list of functionality.
p.drawString(100, 100, "Hello world.")
# Close the PDF object cleanly, and we're done.
p.showPage()
p.save()
return response
However. I want to use RML to generate my PDF. ReportLab Plus offers an rml2pdf function that can turn an RML document into a PDF using a similar markup to Django's templating. How can I provide Django the RML template and have Django return the PDF in the API response, similar to the example in the documentation?
Figured it out.
RML Template
Put this in your Django templates folder so Django can find it.
Call it templates/rml_template.rml
<!DOCTYPE document SYSTEM "rml.dtd">
<document filename="example_01.pdf">
<template>
<!--this section contains elements of the document -->
<!--which are FIXED into position. -->
<pageTemplate id="main">
<frame id="first" x1="100" y1="400" width="150" height="200"/>
</pageTemplate>
</template>
<stylesheet>
<!--this section contains the STYLE information for -->
<!--the document, but there isn't any yet. The tags still -->
<!--have to be present, however, or the document won't compile.-->
</stylesheet>
<story>
<!--this section contains the FLOWABLE elements of the -->
<!--document. These elements will fill up the frames -->
<!--defined in the <template> section above. -->
<para>
Welcome to RML!
</para>
<para>
This is the "story". This is the part of the RML document where
your text is placed.
</para>
<para>
It should be enclosed in "para" and "/para" tags to turn it into
paragraphs.
</para>
</story>
</document>
Django View
This view loads the template as a string and rml2pdf uses the response object as its "file" to write to, similar to the Django documentation example.
from django.template import loader
from rlextra.rml2pdf import rml2pdf
from django.http import HttpResponse
def some_view(request):
# Create the HttpResponse object with the appropriate PDF headers.
response = HttpResponse(content_type='application/pdf')
response['Content-Disposition'] = 'attachment; filename="somefilename.pdf"'
context = {} # You can add values to be inserted into the template here
rml = str(loader.render_to_string('rml_template.rml', context))
rml2pdf.go(rml, outputFileName=response)
return response

Convert HTML string to HTML and display in DIV

I have HTML string saved in data base , I want to display it into DIV in HTML format using javascript.
Example:
<p>Dear Friends</p> <h1> You have got invitation </h1>
I have used DOMParser like this
parser = new DOMParser();
htmlDoc = parser.parseFromString(document.getElementById(controlID).innerHTML, "text/html");
console.log(htmlDoc);
document.getElementById("emailBodyArea").innerHTML = parser
but in result I see [htmlObject]
You dont need to use any DOMParser just replace emailBodyArea innerHTML with controlId innerHTML or any string based HTML.
document.getElementById("emailBodyArea").innerHTML = document.getElementById(controlID).innerHTML;
You can try it on jsfiddle.

Html tags in xml (rss)

Followed http://damieng.com/blog/2010/04/26/creating-rss-feeds-in-asp-net-mvc to create RSS for my blog. Everything fine except html tags in xml document. Typical problem:
<br />
insted of
<br />
Normally I would use
#HtmlRaw()
or
MvcHtmlString()
But how can I fix it in XML document created with SyndicationFeed?
Edit:
Ok, I'm starting to think that my question is pointless.
Should I just leave my RSS as it is?
With the XML element, you can wrap the text with your HTML in it in as a CDATA section:
<![CDATA[
your html
]]>
I don't recommend doing that, however.
wrap the text in side the CDATA
var xml= '<person><name><![CDATA[<h1>john smith</h1>]]></name></person>',
xmlDoc = $.parseXML( xml ),
$xml = $( xmlDoc ),
$title = $xml.find( "name" );
$($title.text()).appendTo("body");
DEMO

How update an image dynamically using data binding?

I have a main.mxml application that lays out my application, it contains a "browse and upload" button. And contains an image to view the users uploaded image like so:
<mx:Application
<mx:Script>
<![CDATA[
import model.myModel;
import control.myControl;
// Create data model
public var model:myModel = new myModel();
//[Bindable]
//private var scld_img:Bitmap;
// Create control
public var mycontrol:myControl = new myControl(mymodel);
</mx:Script>
<!-- Upload and view -->
<mx:Canvas id="upload" label="1: Upload Image">
<mx:VBox>
<mx:HBox>
<mx:Label text="Upload an image: "/>
<mx:Button id="btn"
label="Browse and preview..."
click="mycontrol.browseAndUpload();"
buttonMode="true"
useHandCursor="true"/>
</mx:HBox>
<mx:Image id="mximg_upld"
verticalCenter="0"
horizontalCenter="0"
source="mymodel.img_scld_bm"/>
</mx:VBox>
</mx:Canvas>
......
In my myModel class I have a img_scld_bm that the browseAndUpload() function draws into after scaling it.
My intent is that my mx:Image will display the image. As shown here I'm assigning the image source="mymodel.img_scld_bm", this ends up just showing a broken image icon.
I also tried data binding, where in my myModel class I have [Bindable] var img_scld_bm. And then tried setting my mx:Image source="{myModel.img_scld_bm}" .. that didn't seem to do anything either. All this compiles fine, no warnings. I think in this case, I'm not setting a trigger or propertyChange event to update the binding?
Can someone help me understand, or provide an example of how to correctly bind an mx:Image source to some bitmap??
From the code you've given, it looks like you need to do two things:
Wrap the source with curly brackets.
Set the property img_scld_bm in the model class. Bindings will only execute if you set the property; it looks like you may be drawing into the img_scld_bm that currently exists, but this won't cause the image to update.
Convert this:
<mx:Image id="mximg_upld" verticalCenter="0" horizontalCenter="0" source="mymodel.img_scld_bm"/>
To this:
<mx:Image id="mximg_upld" verticalCenter="0" horizontalCenter="0" source="{mymodel.img_scld_bm}"/>
Then any time you make changes to the img_scld_bm in the model, reset the image.source.
Does that work?

Firefox Extension, get a reference to the loaded document using browser.contentDocument

I want to write a simple firefox extension.
How can I get a reference to the loaded document object in a browser window? For example, how can I access the document in this html page loaded here on stackoverflow? According to the vague mozilla development center I can use browser.contentDocument, but it is not working for me.
<?xml version="1.0"?>
<overlay id="sample"
xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
<script type="application/x-javascript">
function change(){
//var doc = document.getElementsByTagName("browser")[0].contentDocument;
//var doc = document.browser.contentDocument;
var doc = browser.contentDocument;
var body = doc.getElementsByTagName("body")[0];
var text = doc.createTextNode("blah");
body.appendChild(text);
}
</script>
<statusbar id="status-bar">
<statusbarpanel id="my-panel" label="click me" onclick="change();" />
</statusbar>
</overlay>
I'm not sure where you read that the variable is browser.contentDocument, since it's listed in several spots to be content.document (but I'm linking to the FAQ).
Variable for accessing content of current displayed document: window.content.document

Resources