Neil D'Souza
2012-08-31 14:35:02 UTC
Dear All,
I am trying to use CodeMirror [http://codemirror.net] with Wt. Specifically I am trying to modify the :
examples/feature/widgetset/hello.C to do this.
While I am successful in seeing the editor on screen and binding to a Wt Widget I get the following error on the screen, after which the hello application continues to run, if I press the button. However, the "Loading" notification does not go away.
Wt internal error: TypeError: j10 is undefined, code: undefined, description: undefined
Pasted below this is the code, the html file and the output.
In the output sections which I feel are relevant,
I have enclosed in "************".
The version of codemirror I am using is 2.13 and unmodified.
I am able to successfully load the javascript files as it does
syntax highlighting for the javascript entered in the text box.
You will notice that in the DOM, I have done this :
==========================
editor.id = "code_mirror_text_editor";
==========================
immediately after creating the editor and in the c++ code I have
bound the editor to a div element. This is because when inspecting
through firefox, the editor seems to be rendered
as a "div" with class = "CodeMirror",
the original textarea is given a css class of "display: none;"
Of course I could be wrong about the "div" and any advice you have
on a better approach is highly appreciated.
Many thanks for your help in advance.
Kind Regards,
Neil
=========================c++ code =================================
/*
* Copyright (C) 2008 Emweb bvba, Kessel-Lo, Belgium.
*
* See the LICENSE file for terms of use.
*/
#include <Wt/WApplication>
#include <Wt/WBreak>
#include <Wt/WContainerWidget>
#include <Wt/WEnvironment>
#include <Wt/WLineEdit>
#include <Wt/WPushButton>
#include <Wt/WServer>
#include <Wt/WText>
class HelloApplication : public Wt::WApplication
{
public:
HelloApplication(const Wt::WEnvironment& env, bool embedded);
private:
Wt::WLineEdit *nameEdit_;
Wt::WText *greeting_;
void greet();
};
HelloApplication::HelloApplication(const Wt::WEnvironment& env, bool embedded)
: WApplication(env)
{
using namespace std;
Wt::WContainerWidget *top;
Wt::WContainerWidget * code_mirror_text_editor;
setTitle("Hello world");
if (!embedded) {
/*
* In Application mode, we have the root() is a container
* corresponding to the entire browser window
*/
top = root();
cout << " I am not embedded" << endl;
} else {
/*
* In WidgetSet mode, we create and bind containers to existing
* divs in the web page. In this example, we create a single div
* whose DOM id was passed as a request argument.
*/
cout << " I am embedded" << endl;
top = new Wt::WContainerWidget();
const std::string *div = env.getParameter("div");
if (div) {
setJavaScriptClass(*div);
bindWidget(top, *div);
code_mirror_text_editor = new Wt::WContainerWidget(top);
code_mirror_text_editor->setId("code_mirror_text_editor");
bindWidget(code_mirror_text_editor, "code_mirror_text_editor");
cout << "code_mirror_text_editor .id:"
<< code_mirror_text_editor->id() << endl;
} else {
std::cerr << "Missing: parameter: 'div'" << std::endl;
return;
}
}
if (!embedded)
new Wt::WText
("<p><emph>Note: you can also run this application "
"from within <a href=\"qscript-console.html\">a web page</a>.</emph></p>",
root());
/*
* Everything else is business as usual.
*/
top->addWidget(new Wt::WText("Your name, please ? "));
nameEdit_ = new Wt::WLineEdit(top);
nameEdit_->setFocus();
Wt::WPushButton *b = new Wt::WPushButton("Greet me.", top);
b->setMargin(5, Wt::Left);
top->addWidget(new Wt::WBreak());
greeting_ = new Wt::WText(top);
/*
* Connect signals with slots
*/
b->clicked().connect(this, &HelloApplication::greet);
nameEdit_->enterPressed().connect(this, &HelloApplication::greet);
}
void HelloApplication::greet()
{
/*
* Update the text, using text input into the nameEdit_ field.
*/
greeting_->setText("Hello there, " + nameEdit_->text());
}
Wt::WApplication *createApplication(const Wt::WEnvironment& env)
{
return new HelloApplication(env, false);
}
Wt::WApplication *createWidgetSet(const Wt::WEnvironment& env)
{
return new HelloApplication(env, true);
}
int main(int argc, char **argv)
{
Wt::WServer server(argv[0]);
server.setServerConfiguration(argc, argv, WTHTTP_CONFIGURATION);
server.addEntryPoint(Wt::Application, createApplication);
// The following adds an entry point for a widget set. A widget set
// must be loaded as a JavaScript from an HTML page.
server.addEntryPoint(Wt::WidgetSet, createWidgetSet, "/qscript-console.js");
if (server.start()) {
Wt::WServer::waitForShutdown();
// Cleanly terminate all sessions
server.stop();
}
}
============== HTML file qscript-console.html ===========
<html>
<head>
<title>Embedded Wt/CodeMirror 2: JavaScript mode</title>
<style type="text/css">
.widget {
width: 400px;
height: 200px;
margin: 10px auto;
padding: 4px;
border: 4px solid gray;
}
.IE-hidden-iframe {
position: absolute;
top: 0; left: 0;
width: 1px; height: 1px;
visibility: hidden;
}
</style>
<link rel="stylesheet" href="code-mirror/codemirror.css">
<script src="code-mirror/codemirror.js"></script>
<script src="code-mirror/mode/javascript/javascript.js"></script>
<link rel="stylesheet" href="code-mirror/theme/default.css">
<link rel="stylesheet" href="code-mirror/css/docs.css">
<style type="text/css">.CodeMirror {border-top: 1px solid black; border-bottom: 1px solid black;}</style>
</head>
<body>
<input id="Wt-history-field" type="hidden"/>
<div><textarea id="code" name="code">
</textarea>
</div>
<script>
var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
lineNumbers: true,
matchBrackets: true
});
editor.id = "code_mirror_text_editor";
</script>
<!-- End hidden fields for Wt's internal path API -->
<!--
We need a place holder for every widget to which we want to bind
a WContainerWidget in Wt
-->
<div id="hello" class="widget">
</div>
<!--
As the last step, we load our application.
-->
<script src="http://127.0.0.1:8080/qscript-console.js?div=hello&Wt-history=Wt-history"></script>
</body>
</html>
=================== output ===============
/home/nxd/Progs/xtcc/xtcc/qscript/stubs/simple_compiler/stubs/widgetset>./qscript-console.exe --http-address=127.0.0.1 --http-port=8080 --docroot=.
[2012-Aug-31 19:50:23.271032] 1869 - [info] "config: reading Wt config file: /etc/wt/wt_config.xml (location = './qscript-console.exe')"
[2012-Aug-31 19:50:23.272056] 1869 - [info] "WServer/wthttp: initializing built-in wthttpd"
[2012-Aug-31 19:50:23.272376] 1869 - [info] "wthttp: started server: http://127.0.0.1:8080"
[2012-Aug-31 19:50:32.695055] 1869 - [info] "Wt: session created (#sessions = 1)"
[2012-Aug-31 19:50:32.695332] 1869 [/ 6c24O0WArxvBFgbd] [info] "WEnvironment: UserAgent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:13.0) Gecko/20100101 Firefox/13.0.1"
127.0.0.1 - - [2012-Aug-31 19:50:32.696206] "GET / HTTP/1.1" 200 1845
[2012-Aug-31 19:50:32.696276] 1869 - [info] "WebRequest: took 1.354ms"
I am not embedded
127.0.0.1 - - [2012-Aug-31 19:50:33.203252] "GET /?wtd=6c24O0WArxvBFgbd&request=style HTTP/1.1" 200 89
[2012-Aug-31 19:50:33.203343] 1869 - [info] "WebRequest: took 29.771ms"
127.0.0.1 - - [2012-Aug-31 19:50:33.211998] "GET /?wtd=6c24O0WArxvBFgbd&sid=1858417332&htmlHistory=true&deployPath=%2F&request=script&rand=1953192535 HTTP/1.1" 200 48958
[2012-Aug-31 19:50:33.212138] 1869 - [info] "WebRequest: took 10.138ms"
127.0.0.1 - - [2012-Aug-31 19:50:33.493852] "POST /?wtd=6c24O0WArxvBFgbd HTTP/1.1" 200 50
[2012-Aug-31 19:50:33.493913] 1869 - [info] "WebRequest: took 0.494ms"
[2012-Aug-31 19:50:39.975074] 1869 - [info] "Wt: session created (#sessions = 2)"
******************************************************
[2012-Aug-31 19:50:39.975214] 1869 [/qscript-console.js f8GP6gXSMX66R9Q6] [info] "WEnvironment: UserAgent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:13.0) Gecko/20100101 Firefox/13.0.1"
I am embedded
[2012-Aug-31 19:50:39.975602] 1869 [/qscript-console.js f8GP6gXSMX66R9Q6] [warning] "WContainerWidget: addWidget(): reparenting widget"
******************************************************
code_mirror_text_editor .id:code_mirror_text_editor
******************************************************
127.0.0.1 - - [2012-Aug-31 19:50:39.983278] "GET /qscript-console.js?div=hello&Wt-history=Wt-history HTTP/1.1" 200 47839
[2012-Aug-31 19:50:39.983405] 1869 - [info] "WebRequest: took 8.422ms"
127.0.0.1 - - [2012-Aug-31 19:50:40.238160] "POST /qscript-console.js?wtd=f8GP6gXSMX66R9Q6&wtd=f8GP6gXSMX66R9Q6 HTTP/1.1" 200 1550
[2012-Aug-31 19:50:40.238267] 1869 - [info] "WebRequest: took 1.653ms"
I am trying to use CodeMirror [http://codemirror.net] with Wt. Specifically I am trying to modify the :
examples/feature/widgetset/hello.C to do this.
While I am successful in seeing the editor on screen and binding to a Wt Widget I get the following error on the screen, after which the hello application continues to run, if I press the button. However, the "Loading" notification does not go away.
Wt internal error: TypeError: j10 is undefined, code: undefined, description: undefined
Pasted below this is the code, the html file and the output.
In the output sections which I feel are relevant,
I have enclosed in "************".
The version of codemirror I am using is 2.13 and unmodified.
I am able to successfully load the javascript files as it does
syntax highlighting for the javascript entered in the text box.
You will notice that in the DOM, I have done this :
==========================
editor.id = "code_mirror_text_editor";
==========================
immediately after creating the editor and in the c++ code I have
bound the editor to a div element. This is because when inspecting
through firefox, the editor seems to be rendered
as a "div" with class = "CodeMirror",
the original textarea is given a css class of "display: none;"
Of course I could be wrong about the "div" and any advice you have
on a better approach is highly appreciated.
Many thanks for your help in advance.
Kind Regards,
Neil
=========================c++ code =================================
/*
* Copyright (C) 2008 Emweb bvba, Kessel-Lo, Belgium.
*
* See the LICENSE file for terms of use.
*/
#include <Wt/WApplication>
#include <Wt/WBreak>
#include <Wt/WContainerWidget>
#include <Wt/WEnvironment>
#include <Wt/WLineEdit>
#include <Wt/WPushButton>
#include <Wt/WServer>
#include <Wt/WText>
class HelloApplication : public Wt::WApplication
{
public:
HelloApplication(const Wt::WEnvironment& env, bool embedded);
private:
Wt::WLineEdit *nameEdit_;
Wt::WText *greeting_;
void greet();
};
HelloApplication::HelloApplication(const Wt::WEnvironment& env, bool embedded)
: WApplication(env)
{
using namespace std;
Wt::WContainerWidget *top;
Wt::WContainerWidget * code_mirror_text_editor;
setTitle("Hello world");
if (!embedded) {
/*
* In Application mode, we have the root() is a container
* corresponding to the entire browser window
*/
top = root();
cout << " I am not embedded" << endl;
} else {
/*
* In WidgetSet mode, we create and bind containers to existing
* divs in the web page. In this example, we create a single div
* whose DOM id was passed as a request argument.
*/
cout << " I am embedded" << endl;
top = new Wt::WContainerWidget();
const std::string *div = env.getParameter("div");
if (div) {
setJavaScriptClass(*div);
bindWidget(top, *div);
code_mirror_text_editor = new Wt::WContainerWidget(top);
code_mirror_text_editor->setId("code_mirror_text_editor");
bindWidget(code_mirror_text_editor, "code_mirror_text_editor");
cout << "code_mirror_text_editor .id:"
<< code_mirror_text_editor->id() << endl;
} else {
std::cerr << "Missing: parameter: 'div'" << std::endl;
return;
}
}
if (!embedded)
new Wt::WText
("<p><emph>Note: you can also run this application "
"from within <a href=\"qscript-console.html\">a web page</a>.</emph></p>",
root());
/*
* Everything else is business as usual.
*/
top->addWidget(new Wt::WText("Your name, please ? "));
nameEdit_ = new Wt::WLineEdit(top);
nameEdit_->setFocus();
Wt::WPushButton *b = new Wt::WPushButton("Greet me.", top);
b->setMargin(5, Wt::Left);
top->addWidget(new Wt::WBreak());
greeting_ = new Wt::WText(top);
/*
* Connect signals with slots
*/
b->clicked().connect(this, &HelloApplication::greet);
nameEdit_->enterPressed().connect(this, &HelloApplication::greet);
}
void HelloApplication::greet()
{
/*
* Update the text, using text input into the nameEdit_ field.
*/
greeting_->setText("Hello there, " + nameEdit_->text());
}
Wt::WApplication *createApplication(const Wt::WEnvironment& env)
{
return new HelloApplication(env, false);
}
Wt::WApplication *createWidgetSet(const Wt::WEnvironment& env)
{
return new HelloApplication(env, true);
}
int main(int argc, char **argv)
{
Wt::WServer server(argv[0]);
server.setServerConfiguration(argc, argv, WTHTTP_CONFIGURATION);
server.addEntryPoint(Wt::Application, createApplication);
// The following adds an entry point for a widget set. A widget set
// must be loaded as a JavaScript from an HTML page.
server.addEntryPoint(Wt::WidgetSet, createWidgetSet, "/qscript-console.js");
if (server.start()) {
Wt::WServer::waitForShutdown();
// Cleanly terminate all sessions
server.stop();
}
}
============== HTML file qscript-console.html ===========
<html>
<head>
<title>Embedded Wt/CodeMirror 2: JavaScript mode</title>
<style type="text/css">
.widget {
width: 400px;
height: 200px;
margin: 10px auto;
padding: 4px;
border: 4px solid gray;
}
.IE-hidden-iframe {
position: absolute;
top: 0; left: 0;
width: 1px; height: 1px;
visibility: hidden;
}
</style>
<link rel="stylesheet" href="code-mirror/codemirror.css">
<script src="code-mirror/codemirror.js"></script>
<script src="code-mirror/mode/javascript/javascript.js"></script>
<link rel="stylesheet" href="code-mirror/theme/default.css">
<link rel="stylesheet" href="code-mirror/css/docs.css">
<style type="text/css">.CodeMirror {border-top: 1px solid black; border-bottom: 1px solid black;}</style>
</head>
<body>
<input id="Wt-history-field" type="hidden"/>
<div><textarea id="code" name="code">
</textarea>
</div>
<script>
var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
lineNumbers: true,
matchBrackets: true
});
editor.id = "code_mirror_text_editor";
</script>
<!-- End hidden fields for Wt's internal path API -->
<!--
We need a place holder for every widget to which we want to bind
a WContainerWidget in Wt
-->
<div id="hello" class="widget">
</div>
<!--
As the last step, we load our application.
-->
<script src="http://127.0.0.1:8080/qscript-console.js?div=hello&Wt-history=Wt-history"></script>
</body>
</html>
=================== output ===============
/home/nxd/Progs/xtcc/xtcc/qscript/stubs/simple_compiler/stubs/widgetset>./qscript-console.exe --http-address=127.0.0.1 --http-port=8080 --docroot=.
[2012-Aug-31 19:50:23.271032] 1869 - [info] "config: reading Wt config file: /etc/wt/wt_config.xml (location = './qscript-console.exe')"
[2012-Aug-31 19:50:23.272056] 1869 - [info] "WServer/wthttp: initializing built-in wthttpd"
[2012-Aug-31 19:50:23.272376] 1869 - [info] "wthttp: started server: http://127.0.0.1:8080"
[2012-Aug-31 19:50:32.695055] 1869 - [info] "Wt: session created (#sessions = 1)"
[2012-Aug-31 19:50:32.695332] 1869 [/ 6c24O0WArxvBFgbd] [info] "WEnvironment: UserAgent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:13.0) Gecko/20100101 Firefox/13.0.1"
127.0.0.1 - - [2012-Aug-31 19:50:32.696206] "GET / HTTP/1.1" 200 1845
[2012-Aug-31 19:50:32.696276] 1869 - [info] "WebRequest: took 1.354ms"
I am not embedded
127.0.0.1 - - [2012-Aug-31 19:50:33.203252] "GET /?wtd=6c24O0WArxvBFgbd&request=style HTTP/1.1" 200 89
[2012-Aug-31 19:50:33.203343] 1869 - [info] "WebRequest: took 29.771ms"
127.0.0.1 - - [2012-Aug-31 19:50:33.211998] "GET /?wtd=6c24O0WArxvBFgbd&sid=1858417332&htmlHistory=true&deployPath=%2F&request=script&rand=1953192535 HTTP/1.1" 200 48958
[2012-Aug-31 19:50:33.212138] 1869 - [info] "WebRequest: took 10.138ms"
127.0.0.1 - - [2012-Aug-31 19:50:33.493852] "POST /?wtd=6c24O0WArxvBFgbd HTTP/1.1" 200 50
[2012-Aug-31 19:50:33.493913] 1869 - [info] "WebRequest: took 0.494ms"
[2012-Aug-31 19:50:39.975074] 1869 - [info] "Wt: session created (#sessions = 2)"
******************************************************
[2012-Aug-31 19:50:39.975214] 1869 [/qscript-console.js f8GP6gXSMX66R9Q6] [info] "WEnvironment: UserAgent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:13.0) Gecko/20100101 Firefox/13.0.1"
I am embedded
[2012-Aug-31 19:50:39.975602] 1869 [/qscript-console.js f8GP6gXSMX66R9Q6] [warning] "WContainerWidget: addWidget(): reparenting widget"
******************************************************
code_mirror_text_editor .id:code_mirror_text_editor
******************************************************
127.0.0.1 - - [2012-Aug-31 19:50:39.983278] "GET /qscript-console.js?div=hello&Wt-history=Wt-history HTTP/1.1" 200 47839
[2012-Aug-31 19:50:39.983405] 1869 - [info] "WebRequest: took 8.422ms"
127.0.0.1 - - [2012-Aug-31 19:50:40.238160] "POST /qscript-console.js?wtd=f8GP6gXSMX66R9Q6&wtd=f8GP6gXSMX66R9Q6 HTTP/1.1" 200 1550
[2012-Aug-31 19:50:40.238267] 1869 - [info] "WebRequest: took 1.653ms"