diff --git a/CMakeLists.txt b/CMakeLists.txt --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -304,6 +304,7 @@ core/script/kjs_app.cpp core/script/kjs_console.cpp core/script/kjs_data.cpp + core/script/kjs_display.cpp core/script/kjs_document.cpp core/script/kjs_field.cpp core/script/kjs_fullscreen.cpp diff --git a/core/script/builtin.js b/core/script/builtin.js --- a/core/script/builtin.js +++ b/core/script/builtin.js @@ -59,4 +59,4 @@ } event.value = ret; -} +} \ No newline at end of file diff --git a/core/script/executor_kjs.cpp b/core/script/executor_kjs.cpp --- a/core/script/executor_kjs.cpp +++ b/core/script/executor_kjs.cpp @@ -24,6 +24,7 @@ #include "kjs_app_p.h" #include "kjs_console_p.h" #include "kjs_data_p.h" +#include "kjs_display_p.h" #include "kjs_document_p.h" #include "kjs_event_p.h" #include "kjs_field_p.h" @@ -64,6 +65,7 @@ JSFullscreen::initType( ctx ); JSConsole::initType( ctx ); JSData::initType( ctx ); + JSDisplay::initType( ctx ); JSDocument::initType( ctx ); JSEvent::initType( ctx ); JSField::initType( ctx ); @@ -73,6 +75,7 @@ m_docObject.setProperty( ctx, QStringLiteral("app"), JSApp::object( ctx, m_doc ) ); m_docObject.setProperty( ctx, QStringLiteral("console"), JSConsole::object( ctx ) ); m_docObject.setProperty( ctx, QStringLiteral("Doc"), m_docObject ); + m_docObject.setProperty( ctx, QStringLiteral("display"), JSDisplay::object( ctx )); m_docObject.setProperty( ctx, QStringLiteral("spell"), JSSpell::object( ctx ) ); m_docObject.setProperty( ctx, QStringLiteral("util"), JSUtil::object( ctx ) ); } diff --git a/core/script/kjs_display.cpp b/core/script/kjs_display.cpp new file mode 100644 --- /dev/null +++ b/core/script/kjs_display.cpp @@ -0,0 +1,49 @@ +/*************************************************************************** + * Copyright (C) 2019 by João Netto * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + ***************************************************************************/ + +#include "kjs_display_p.h" + +#include +#include + +#include + +using namespace Okular; + +static KJSPrototype *g_displayProto; + +// display.hidden +static KJSObject displayGetHidden( KJSContext *, void * ) +{ + return KJSBoolean( false ); +} + +// display.visible +static KJSObject displayGetVisible( KJSContext *, void * ) +{ + return KJSBoolean( true ); +} + +void JSDisplay::initType( KJSContext *ctx ) +{ + static bool initialized = false; + if ( initialized ) + return; + initialized = true; + + g_displayProto = new KJSPrototype(); + + g_displayProto->defineProperty( ctx, QStringLiteral("hidden"), displayGetHidden ); + g_displayProto->defineProperty( ctx, QStringLiteral("visible"), displayGetVisible ); +} + +KJSObject JSDisplay::object( KJSContext *ctx ) +{ + return g_displayProto->constructObject( ctx, nullptr ); +} diff --git a/core/script/kjs_display_p.h b/core/script/kjs_display_p.h new file mode 100644 --- /dev/null +++ b/core/script/kjs_display_p.h @@ -0,0 +1,27 @@ +/*************************************************************************** + * Copyright (C) 2019 by João Netto * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + ***************************************************************************/ + +#ifndef OKULAR_SCRIPT_KJS_DISPLAY_P_H +#define OKULAR_SCRIPT_KJS_DISPLAY_P_H + +class KJSContext; +class KJSObject; + +namespace Okular { + +class JSDisplay +{ + public: + static void initType( KJSContext *ctx ); + static KJSObject object( KJSContext *ctx ); +}; + +} + +#endif diff --git a/core/script/kjs_document.cpp b/core/script/kjs_document.cpp --- a/core/script/kjs_document.cpp +++ b/core/script/kjs_document.cpp @@ -129,6 +129,23 @@ return KJSBoolean( !isShell ); } +// Document.numFields +static KJSObject docGetNumFields( KJSContext *, void *object ) +{ + DocumentPrivate *doc = reinterpret_cast< DocumentPrivate* >( object ); + + QVector< Page * >::const_iterator pIt = doc->m_pagesVector.constBegin(), pEnd = doc->m_pagesVector.constEnd(); + unsigned int numFields = 0; + + for ( Page * pIt : doc->m_pagesVector ) + { + const QLinkedList< Okular::FormField * > pageFields = pIt->formFields(); + numFields += pageFields.size(); + } + + return KJSNumber( numFields ); +} + static KJSObject docGetInfo( KJSContext *ctx, void *object ) { @@ -248,6 +265,29 @@ return KJSUndefined(); } +// Document.getNthFieldName +static KJSObject docGetNthFieldName( KJSContext *ctx, void *object, + const KJSArguments &arguments ) +{ + DocumentPrivate *doc = reinterpret_cast< DocumentPrivate* >( object ); + + int numField = arguments.at( 0 ).toInt32( ctx ); + + for ( Page * pIt : doc->m_pagesVector ) + { + const QLinkedList< Okular::FormField * > pageFields = pIt->formFields(); + for( Okular::FormField *ffIt : pageFields ) + { + if(numField == 0) + { + return KJSString( ffIt->name() ); + } + numField--; + } + } + return KJSUndefined(); +} + void JSDocument::initType( KJSContext *ctx ) { assert( g_docProto ); @@ -266,6 +306,7 @@ g_docProto->defineProperty( ctx, QStringLiteral("permStatusReady"), docGetPermStatusReady ); g_docProto->defineProperty( ctx, QStringLiteral("dataObjects"), docGetDataObjects ); g_docProto->defineProperty( ctx, QStringLiteral("external"), docGetExternal ); + g_docProto->defineProperty( ctx, QStringLiteral("numFields"), docGetNumFields ); // info properties g_docProto->defineProperty( ctx, QStringLiteral("info"), docGetInfo ); @@ -281,6 +322,7 @@ g_docProto->defineFunction( ctx, QStringLiteral("getPageRotation"), docGetPageRotation ); g_docProto->defineFunction( ctx, QStringLiteral("gotoNamedDest"), docGotoNamedDest ); g_docProto->defineFunction( ctx, QStringLiteral("syncAnnotScan"), docSyncAnnotScan ); + g_docProto->defineFunction( ctx, QStringLiteral("getNthFieldName"), docGetNthFieldName ); } KJSGlobalObject JSDocument::wrapDocument( DocumentPrivate *doc ) diff --git a/core/script/kjs_field.cpp b/core/script/kjs_field.cpp --- a/core/script/kjs_field.cpp +++ b/core/script/kjs_field.cpp @@ -224,6 +224,24 @@ updateField( field ); } +// Field.display (getter) +static KJSObject fieldGetDisplay( KJSContext *, void *object ) +{ + const FormField *field = reinterpret_cast< FormField * >( object ); + return KJSBoolean( field->isVisible() ); +} + +// Field.display (setter) +static void fieldSetDisplay( KJSContext *context, void *object, KJSObject value ) +{ + FormField *field = reinterpret_cast< FormField * >( object ); + bool b = value.toBoolean( context ); + field->setVisible( b ); + + updateField( field ); +} + + void JSField::initType( KJSContext *ctx ) { static bool initialized = false; @@ -241,6 +259,7 @@ g_fieldProto->defineProperty( ctx, QStringLiteral("type"), fieldGetType ); g_fieldProto->defineProperty( ctx, QStringLiteral("value"), fieldGetValue, fieldSetValue ); g_fieldProto->defineProperty( ctx, QStringLiteral("hidden"), fieldGetHidden, fieldSetHidden ); + g_fieldProto->defineProperty( ctx, QStringLiteral("display"), fieldGetDisplay, fieldSetDisplay ); } KJSObject JSField::wrapField( KJSContext *ctx, FormField *field, Page *page )