<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>MyApp</title>
<!--<base href="/">-->
<script>document.write('<base href="' + document.location + '" />');</script>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="styles.css">
<link rel="icon" type="image/x-icon" href="favicon.ico">
<script src="https://unpkg.com/core-js/client/shim.min.js"></script>
<script src="https://unpkg.com/zone.js@0.7.4?main=browser"></script>
<script src="https://unpkg.com/systemjs@0.19.39/dist/system.src.js"></script>
<script src="systemjs.config.js"></script>
<script>
System.import('main.js').catch(function(err){ console.error(err); });
</script>
</head>
<body>
<app-root></app-root>
</body>
</html>
import { enableProdMode } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app/app.module';
platformBrowserDynamic().bootstrapModule(AppModule);
/*
Copyright 2017 Google Inc. All Rights Reserved.
Use of this source code is governed by an MIT-style license that
can be found in the LICENSE file at http://angular.io/license
*/
.container {
height: 500px;
width: 750px;
overflow: scroll;
}
.grid {
display: grid;
}
.cell {
border: 1px solid #b0d347;
}
<div class="container" (scroll)="onScroll($event)">
<div class="grid" [ngStyle]="getGridDefinition()">
<div [ngStyle]="totalSpan()"></div>
<div class="cell" *ngFor="let cell of gridCells" [ngStyle]="positionDataCell(cell)">
{{ cell.data }}
</div>
</div>
</div>
import { Component, ElementRef, OnInit } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
public dataCells: any[];
public gridCells: any[];
private bufferCells = 1;
private totalColumns = 50;
private totalRows = 50;
private columnWidth = 50;
private rowHeight = 50;
private totalVisibleColumns;
private totalVisibleRows;
private lastRowIndex = 0;
private lastColIndex = 0;
constructor(private el: ElementRef) {
}
ngOnInit() {
this.calculateVisibleCells(this.bufferCells);
this.dataCells = this.generateDataCells();
this.gridCells = this.createGridCells(this.totalVisibleRows, this.totalVisibleColumns);
}
public totalSpan() {
return {
'grid-row': '1 / span ' + this.totalRows,
'grid-column': '1 / span ' + this.totalColumns
};
}
public onScroll(event: Event): void {
const firstVisibleRow = Math.ceil((event.srcElement.scrollTop) / this.rowHeight);
const firstVisibleCol = Math.ceil((event.srcElement.scrollLeft) / this.columnWidth);
if (firstVisibleRow !== this.lastRowIndex && firstVisibleRow + this.totalVisibleRows <= this.totalRows + 1) {
this.shiftToRow(firstVisibleRow);
this.lastRowIndex = firstVisibleRow;
}
if (firstVisibleCol !== this.lastColIndex && firstVisibleCol + this.totalVisibleColumns <= this.totalColumns + 1) {
this.shiftToCol(firstVisibleCol);
this.lastColIndex = firstVisibleCol;
}
}
private shiftToRow(row: number): void {
let gridCellIndex = 0;
for (let i = 1; i <= this.totalVisibleColumns; i++) {
let shiftToRow = row;
const colIndex = this.lastColIndex + i;
for (let j = 1; j <= this.totalVisibleRows; j++) {
const dataCell = this.getDataCellByPosition(colIndex, shiftToRow);
const gridCell = this.gridCells[gridCellIndex];
if (dataCell) {
gridCell.col = colIndex;
gridCell.row = shiftToRow;
gridCell.data = dataCell.id;
}
shiftToRow++;
gridCellIndex++;
}
}
}
private shiftToCol(col: number): void {
let gridCellIndex = 0;
for (let i = 1; i <= this.totalVisibleRows; i++) {
let shiftToCol = col;
const rowIndex = this.lastRowIndex + i;
for (let j = 1; j <= this.totalVisibleColumns; j++) {
const dataCell = this.getDataCellByPosition(rowIndex, shiftToCol);
const gridCell = this.gridCells[gridCellIndex];
if (dataCell) {
gridCell.col = shiftToCol;
gridCell.row = rowIndex;
gridCell.data = dataCell.id;
}
shiftToCol++;
gridCellIndex++;
}
}
}
private getDataCellByPosition(colIndex, rowIndex) {
return this.dataCells.find(cell => cell.col === colIndex && cell.row === rowIndex);
}
private calculateVisibleCells(bufferCells: number) {
const containerWidth = this.el.nativeElement.firstElementChild.offsetWidth;
const containerHeight = this.el.nativeElement.firstElementChild.offsetHeight;
this.totalVisibleColumns = Math.ceil(containerWidth / this.columnWidth) + bufferCells;
this.totalVisibleRows = Math.ceil(containerHeight / this.rowHeight) + bufferCells;
}
private createGridCells(totalVisibleRows: number, totalVisibleColumns: number): any[] {
const gridCells = [];
for (let i = 1; i <= totalVisibleColumns; i++) {
for (let j = 1; j <= totalVisibleRows; j++) {
gridCells.push(this.createCell(i, j));
}
}
return gridCells;
}
public getGridDefinition() {
return {
'grid-auto-columns': this.columnWidth + 'px',
'grid-auto-rows': this.rowHeight + 'px'
};
}
public positionDataCell(cell) {
return {
'grid-row': cell.row,
'grid-column': cell.col
};
}
private generateDataCells(): any[] {
const data = [];
// We start at 1 since CSS grid isn't 0 based.
for (let i = 1; i <= this.totalColumns; i++) {
for (let j = 1; j <= this.totalRows; j++) {
data.push(this.createCell(i, j));
}
}
return data;
}
private createCell(col: number, row: number): { id: string, col: number, row: number, data: string } {
return {
id: col + '-' + row,
col: col,
row: row,
data: col + '-' + row
};
}
}
import {BrowserModule} from '@angular/platform-browser';
import {NgModule} from '@angular/core';
import {FormsModule} from '@angular/forms';
import {AppComponent} from './app.component';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
FormsModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule {}
var templateUrlRegex = /templateUrl\s*:(\s*['"`](.*?)['"`]\s*)/gm;
var stylesRegex = /styleUrls *:(\s*\[[^\]]*?\])/g;
var stringRegex = /(['`"])((?:[^\\]\\\1|.)*?)\1/g;
module.exports.translate = function(load){
if (load.source.indexOf('moduleId') != -1) return load;
var url = document.createElement('a');
url.href = load.address;
var basePathParts = url.pathname.split('/');
basePathParts.pop();
var basePath = basePathParts.join('/');
var baseHref = document.createElement('a');
baseHref.href = this.baseURL;
baseHref = baseHref.pathname;
if (!baseHref.startsWith('/base/')) { // it is not karma
basePath = basePath.replace(baseHref, '');
}
load.source = load.source
.replace(templateUrlRegex, function(match, quote, url){
var resolvedUrl = url;
if (url.startsWith('.')) {
resolvedUrl = basePath + url.substr(1);
}
return 'templateUrl: "' + resolvedUrl + '"';
})
.replace(stylesRegex, function(match, relativeUrls) {
var urls = [];
while ((match = stringRegex.exec(relativeUrls)) !== null) {
if (match[2].startsWith('.')) {
urls.push('"' + basePath + match[2].substr(1) + '"');
} else {
urls.push('"' + match[2] + '"');
}
}
return "styleUrls: [" + urls.join(', ') + "]";
});
return load;
};
/**
* WEB ANGULAR VERSION
* (based on systemjs.config.js in angular.io)
* System configuration for Angular samples
* Adjust as necessary for your application needs.
*/
(function (global) {
System.config({
// DEMO ONLY! REAL CODE SHOULD NOT TRANSPILE IN THE BROWSER
transpiler: 'ts',
typescriptOptions: {
// Copy of compiler options in standard tsconfig.json
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"lib": ["es2015", "dom"],
"noImplicitAny": true,
"suppressImplicitAnyIndexErrors": true
},
meta: {
'typescript': {
"exports": "ts"
}
},
paths: {
// paths serve as alias
'npm:': 'https://unpkg.com/'
},
// map tells the System loader where to look for things
map: {
// our app is within the app folder
'app': 'app',
// angular bundles
'@angular/animations': 'npm:@angular/animations/bundles/animations.umd.js',
'@angular/animations/browser': 'npm:@angular/animations/bundles/animations-browser.umd.js',
'@angular/core': 'npm:@angular/core/bundles/core.umd.js',
'@angular/common': 'npm:@angular/common/bundles/common.umd.js',
'@angular/common/http': 'npm:@angular/common/bundles/common-http.umd.js',
'@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
'@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
'@angular/platform-browser/animations': 'npm:@angular/platform-browser/bundles/platform-browser-animations.umd.js',
'@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
'@angular/http': 'npm:@angular/http/bundles/http.umd.js',
'@angular/router': 'npm:@angular/router/bundles/router.umd.js',
'@angular/router/upgrade': 'npm:@angular/router/bundles/router-upgrade.umd.js',
'@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js',
'@angular/upgrade': 'npm:@angular/upgrade/bundles/upgrade.umd.js',
'@angular/upgrade/static': 'npm:@angular/upgrade/bundles/upgrade-static.umd.js',
// other libraries
'rxjs': 'npm:rxjs@5.5.2',
'rxjs/operators': 'npm:rxjs@5.5.2/operators/index.js',
'tslib': 'npm:tslib/tslib.js',
'angular-in-memory-web-api': 'npm:angular-in-memory-web-api@0.4/bundles/in-memory-web-api.umd.js',
'ts': 'npm:plugin-typescript@5.2.7/lib/plugin.js',
'typescript': 'npm:typescript@2.4.2/lib/typescript.js',
},
// packages tells the System loader how to load when no filename and/or no extension
packages: {
app: {
main: './main.ts',
defaultExtension: 'ts',
meta: {
'./*.ts': {
loader: 'systemjs-angular-loader.js'
}
}
},
rxjs: {
defaultExtension: 'js'
}
}
});
})(this);
/*
Copyright 2016 Google Inc. All Rights Reserved.
Use of this source code is governed by an MIT-style license that
can be found in the LICENSE file at http://angular.io/license
*/
/* Application-wide Styles */
h1 {
color: #369;
font-family: Arial, Helvetica, sans-serif;
font-size: 250%;
}
h2, h3 {
color: #444;
font-family: Arial, Helvetica, sans-serif;
font-weight: lighter;
}
body {
margin: 2em;
}
body, input[text], button {
color: #888;
font-family: Cambria, Georgia;
}
/* everywhere else */
* {
font-family: Arial, Helvetica, sans-serif;
}