Modify DOM elements outside root component - internationalization

My Angular 2 app is structured something like this:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<base href="/" />
<script src="jspm_packages/system.js"></script>
<script src="config.js"></script>
<script>
System.import('client/main.js');
</script>
</head>
<body>
<my-app></my-app>
</body>
</html>
The root component is the my-app component.
I would like to modify the html element which is outside the root component template to add some attributes to it. Is this possible?
In my case, I'm trying to set dir and lang attributes based on the current user's settings, which would be retrieved at runtime.

You can either use normal JS DOM access or use the DOM adapter as shown in the Title service (source)
https://github.com/angular/angular/blob/master/CHANGELOG.md#400-beta0-2016-12-15 also introduces a Meta service to create and modify <meta> tags.

Related

Laravel: How to include external links in selected views

i have included all the css and js external links under <header> tag of my index view.
Now there are some external links which i want to load on selected views only,
i can add links separately on that view but that link will go out of <head> tag.
how can i load selected links on selected views only from <header> tag of index view.
i tried
<head>
...other links
#if(View::exists('ispblade.calendar'))
#include('ispblade.calendar_links')
#endif
</head>
but it's loading selected link on all views.
You need to use stacks for that.
In Layout:
<head>
<!-- Head Contents -->
#stack('scripts')
</head>
In your view:
#push('scripts')
<script src="/example.js"></script>
#endpush
https://laravel.com/docs/8.x/blade#stacks

Laravel 5 proper way to require CSS file from view

