Skip to content

Commit

Permalink
v0.2.3
Browse files Browse the repository at this point in the history
  • Loading branch information
aesqe committed Oct 29, 2016
1 parent f587d1c commit 8b231c5
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 65 deletions.
128 changes: 67 additions & 61 deletions app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,56 +13,7 @@ const displayableExtensions = [

const cwd = remote.process.cwd();
const inputArgs = remote.process.argv;
let inputPath = inputArgs.pop();

function parseInput( inputPath = "", dir = cwd )
{
if( ! path.isAbsolute(inputPath) ) {
inputPath = path.resolve(dir, inputPath);
}

let files = [];
let index = 0;
let inputFile = "";
let inputDir = inputPath;

if( sander.existsSync(inputPath) )
{
if( isDisplayableImage(inputPath) )
{
inputFile = path.basename(inputPath);
inputDir = path.dirname(inputPath);
}

files = sander.readdirSync(inputDir)
.filter(isDisplayableImage)
.map(fileName => path.join(inputDir, fileName));

if( inputFile ) {
index = files.indexOf(path.join(inputDir, inputFile));
}
}

return {
files: files.map(slash).map(encodeChars),
index: index
};
}

function slash (str) {
return str.replace(/\\/g, "/");
}

function encodeChars( str ) {
return str.replace(/\s/g, "%20");
}

function isDisplayableImage ( inputPath )
{
const ext = path.extname(inputPath).slice(1);
return ext && displayableExtensions.indexOf(ext) > -1;
}

let inputPath = inputArgs.pop();
const templateString = sander.readFileSync(
path.join(__dirname, "template.html"),
{ encoding: "utf-8" }
Expand All @@ -79,16 +30,16 @@ const app = new Ractive({
data: function() {
return {
currentIndex: 0,
images: []
files: []
};
},

computed: {
currentImage: function()
{
const images = this.get("images");
const files = this.get("files");
const currentIndex = this.get("currentIndex");
return images[currentIndex] || "";
return files[currentIndex] || "";
},
previousButtonHidden: function(){
return ! this.hasPreviousImage() ? "hidden" : "";
Expand All @@ -102,8 +53,8 @@ const app = new Ractive({
{
this.on({
input: function (data) {
const { files, index } = parseInput(data);
this.set("images", files);
const { files, index } = this.parseInput(data);
this.set("files", files);
this.set("currentIndex", index);
},

Expand Down Expand Up @@ -140,6 +91,61 @@ const app = new Ractive({
this.fire("input", inputPath);
},

parseInput: function( inputPath = "", dir = cwd )
{
if( ! path.isAbsolute(inputPath) ) {
inputPath = path.resolve(dir, inputPath);
}

let files = [];
let index = 0;
let inputFile = "";
let inputDir = inputPath;

if( sander.existsSync(inputPath) )
{
if( this.isDisplayableImage(inputPath) )
{
inputFile = path.basename(inputPath);
inputDir = path.dirname(inputPath);
}

if( this.isDirectory(inputDir) )
{
files = sander.readdirSync(inputDir)
.filter(this.isDisplayableImage)
.map(fileName => path.join(inputDir, fileName));
}

if( files.length && inputFile ) {
index = files.indexOf(path.join(inputDir, inputFile));
}
}

return {
files: files.map(this.slash).map(this.encodeChars),
index: index
};
},

isDisplayableImage: function ( inputPath ) {
const ext = path.extname(inputPath).slice(1);
return ext && displayableExtensions.indexOf(ext) > -1;
},

isDirectory: function ( inputPath ) {
const stats = sander.lstatSync(inputPath);
return stats.isDirectory();
},

slash: function (str) {
return str.replace(/\\/g, "/");
},

encodeChars: function( str ) {
return str.replace(/\s/g, "%20");
},

handleMouseWheel: function(e, d, dx, dy){
if( dy === 1 ) {
this.fire("nextImage");
Expand All @@ -158,8 +164,8 @@ const app = new Ractive({

hasNextImage: function () {
const i = this.get("currentIndex");
const images = this.get("images");
const len = images.length - 1;
const files = this.get("files");
const len = files.length - 1;
return i < len;
},

Expand All @@ -186,18 +192,18 @@ document.ondragover = document.ondrop = (e) => {
}

document.ondragover = (e) => {
app.set("dragover", true);
e.preventDefault();
app.set("dragover", true);
}

document.ondragleave = document.ondragexit = document.ondragend = (e) => {
app.set("dragover", false);
e.preventDefault();
app.set("dragover", false);
}

document.body.ondrop = (e) => {
app.set("dragover", false);
e.preventDefault();
const inputPath = e.dataTransfer.files[0].path;
app.fire("input", inputPath);
e.preventDefault();
app.set("dragover", false);
}
6 changes: 3 additions & 3 deletions app/template.html
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<div class="window {{maximized ? 'maximized' : ''}} {{dragover ? 'dragover' : ''}}">

{{#if images.length}}
{{#if files.length}}
<header class="toolbar toolbar-header text-center">
<div class="toolbar-actions">
<div class="btn-group">
<button class="btn btn-default" on-tap="previousImage" disabled="{{previousButtonHidden}}">
<span class="icon icon-left-dir"></span>
</button>
<span id="current-of-display" class="btn">{{currentIndex+1}} of {{images.length}}</span>
<span id="current-of-display" class="btn">{{currentIndex+1}} of {{files.length}}</span>
<button class="btn btn-default" on-tap="nextImage" disabled="{{nextButtonHidden}}">
<span class="icon icon-right-dir"></span>
</button>
Expand All @@ -17,7 +17,7 @@
{{/if}}

<div class="window-content">
{{#if images.length}}
{{#if files.length}}
<div id="display" style="background-image: url({{currentImage}});"></div>
{{else}}
<div id="drag-drop">
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "electron-image-viewer",
"productName": "electron-image-viewer",
"version": "0.2.2",
"version": "0.2.3",
"description": "Image viewer",
"main": "main.js",
"author": "aesqe",
Expand Down

0 comments on commit 8b231c5

Please sign in to comment.