Skip to content

Commit

Permalink
fix error checking, makes check for keys before trying to access
Browse files Browse the repository at this point in the history
  • Loading branch information
paperscissors committed Nov 8, 2017
1 parent e3f73cd commit 2c89391
Show file tree
Hide file tree
Showing 5 changed files with 182 additions and 13 deletions.
16 changes: 4 additions & 12 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
{
"name": "vinceg/usps-php-api",
"name": "paperscissors/usps-php-api",
"type": "library",
"description": "PHP wrapper for USPS Web Tools API",
"description": "Fork of PHP wrapper for USPS Web Tools API with quick issue fixes; originally by Vincent Gabriel et al.",
"keywords": ["usps", "api"],
"license": "MIT",
"authors": [
{
"name": "Vincent Gabriel",
"homepage": "http://vadimg.com",
"name": "Tom Filepp",
"homepage": "https://paperscissorsandglue.com",
"role": "Developer"
},
{
"name": "Alexander Reiff",
"role": "Developer"
},
{
"name": "Ryan Hayle",
"role": "Developer"
}
],
"require": {
Expand Down
55 changes: 55 additions & 0 deletions helpers/context_menu.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// This gives you default context menu (cut, copy, paste)
// in all input fields and textareas across your app.

import { remote } from 'electron';

const Menu = remote.Menu;
const MenuItem = remote.MenuItem;

const isAnyTextSelected = () => {
return window.getSelection().toString() !== '';
};

const cut = new MenuItem({
label: 'Cut',
click: () => {
document.execCommand('cut');
},
});

const copy = new MenuItem({
label: 'Copy',
click: () => {
document.execCommand('copy');
},
});

const paste = new MenuItem({
label: 'Paste',
click: () => {
document.execCommand('paste');
},
});

const normalMenu = new Menu();
normalMenu.append(copy);

const textEditingMenu = new Menu();
textEditingMenu.append(cut);
textEditingMenu.append(copy);
textEditingMenu.append(paste);

document.addEventListener('contextmenu', (event) => {
switch (event.target.nodeName) {
case 'TEXTAREA':
case 'INPUT':
event.preventDefault();
textEditingMenu.popup(remote.getCurrentWindow());
break;
default:
if (isAnyTextSelected()) {
event.preventDefault();
normalMenu.popup(remote.getCurrentWindow());
}
}
}, false);
40 changes: 40 additions & 0 deletions helpers/external_links.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Convenient way for opening links in external browser, not in the app.
// Useful especially if you have a lot of links to deal with.
//
// Usage:
//
// Every link with class ".js-external-link" will be opened in external browser.
// <a class="js-external-link" href="http://google.com">google</a>
//
// The same behaviour for many links can be achieved by adding
// this class to any parent tag of an anchor tag.
// <p class="js-external-link">
// <a href="http://google.com">google</a>
// <a href="http://bing.com">bing</a>
// </p>

import { shell } from 'electron';

const supportExternalLinks = (event) => {
let href;
let isExternal = false;

const checkDomElement = (element) => {
if (element.nodeName === 'A') {
href = element.getAttribute('href');
}
if (element.classList.contains('js-external-link')) {
isExternal = true;
}
if (href && isExternal) {
shell.openExternal(href);
event.preventDefault();
} else if (element.parentElement) {
checkDomElement(element.parentElement);
}
};

checkDomElement(event.target);
};

document.addEventListener('click', supportExternalLinks, false);
82 changes: 82 additions & 0 deletions helpers/window.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// This helper remembers the size and position of your windows (and restores
// them in that place after app relaunch).
// Can be used for more than one window, just construct many
// instances of it and give each different name.

import { app, BrowserWindow, screen } from 'electron';
import jetpack from 'fs-jetpack';

export default (name, options) => {
const userDataDir = jetpack.cwd(app.getPath('userData'));
const stateStoreFile = `window-state-${name}.json`;
const defaultSize = {
width: options.width,
height: options.height,
};
let state = {};
let win;

const restore = () => {
let restoredState = {};
try {
restoredState = userDataDir.read(stateStoreFile, 'json');
} catch (err) {
// For some reason json can't be read (might be corrupted).
// No worries, we have defaults.
}
return Object.assign({}, defaultSize, restoredState);
};

const getCurrentPosition = () => {
const position = win.getPosition();
const size = win.getSize();
return {
x: position[0],
y: position[1],
width: size[0],
height: size[1],
};
};

const windowWithinBounds = (windowState, bounds) => {
return windowState.x >= bounds.x
&& windowState.y >= bounds.y
&& windowState.x + windowState.width <= bounds.x + bounds.width
&& windowState.y + windowState.height <= bounds.y + bounds.height;
};

const resetToDefaults = () => {
const bounds = screen.getPrimaryDisplay().bounds;
return Object.assign({}, defaultSize, {
x: (bounds.width - defaultSize.width) / 2,
y: (bounds.height - defaultSize.height) / 2,
});
};

const ensureVisibleOnSomeDisplay = (windowState) => {
const visible = screen.getAllDisplays().some((display) => {
return windowWithinBounds(windowState, display.bounds);
});
if (!visible) {
// Window is partially or fully not visible now.
// Reset it to safe defaults.
return resetToDefaults();
}
return windowState;
};

const saveState = () => {
if (!win.isMinimized() && !win.isMaximized()) {
Object.assign(state, getCurrentPosition());
}
userDataDir.write(stateStoreFile, state, { atomic: true });
};

state = ensureVisibleOnSomeDisplay(restore());

win = new BrowserWindow(Object.assign({}, options, state));

win.on('close', saveState);

return win;
};
2 changes: 1 addition & 1 deletion src/USPSBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ protected function doRequest($ch = null)
// Find the error number
$errorInfo = $this->getValueByKey($arrayResponse, 'Error');

if ($errorInfo) {
if (isset($errorInfo['Number'], $errorInfo['Description'])) {
$this->setErrorCode($errorInfo['Number']);
$this->setErrorMessage($errorInfo['Description']);
}
Expand Down

0 comments on commit 2c89391

Please sign in to comment.