Load SVG file with ts functions IONIC 3 - svg

I converted a dwg file to svg file.
Now this svg file I imported it into a html file.
<ion-scroll scrollX="false" scrollY="true" zoom="true" maxZoom="10">
<object id="svg1" data="assets/svgs/file.svg" type="image/svg+xml" style="width:400%;"></object>
</ion-scroll>
In the svg file on rectangles i put this :
onclick="btnClicked();"
<g id="P2" onclick="btnEvent($event)">
And here the problem, the click works but the function btnClicked() dosen't works.
It gives me this error (in the console)
Uncaught ReferenceError: btnClicked is not defined
at SVGGElement.onclick (file.svg:2946)
It seems like I have to check if this object is loaded and the call the function?
Something like this ?
var elementExists = document.getElementById("svg1");
svg1 is the id of object maybe if i check if it's populate with the svg it ll work?
Anyway this is js I ll do it in ts .
Sorry for my english.
UPDATE
When the page is ready I have to go inside the svg and listen all ids that starts with PI and capture the click. svgDwg is the id of the object. Now I'm not able to go inside, where is the svg.
a.contentDocument dosen't works.
ionViewDidLoad() {
var a = document.getElementById("svgDwg");
console.log(a);
}

I find the answer :
If you want to import in your ionic page a file.svg, without use functions like onclick but use something like listener (.on('click',...) in jquery), you can use a combination of jquery and angular-svg-icon
Install first jquery and import in your .ts ionic page :
$ npm install jquery --save
import * as $ from 'jquery'
Then install the plugin angular-svg-icon :
full description here
Import your file.svg in your html :
<svg-icon src="path/to/file.svg"></svg-icon>
Then write some .ts and jquery for your file.svg in ionViewDidEnter() function of ionic
This works for me (after 2 days of research)

Related

Cannot use JSX with nodejs ESM module loader

I attempted to run a simple block of code featuring React "render" method , but the browser didn't display the text.
import React from 'react';
import ReactDOM from 'react-dom';
ReactDOM.render(<h1>Hello World</h1>,document.getElementById('root'));
I'm using VS Code as editor therefore I typed the "Run and Debug Node.js" command. It came up with the warning below
(node:3004) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
Setting "type:module" in the package.json file solved the problem but on the other side another problem arised
(node:18968) ExperimentalWarning: The ESM module loader is experimental.
warning.js:32
SyntaxError: Unexpected token '<'
at Loader.moduleStrategy (internal/modules/esm/translators.js:81:18)
at async link (internal/modules/esm/module_job.js:37:21)
That won't allow me to write any tags whatsover. What am I missing and how can I solve it? Below is the index.html file and the file structure
<html>
<head>
<title> React Test</title>
</head>
<body>
<div id="root"></div>
</body>
</html>
JSX isn't a valid JavaScript syntax and it isn't part of any ECMAScript specification as well at the current time. NodeJS supporting ESM does not mean it supports JSX natively.
JSX are expected to be transpiled into a valid JavaScript on build/compile time using tools like babel. If you are using React, the most simple way to do this is to use babel with #babel/preset-react, which will transpile all JSX into React.createElement() calls. You can then run the code generated by babel using node.
To give you a clearer picture, here is a Babel online REPL that you can play with to see how your code are transpiled by babel.
A common setup for react apps that is going to be run on the browser is like:
Use webpack to bundle your app
Configure webpack to use babel-loader so it transpiles your code into something that browsers can run
Use the generated javascript bundle on browser
I don't understand why you have .vscode folder in your src folder and it contains index.html and index.js files.
I think you need to move your index.js file from .vscode folder to src and delete .vscode folder. Or just create a new app with npm.
that folder structure came out after using create-react-app?
btw, try to return the component with React.createElement()
return React.createElement('div', {className: 'shopping-list'},
React.createElement('h1', /* ... h1 children ... */),
React.createElement('ul', /* ... ul children ... */)
);
as docs says "The render method returns a description of what you want to see on the screen. React takes the description and displays the result. In particular, render returns a React element, which is a lightweight description of what to render. Most React developers use a special syntax called “JSX” which makes these structures easier to write. The syntax is transformed at build time to React.createElement('div')."
check this out
https://reactjs.org/docs/react-api.html#createelement
----- add on
You can now import .js file in node v12.x, in 2 steps:
after adding the following line in your package.json file:
// package.json
{
"type": "module"
}
you need to add --experimental-modules flag when launching the script:
node --experimental-modules index.js
https://nodejs.org/api/esm.html#esm_commonjs_json_and_native_modules

How to install and use Jade