I've got specific Form component, which is declared as
Form::component('fcRadio', 'components.form.fcradio', ['name', 'options', 'selected' => null]);
and used as
{{ Form::fcRadio('name', $options }}
What I want is somehow attach custom CSS file, so if the page fires this component at least once, the desired CSS file is included to the <head> of my document.
For example, in Joomla it was like
$this->document->addStylesheet('my_awesome_style.css');
Is there any way to achieve the same in Laravel?
UPD:
I've extended the answers below a bit to let it add multiple styles from multiple templates. Finally, it looks like this:
#section('styles')
#parent
{{HTML::style('css/fcradio.css')}}
#stop
It works fine, but if I use the component twice per page, style is also adds twice. How can I allow multiple but unique entries?
So this is typically how I deal with it:
In your folder: resources/views I create a folder called layout. This folder handles the templates for all my pages.
Then I create a file called default.blade.php. In it I put the bulk of the HTML code. Here's an example of how default.blade.php could look (slimmed down, obviously)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>
#yield('title')
</title>
<link rel="stylesheet" href="{{ asset('css/main.css') }}">
<!-- Additional per-page css -->
#yield('css')
</head>
<body>
#yield('content')
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="js/script.js"></script>
<script src="{{ asset('js/bootstrap.min.js') }}"></script>
<!-- Include per-page JS -->
#yield('js')
</body>
</html>
Right, so essentially what we have so far is the #yield() and asset() helpers.
#yield() is special blade syntax that Laravel uses to say, "Okay. Any time a blade view that is inheriting THIS master template calls the section named in this #yield() I will display that content right here.
asset() is a nifty little helper that basically appends your URL structure onto the string you pass it. So if your url is http://MyGreatSite.com and you use asset('js/script.js') it will spit out a fully qualified URL that will work anywhere on the site (http://MyGreatSite.com/js/script.js). asset() is great because you can use it in blade templates that will get sent out as an email and all of the files will work in an email inbox because they are absolute links.
Right. So now we have this master template and we need to use it. So what I do is create another view in the resources/views directory. Lets say we're doing a contact page. I would make contact.blade.php. Now I want to inherit that master template we created. So we do that like so:
#extends('layout.default)
#section('css')
<link rel="stylesheet" href="{{ asset('css/contact.css') }}">
#stop
#section('title')
Contact Us
#stop
#section('content')
<h1>Contact us</h1>
<p>
Contact us via email: contact#mygreatsite.com
</p>
#stop
#section('js')
<script src="{{ asset('js/contact-form.js') }}"></script>
#stop
Okay, so, first things first. At the very top we tell this blade file that we want to use the template we just made. We use the blade helper #extends() and pass it the path to our view relative to the views directory separated by periods.
Next, we want to create the sections that correspond to the template. We do that by opening the section with #section() and passing the name of the section we want to push this block of content to. We write our content and then we close the section by using #stop. Pretty simple. For images, css, or js, we simply use the asset() helper again.
I know it's a little long-winded, but hopefully that helps and explains the process a little better.
tl;dr: Use #yield(), #section(), and asset().
So I think I understand what you are saying.
In your blade layout file create a section inside the head:
<head>
#yield('componentcss')
</head>
And in the component do:
#section('componentcss')
{{HTML::style('css/fcradio.css')}}
#stop
You could also just include the css but I wouldn't advise this:
#section('componentcss')
<style>
.exampleclass {text-align:center;}
</style>
#stop
Hopefully I have understood you correctly.
I've finally found a bit tricky but working solution:
#hasSection('fcRadioStyle')
#else
#section('fcRadioStyle')
{{Html::style('css/components/fcradio.css')}}
#stop
#section('styles')
#yield('fcRadioStyle')
#append
#endif
This makes by Form::fcRadio append this style only once

Datalogic Skorpio scanner javascript

We have a browser-based solution that we want to integrate with Datalogic scanners.
We will be using the locked down browser as our primary interface.
We've got as far as configuring the scanner and can confirm that it is decoding our Code 39 barcodes.
We've set up a test page that is supposed to take the scanned code and dump it in a text area.
The test page is
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>TEST</title>
<meta http-equiv="DL_Code_39" content="Enable">
<meta http-equiv="DL_Scan" content="Javascript:ValidateInput()">
<script language="javascript" type="text/javascript">
function ValidateInput(n){
document.getElementById("sku").value+=";"+n;
};
</script>
</head>
<body>
<form method="post" name="fTest">
<textarea rows="5" cols="15" name="sku" id="sku"></textarea><br>
<input type="submit" value="go">
</form>
</body>
</html>
When we scan, the javascript call is firing, but returning undefined.
If we give the javascript function call a variable (something not done in the documentation) it does not fire
We must be missing something simple but there is no sample code in the DL documentation and google can find nothing else either.
Any help would be greatly appreciated.
I have always found javascript support to be flaky on Windows CE. I assume this is what the data logic scanner is running on?
I would normally configure the scanner to act as a keyboard, that way you can use standard html forms and handle the logic server side. I haven't got a Scorpio to test with but the Falcons have this ability under encoding options.
You can also set a prefix and suffix that the scanner will append to the scanned barcode. In your case it looks like this might be ';\n'
I have solved this on my own
The problem in this case is one of the reasons why developers drink too much.
The problem is the name of the example javascript function described in the documentation
This code works perfectly
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>TEST</title>
<meta http-equiv="DL_Triggers" content="Enable">
<meta http-equiv="DL_Code_39" content="Enable">
<meta http-equiv="DL_Scan" content="Javascript:PassSKU">
<script type="text/javascript">
function PassSKU(n){
if (n === undefined) {
n = 0;
}
document.getElementById("sku").value+=";"+n;
};
</script>
</head>
<body>
<form method="post" name="fTest">
<textarea rows="5" cols="20" name="sku" id="sku"></textarea><br>
<input type="submit" value="go">
</form>
</body>
</html>
The only thing I changed was to rename my function call from ValidateInput() (the function name given in the documentation which I copied and pasted) to PassSKU
So
<meta http-equiv="DL_Scan" content="Javascript:ValidateInput">
This does not work
<meta http-equiv="DL_Scan" content="Javascript:AnyOtherFunctionName">
This works fine
WHY this fixed the problem is a topic for another time.
In case someone comes across this question while searching for information on getting a Datalogic scanner to work with a web form, I've posted a working solution here: stackoverflow: "Datalogic Falcon X3 - Barcode Scanner"

Preprocessing HTML files

I'm evaluating brunch build system for my needs. I need to make simple HTML preprocessing. So basically I need to produce several files with common headers and footers:
file1.html:
<html>
<head>
<title>Title1</title>
</head>
<body>
<div id="content">
<div id="header">...</div>
Page1
<div id="footer">...</div>
</div>
</body>
</html>
file2.html:
<html>
<head>
<title>Title2</title>
</head>
<body>
<div id="content">
<div id="header">...</div>
Page2
<div id="footer">...</div>
</div>
</body>
</html>
So either a simple include functionality or (preferrable) some kind of extends functionality. Ideally syntax should hide in comments so my IDE won't complain about non-HTML characters. I liked preprocess javascript library, but that's not necessary, of course.
Unfortunately I didn't find anything suited for that task in brunch. There's support for many HTML template engines, but they seem to generate JS functions. I need simple static HTML as a result, not JavaScript SPA.
I'm not certain there's any built-in solution to this yet, but if I was to go in the direction of HTML templates / partials, I'd look into either "after-brunch" and "before-brunch" plugins on NPM.
I don't know what's your program-language of choice for FileSystem manipulation (read, merge, write, etc.), but in theory you could use something like "before-brunch" to execute a batch / shellscript / or command of somekind to collect your HTML partials together into your file1.html, file2.html, ... before Brunch compiles and copies it to the public/ folder.
In case you're familiar with Haxe, here's a Gist that I shared a while ago. It's a post-process script to merge other files on specific lines of documents.
https://gist.github.com/bigp/90e38deeccc94145b033
Here's what an HTML document could look like:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>DEMO</title>
<link rel="icon" href="data:;base64,=">
<style id="css" type="text/css" rel="stylesheet">
/* #MacroMerge: public/app.css */ //<--- Merges All CSS here..
</style>
</head>
<body>
<script type="text/javascript">
/* #MacroMerge: public/vendor.js, public/app.js */ //<--- And all JS here..
</script>
</body>
</html>
EDIT:
Almost forgot, here's how the brunch-config.coffee script would use the Haxe script with the after-brunch plugin:
plugins:
afterBrunch: [
"haxe -cp . --macro MacroMerge.html('app/index.template.html','public/index.html')"
]
Come to think of it... nothing stops you from taking this example and specifying HTML partials (or any file extension really, ex: *.txt, *.xml) wherever you need them. Again, might only be useful to you if you're familiar with Haxe. If not, it's open-sourced & free to download (http://haxe.org/download/).

How to load static files from view HTML in web2py?

Given a view with layout, how can I load static files (CSS and JS, essentially) into the <head> from the view file?
layout.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="{{=T.accepted_language or 'en'}}">
<head>
<title>{{=response.title or request.application}}</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<!-- include requires CSS files
{{response.files.append(URL(request.application,'static','base.css'))}}
{{response.files.append(URL(request.application,'static','ez-plug-min.css'))}}
-->
{{include 'web2py_ajax.html'}}
</head>
<body>
{{include}}
</body>
</html>
myview.html
{{extend 'layout.html'}}
{{response.files.append(URL(r=request,c='static',f='myview.css'))}}
<h1>Some header</h1>
<div>
some content
</div>
In the above example, the "myview.css" file is either ignored by web2py or stripped out by the browser.
So what is the best way to load page-specific files like this CSS file? I'd rather not stuff all my static files into my layout.
In myview.html reverse the first two lines
{{response.files.append(URL(r=request,c='static',f='myview.css'))}}
{{extend 'layout.html'}}
Mind that 1.78.1 and 1.78.2 had a bug did not allow this to work. It was fixed in 1.78.3 on the same day. The response.file.append(...) can also be moved in the controller action that needs it. You are not supposed to put logic before extend but you define variables to be passed to the extended view.

Resources