Skip to content

Instantly share code, notes, and snippets.

@m-abs
m-abs / dropshadow.directive.ts
Last active June 6, 2019 11:13
Nativescript angular dropshow directive
import { Directive, ElementRef, Input, OnDestroy } from '@angular/core';
import { fromEvent, merge, Observable, ReplaySubject } from 'rxjs';
import { debounceTime } from 'rxjs/operators';
import { Color } from 'tns-core-modules/color';
import * as knownColors from 'tns-core-modules/color/known-colors';
import { isAndroid, isIOS, screen } from 'tns-core-modules/platform';
import { View } from 'tns-core-modules/ui/core/view';
/**
* Directive for adding a drop shadow to an element.
@m-abs
m-abs / update-label.directive.ts
Created May 7, 2018 16:04
NativeScript resize label on text change:
import { Directive, ElementRef, Input } from '@angular/core';
import { Label } from 'tns-core-modules/ui/label';
/**
* Make sure the layout is updated then the Label's text is changed.
*/
@Directive({
// tslint:disable-next-line:directive-selector
selector: 'Label[text]',
})
@m-abs
m-abs / apply-patches.sh
Created March 28, 2018 12:06
patch nrwl/nx formatter for nativescript projects
#!/bin/bash
realpath() {
[[ $1 = /* ]] && echo "$1" || (cd "$PWD/${1#./}" && echo $PWD);
}
apply_patch() {
PATCH=$1
patch -p0 -N --dry-run --silent -i $PATCH 2>&1 > /dev/null
if [[ $? -eq 0 ]]; then
patch -p0 -N --silent -i $PATCH
@m-abs
m-abs / index.ts
Created January 12, 2018 15:03
@ngx-translate/core with nativescript-angular
import { TranslateLoader, TranslateModule } from '@ngx-translate/core';
import * as fs from 'tns-core-modules/file-system';
@Injectable()
export class NSTranslateLoader extends TranslateLoader {
public getTranslation(lang: string) {
return fromPromise(fs.File.fromPath(fs.path.join(fs.knownFolders.currentApp().path, `/assets/i18n/`, `${lang}.json`)).readText('UTF-8'))
.map((text: string) => JSON.parse(text));
}
}
@m-abs
m-abs / main.ts
Last active September 6, 2018 14:33
Use async/await with NativeScript
// this import should be first in order to load some required settings (like globals and reflect-metadata)
import { platformNativeScriptDynamic } from "nativescript-angular/platform";
import { AppModule } from "./app.module";
import './tslib.nativescript'; // <-- ADD THIS LINE
// A traditional NativeScript application starts by initializing global objects, setting up global CSS rules, creating, and navigating to the main page.
// Angular applications need to take care of their own initialization: modules, components, directives, routes, DI providers.
// A NativeScript Angular app needs to make both paradigms work together, so we provide a wrapper platform object, platformNativeScriptDynamic,
@m-abs
m-abs / label-max-lines.directive.ts
Created March 10, 2017 14:38
Directive for NativeScript-angular, adding the property maxLines to Label
import { Directive, ElementRef, Input, OnInit, OnChanges } from '@angular/core';
import { Label } from 'ui/label';
@Directive({
selector: '[maxLines]',
})
export class LabelMaxLinesDirective implements OnInit, OnChanges {
@Input('maxLines') public maxLines: number = 1;
public get nativeView(): Label {
@m-abs
m-abs / image-cache.pipe.ts
Last active February 26, 2020 21:16
ImageCache pipe for NativeScript-Angular
import { Pipe, PipeTransform, NgZone } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { ImageSource, fromResource } from 'image-source';
import { Cache } from 'ui/image-cache';
import { isFileOrResourcePath, isDataURI } from 'utils/utils';
let cache = new Cache();
cache.placeholder = fromResource('placeholder');
@m-abs
m-abs / json_without_string_keys.js
Created March 12, 2012 15:21
Example of the JSON object without quotation around object keys
{
key1 : {
subkey1 : 1,
subkey2 : 2
},
key2 : {
subkey1 : 1,
subkey2 : 2
}
}
@m-abs
m-abs / json_with_string_keys.js
Created March 12, 2012 15:20
Example of the JSON object with quotation around object keys
{
"key1" : {
"subkey1" : 1,
"subkey2" : 2
},
"key2" : {
"subkey1" : 1,
"subkey2" : 2
}
}