Recently I was watching some video tutorials on youtube and came across this JADE thing, how do I install and use it for my web development?
I googled it around but didn't find a proper site that teaches step-by-step to proceed with. And the site jade-lang.com is not available. I did this from websites but node.js is throwing some errors. screenshot below:
first jade is deprecated and new name for jade is pug for more information about this check this link
pug documentation
second to install pug write this command npm install pug -g after you install pug create a new folder to your project and create a file inside your project file.pug and right click on your folder project + shift
and open command window here and write this command pug file.pug after you write this command it will generate a new file file.html
Third write this command pug input.pug input.html --watch --pretty
--watch to compile your code after saving in file.pug
--pretty to write a code with format of html(organized code) not minimized code
to write a tag in pug write the name of tag like
html tag and we will compile to <html></html>
a(href='#' target='_blank') link compile to link
a(href='#' target=''): img(src='' alt='') compile to <img src="" alt=""/>
to write a comment // this is a comment compile to <!-- this is a comment -->
to write class name and content for tag for example p.demo this is a paragraph compile to <p class="demo"> this is a paragraph</p>
to write id and content for tag for example p#demo this is a paragraph compile to <p id="demo"> this is a paragraph</p>
to write a function
mixin list
ul
li foo
li bar
li baz
to use this function write the name of function +list and will compile to
<ul>
<li>foo</li>
<li>bar</li>
<li>baz</li>
</ul>
to include file write include file_name
Finally this is a example for pug code
html
head
title Hello
link(href='' rel='stylesheet' type='text/css')
body
//this is a comment
p.item hello
p#item
br/
a(href='#' target=''): img(src='' alt='')
// to start a new line
|
|
a(href='google.com') Google
// write a function
mixin list
ul
li foo
li bar
li baz
// use function
+list
// include file
include content
For more information about pug check this link pug full documentation
Jade is now called "pug".
npm install pug -g
Here you will find how to use it if you scroll down:
https://www.npmjs.com/package/pug

Order require scripts / dependencies with laravel-mix and webpack

this the content of my /laravel/webpack.mix.js :
mix
.js([
'resources/assets/js/jquery.js',
'resources/assets/js/plugin.js'
], 'public/js/my_app.js');
The content of /resources/assets/js/jquery.js is :
window.$ = window.jQuery = require('jquery');
The content of /resources/assets/js/plugin.js is the local code written like this :
(function($) {
// plugin script
})(jQuery);
When the plugin.js script is written locally (as above), it is loaded BEFORE jQuery in my_app.js (e.g plugin.js THEN jQuery)
BUT
when I extract plugin.js with "require" or "import" instruction directly from
node_modules**, e.g require('plugin') written in plugin.js, the order is OK :
jquery.js is loaded first THEN plugin.js.
My question:
I want to load jquery.js BEFORE plugin.js.
So, How to do to respect the order EVEN when the plugin.js is a local script?
Laravel mix provides feature where you can extract vendor libraries to vendor.js. But you have to make sure your application code app.js is called after your vendor.js.
<script src="/js/manifest.js"></script>
<script src="/js/vendor.js"></script>
<script src="/js/app.js"></script>
Reference - https://laravel.com/docs/5.4/mix#vendor-extraction
After the .extract call, add
autoload({
jquery: ['$', 'jQuery', 'window.jQuery']
});
So,
mix.js(...).extract(...).autoload(...);
Edit : just saw your response that you tried this

Browserify document is undefined

i just found browserify, which sounds really cool. However i try to append a canvas element to the body(filename : Renderer.js):
window.document.body.appendChild(this.canvas)
module.exports = Renderer
And i also have a main.js
var Renderer = require("Renderer.js")
var r = new Renderer();
So i build the bundle like this:
browserify main.js -o bundle.js
And when i start the project:
node server.js
I get the following error message
ReferenceError : document is not defined (if only document.body)
And window is not defined (when window.document.body)
Can someone explain this behaviour and how to fix it?
Did you try to run that browserify code on the server? Please do not, as in the server there is not window element, whereas node only has global. Instead, you need to use your browser to download the browserified code and execute on your browser.
Note that, browserify library is mainly to bundle packages for client to use require styled call.

Overcome differences in node and browserify path resolving

I'm writing a module for a react app that needs to be included on both the backend and frontend.
At some point in my code, I'm requiring some svg file (for which I use a browserify module, but this has nothing to do with the question).
For example I have in my ./src/js/components/tools/svg.js the following bit of code:
// ...
var BACKEND = /* code to detect if this is running on browser or on node */;
var svg;
if ( BACKEND ) {
svg = require("./../../../icon/" + this.props.icon + ".svg");
} else {
svg = require("./src/icon/" + this.props.icon + ".svg");
}
// ....
I use browserify's require option to require all the svg files at bundle-time:
browserify({
paths: ['./src/icon'],
})
.transform(/* svg tansformer */)
.require(glob.sync("./src/icon/*.svg")) // <-- svg's get added here
.add("./src/main.js"); // main entry point
However this conflicts with how node resolves the filenames. It cannot find ./src/icon/ from ./src/js/components/tools/svg.js.
This is why I have to guard the require with the BACKEN clause. This breaks my eyes though and I would like to just be able to write:
var svg = require('./src/icon/' + this.props.icon + '.svg');
I've tried two things so far:
fix node to find ./src/icon
I can use export NODE_PATH=`cwd` to allow node to look for src/icon from ./. This allows me to write:
var svg = require('src/icon/' + this.props.icon + '.svg');
in the backend. But, since browserify only accepts paths that start with ./ (thus, ignoring src/icon) this will not resolve on the frontend.
fix browserify to use ../../../icon/
Haven't got this to work either because of the same reason: browserify only accepts paths that start with ./.
It's considered bad practice doing conditional requires when using Browserify because it can't evaluate the code at "compile time" and will always attempt to load all the files.
To load different files in the browser environment than on node is easy:
Add a "browser" field to your package.json that points to the browser main file. Use "main" for the node main file. Then just require the module.
You can do the same thing with sub folders within your project. Just add a package.json file with "private": true and both, the main and the browser properties and require the folder path.

Resources