Skip to content

Commit

Permalink
added React component modules.
Browse files Browse the repository at this point in the history
  • Loading branch information
treefrogframework committed Mar 29, 2016
1 parent 0fefdcd commit 3ac57f2
Show file tree
Hide file tree
Showing 21 changed files with 16,351 additions and 4 deletions.
15,919 changes: 15,919 additions & 0 deletions defaults/JSXTransformer.js

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion defaults/_src.pro
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
TARGET = view
TEMPLATE = lib
CONFIG += shared c++11
QT += network xml
QT += network xml qml
QT -= gui
greaterThan(QT_MAJOR_VERSION, 4): QT += qml
DEFINES += TF_DLL
INCLUDEPATH += ../../helpers ../../models
DEPENDPATH += ../../helpers ../../models
Expand Down
1 change: 1 addition & 0 deletions defaults/controllers.pro
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ TEMPLATE = lib
CONFIG += shared c++11
QT += network sql xml
QT -= gui
greaterThan(QT_MAJOR_VERSION, 4): QT += qml
DEFINES += TF_DLL
DESTDIR = ../lib
INCLUDEPATH += ../helpers ../models
Expand Down
3 changes: 2 additions & 1 deletion defaults/helpers.pro
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
TARGET = helper
TEMPLATE = lib
CONFIG += shared c++11
QT += xml
QT -= gui
QT +=
greaterThan(QT_MAJOR_VERSION, 4): QT += qml
DEFINES += TF_DLL
DESTDIR = ../lib
DEPENDPATH +=
Expand Down
12 changes: 12 additions & 0 deletions defaults/react-dom-server.min.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* ReactDOMServer v0.14.7
*
* Copyright 2013-2015, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/
!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e(require("react"));else if("function"==typeof define&&define.amd)define(["react"],e);else{var f;f="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this,f.ReactDOMServer=e(f.React)}}(function(e){return e.__SECRET_DOM_SERVER_DO_NOT_USE_OR_YOU_WILL_BE_FIRED});
16 changes: 16 additions & 0 deletions defaults/react.min.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions include/TJSContext
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "tjscontext.h"
1 change: 1 addition & 0 deletions include/TReactComponent
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "treactcomponent.h"
8 changes: 8 additions & 0 deletions include/headers.pri
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@ HEADER_CLASSES = ../include/TAbstractModel ../include/TAbstractUser ../include/T

HEADER_FILES = tabstractmodel.h tabstractuser.h tactioncontext.h tactioncontroller.h tactionhelper.h tactionthread.h tactionview.h tprototypeajaxhelper.h tapplicationserverbase.h tthreadapplicationserver.h tpreforkapplicationserver.h tcontentheader.h tcookie.h tcookiejar.h tcriteria.h tcriteriaconverter.h tcryptmac.h tdirectview.h tdispatcher.h tfcore.h tfexception.h tfnamespace.h tglobal.h thtmlattribute.h thtmlparser.h thttpheader.h thttprequest.h thttprequestheader.h thttpresponse.h thttpresponseheader.h thttputility.h tinternetmessageheader.h tjavascriptobject.h tlog.h tlogger.h tloggerplugin.h tmailmessage.h tmodelutil.h tmultipartformdata.h toption.h tsession.h tsessionstore.h tsessionstoreplugin.h tsharedmemorylogstream.h tsmtpmailer.h tsqlobject.h tsqlormapper.h tsqlormapperiterator.h tsqlquery.h tsqlqueryormapper.h tsystemglobal.h ttemporaryfile.h tviewhelper.h twebapplication.h tabstractcontroller.h tactionmailer.h tformvalidator.h tsqlqueryormapperiterator.h taccessvalidator.h tsqltransaction.h tpaginator.h tkvsdatabase.h tkvsdriver.h tmodelobject.h tpopmailer.h tmultiplexingserver.h taccesslog.h tactionworker.h tatomicqueue.h tjsonutil.h tscheduler.h tapplicationscheduler.h tcommandlineinterface.h tsendmailmailer.h tappsettings.h twebsocketendpoint.h tdatabasecontext.h tsystembus.h tprocessinfo.h twebsocketsession.h tredis.h tsqljoin.h

# Qt5
greaterThan(QT_MAJOR_VERSION, 4) {
HEADER_CLASSES += ../include/TReactComponent
HEADER_FILES += treactcomponent.h
HEADER_CLASSES += ../include/TJSContext
HEADER_FILES += tjscontext.h
}

unix {
HEADER_FILES += tfcore_unix.h
}
Expand Down
1 change: 1 addition & 0 deletions include/tjscontext.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "../src/tjscontext.h"
1 change: 1 addition & 0 deletions include/treactcomponent.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "../src/treactcomponent.h"
5 changes: 5 additions & 0 deletions src/corelib.pro
Original file line number Diff line number Diff line change
Expand Up @@ -321,8 +321,13 @@ macx {

# Qt5
greaterThan(QT_MAJOR_VERSION, 4) {
QT += qml
HEADERS += tjsonutil.h
SOURCES += tjsonutil.cpp
HEADERS += tjscontext.h
SOURCES += tjscontext.cpp
HEADERS += treactcomponent.h
SOURCES += treactcomponent.cpp

SOURCES += tactioncontroller_qt5.cpp
}
Expand Down
52 changes: 51 additions & 1 deletion src/tactionview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@
*/

#include <TActionView>
#include <TWebApplication>
#include <TActionController>
#include <THttpUtility>
#include <THtmlAttribute>
#include <TReactComponent>
#include <QDir>
#include <QMutex>
#include <QMutexLocker>
#include "tsystemglobal.h"

/*!
\class TActionView
Expand Down Expand Up @@ -39,11 +45,55 @@ QString TActionView::renderPartial(const QString &templateName, const QVariantMa
{
QString temp = templateName;
if (!temp.contains('/')) {
temp = QLatin1String("partial/") + temp;
temp = QLatin1String("partial") + QDir::separator() + temp;
}
return (actionController) ? actionController->getRenderingData(temp, vars) : QString();
}

/*!
Renders the React \a component on the server. Calls ReactDOMServer.renderToString()
internally.
*/
QString TActionView::renderReact(const QString &component)
{
static QMap<QString, TReactComponent*> reactComponents;
static QMutex mutex;

if (component.isEmpty()) {
return QString();
}

QMutexLocker locker(&mutex);
TReactComponent *react = reactComponents.value(component);

if (react) {
QDateTime modified = QFileInfo(react->filePath()).lastModified();
if (!modified.isValid() || modified > react->loadedDateTime()) {
// Removes the item to reload
reactComponents.remove(component);
delete react;
react = nullptr;
}
}
locker.unlock();

if (!react) {
QDir dir(Tf::app()->publicPath() + "js" + QDir::separator() + "components");
QStringList filter = { component + ".*" };
QString file = dir.entryList(filter, QDir::Files).value(0);

if (!file.isEmpty()) {
react = new TReactComponent(dir.absolutePath() + QDir::separator() + file);
locker.relock();
reactComponents.insert(component, react);
locker.unlock();
} else {
return QString();
}
}
return react->renderToString(component);
}

/*!
Returns a authenticity token for CSRF protection.
*/
Expand Down
1 change: 1 addition & 0 deletions src/tactionview.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class T_CORE_EXPORT TActionView : public QObject, public TActionHelper, public T
QString eh(double d, char format = 'g', int precision = 6);
QString eh(const THtmlAttribute &attr);
QString eh(const QVariant &var);
QString renderReact(const QString &component);
QString responsebody;

private:
Expand Down
1 change: 1 addition & 0 deletions src/test/test.pri
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ CONFIG += console c++11
CONFIG -= app_bundle
QT += network sql
QT -= gui
greaterThan(QT_MAJOR_VERSION, 4): QT += qml
DEFINES += TF_DLL

lessThan(QT_MAJOR_VERSION, 5) {
Expand Down
123 changes: 123 additions & 0 deletions src/tjscontext.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/* Copyright (c) 2016, AOYAMA Kazuharu
* All rights reserved.
*
* This software may be used and distributed according to the terms of
* the New BSD License, which is incorporated herein by reference.
*/

#include <QJSEngine>
#include <QJSValue>
#include <QFile>
#include <QTextStream>
#include "tjscontext.h"
#include "tsystemglobal.h"


inline const char *prop(const QJSValue &val, const QString &name = QString())
{
return (name.isEmpty()) ? qPrintable(val.toString()) : qPrintable(val.property(name).toString());
}


TJSContext::TJSContext(const QStringList &scriptFiles)
: jsEngine(new QJSEngine()), funcObj(nullptr), lastFunc(), mutex(QMutex::Recursive)
{
for (auto &file : scriptFiles) {
load(file);
}
}


TJSContext::~TJSContext()
{
if (funcObj) {
delete funcObj;
}
delete jsEngine;
}


QJSValue TJSContext::evaluate(const QString &program, const QString &fileName, int lineNumber)
{
QJSValue ret = jsEngine->evaluate(program, fileName, lineNumber);
if (ret.isError()) {
tSystemError("JS uncaught exception at %s:%s : %s", prop(ret, "fileName"),
prop(ret, "lineNumber"), prop(ret, "message"));
}
return ret;
}


QJSValue TJSContext::call(const QString &func, const QJSValue &arg)
{
QJSValueList args = { arg };
return call(func, args);
}


QJSValue TJSContext::call(const QString &func, const QJSValueList &args)
{
QMutexLocker locker(&mutex);
QJSValue ret;

QString funcsym = QString::number(args.count()) + func;
if (funcsym != lastFunc || !funcObj) {
lastFunc = funcsym;

QString argstr;
for (int i = 0; i < args.count(); i++) {
argstr = QChar('a') + QString::number(i) + ',';
}
argstr.chop(1);

QString defFunc = QString("function(%1){return(%2(%1));}").arg(argstr, func);

if (!funcObj) {
funcObj = new QJSValue();
}

*funcObj = evaluate(defFunc);
if (funcObj->isError()) {
goto eval_error;
}
}

ret = funcObj->call(args);
if (ret.isError()) {
tSystemError("JS uncaught exception at %s:%s : %s", prop(ret, "fileName"),
prop(ret, "lineNumber"), prop(ret));
goto eval_error;
}

return ret;

eval_error:
delete funcObj;
funcObj = nullptr;
return ret;
}


bool TJSContext::load(const QString &fileName)
{
QMutexLocker locker(&mutex);

QFile script(fileName);
if (!script.open(QIODevice::ReadOnly)) {
// open error
tSystemError("TJSContext open error: %s", qPrintable(fileName));
return false;
}

QTextStream stream(&script);
QString contents = stream.readAll();
script.close();

QJSValue res = evaluate(contents, fileName);
if (res.isError()) {
return false;
}

tSystemDebug("TJSContext evaluation completed: %s", qPrintable(fileName));
return true;
}
32 changes: 32 additions & 0 deletions src/tjscontext.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#ifndef TJSCONTEXT_H
#define TJSCONTEXT_H

#include <QString>
#include <QJSValue>
#include <QMutex>
#include <TGlobal>

class QJSEngine;


class T_CORE_EXPORT TJSContext
{
public:
TJSContext(const QStringList &scriptFiles = QStringList());
virtual ~TJSContext();

bool load(const QString &scriptFile);
QJSValue evaluate(const QString &program, const QString &fileName = QString(), int lineNumber = 1);
QJSValue call(const QString &func, const QJSValue &arg);
QJSValue call(const QString &func, const QJSValueList &args = QJSValueList());

private:
QJSEngine *jsEngine;
QJSValue *funcObj;
QString lastFunc;
QMutex mutex;

Q_DISABLE_COPY(TJSContext);
};

#endif // TJSCONTEXT_H
Loading

0 comments on commit 3ac57f2

Please sign in to comment.