Skip to content

Commit

Permalink
Updated package
Browse files Browse the repository at this point in the history
  • Loading branch information
daenuprobst committed Jan 29, 2018
1 parent f6bb545 commit 21e3d85
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 72 deletions.
24 changes: 18 additions & 6 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,23 @@
import Drawer from './src/Drawer'
import Parser from './src/Parser'


// Detect SSR (server side rendering)
var canUseDOM = !!(
(typeof window !== 'undefined' &&
window.document && window.document.createElement)
);

/**
* The SmilesDrawer namespace.
* @typicalname SmilesDrawer
*/
window.SmilesDrawer = {
var SmilesDrawer = {
Version: '1.0.0'
};

window.SmilesDrawer.Drawer = Drawer;
window.SmilesDrawer.Parser = Parser;
SmilesDrawer.Drawer = Drawer;
SmilesDrawer.Parser = Parser;

/**
* Cleans a SMILES string (removes non-valid characters)
Expand All @@ -20,7 +27,7 @@ window.SmilesDrawer.Parser = Parser;
* @param {String} smiles A SMILES string.
* @returns {String} The clean SMILES string.
*/
window.SmilesDrawer.clean = function(smiles) {
SmilesDrawer.clean = function(smiles) {
return smiles.replace(/[^A-Za-z0-9@\.\+\-\?!\(\)\[\]\{\}/\\=#\$:\*]/g,'');
}

Expand All @@ -33,7 +40,7 @@ window.SmilesDrawer.clean = function(smiles) {
* @param {String} [themeName='light'] The theme to apply.
* @param {Function} [onError='null'] A callback function providing an error object.
*/
window.SmilesDrawer.apply = function(options, selector='canvas[data-smiles]', themeName='light', onError=null) {
SmilesDrawer.apply = function(options, selector='canvas[data-smiles]', themeName='light', onError=null) {
let smilesDrawer = new Drawer(options);
let elements = document.querySelectorAll(selector);

Expand All @@ -58,7 +65,7 @@ window.SmilesDrawer.apply = function(options, selector='canvas[data-smiles]', th
* @param {Function} successCallback A callback that is called on success with the parse tree.
* @param {Function} errorCallback A callback that is called with the error object on error.
*/
window.SmilesDrawer.parse = function(smiles, successCallback, errorCallback) {
SmilesDrawer.parse = function(smiles, successCallback, errorCallback) {
try {
if (successCallback) {
successCallback(Parser.parse(smiles));
Expand All @@ -70,6 +77,11 @@ window.SmilesDrawer.parse = function(smiles, successCallback, errorCallback) {
}
}

if (canUseDOM) {
window.SmilesDrawer = SmilesDrawer;
}

module.exports = SmilesDrawer;

// There be dragons (polyfills)

Expand Down
135 changes: 72 additions & 63 deletions dist/smiles-drawer.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,20 @@ var _Parser2 = _interopRequireDefault(_Parser);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

// Detect SSR (server side rendering)
//@ts-check
var canUseDOM = !!(typeof window !== 'undefined' && window.document && window.document.createElement);

/**
* The SmilesDrawer namespace.
* @typicalname SmilesDrawer
*/
//@ts-check
window.SmilesDrawer = {
Version: '1.0.0'
var SmilesDrawer = {
Version: '1.0.0'
};

window.SmilesDrawer.Drawer = _Drawer2.default;
window.SmilesDrawer.Parser = _Parser2.default;
SmilesDrawer.Drawer = _Drawer2.default;
SmilesDrawer.Parser = _Parser2.default;

/**
* Cleans a SMILES string (removes non-valid characters)
Expand All @@ -30,8 +33,8 @@ window.SmilesDrawer.Parser = _Parser2.default;
* @param {String} smiles A SMILES string.
* @returns {String} The clean SMILES string.
*/
window.SmilesDrawer.clean = function (smiles) {
return smiles.replace(/[^A-Za-z0-9@\.\+\-\?!\(\)\[\]\{\}/\\=#\$:\*]/g, '');
SmilesDrawer.clean = function (smiles) {
return smiles.replace(/[^A-Za-z0-9@\.\+\-\?!\(\)\[\]\{\}/\\=#\$:\*]/g, '');
};

/**
Expand All @@ -43,29 +46,29 @@ window.SmilesDrawer.clean = function (smiles) {
* @param {String} [themeName='light'] The theme to apply.
* @param {Function} [onError='null'] A callback function providing an error object.
*/
window.SmilesDrawer.apply = function (options) {
var selector = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'canvas[data-smiles]';
var themeName = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'light';
var onError = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : null;
SmilesDrawer.apply = function (options) {
var selector = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'canvas[data-smiles]';
var themeName = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'light';
var onError = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : null;

var smilesDrawer = new _Drawer2.default(options);
var elements = document.querySelectorAll(selector);
var smilesDrawer = new _Drawer2.default(options);
var elements = document.querySelectorAll(selector);

var _loop = function _loop() {
var element = elements[i];
var _loop = function _loop() {
var element = elements[i];

SmilesDrawer.parse(element.getAttribute('data-smiles'), function (tree) {
smilesDrawer.draw(tree, element, themeName, false);
}, function (err) {
if (onError) {
onError(err);
}
});
};
SmilesDrawer.parse(element.getAttribute('data-smiles'), function (tree) {
smilesDrawer.draw(tree, element, themeName, false);
}, function (err) {
if (onError) {
onError(err);
}
});
};

for (var i = 0; i < elements.length; i++) {
_loop();
}
for (var i = 0; i < elements.length; i++) {
_loop();
}
};

/**
Expand All @@ -76,58 +79,64 @@ window.SmilesDrawer.apply = function (options) {
* @param {Function} successCallback A callback that is called on success with the parse tree.
* @param {Function} errorCallback A callback that is called with the error object on error.
*/
window.SmilesDrawer.parse = function (smiles, successCallback, errorCallback) {
try {
if (successCallback) {
successCallback(_Parser2.default.parse(smiles));
}
} catch (err) {
if (errorCallback) {
errorCallback(err);
}
SmilesDrawer.parse = function (smiles, successCallback, errorCallback) {
try {
if (successCallback) {
successCallback(_Parser2.default.parse(smiles));
}
} catch (err) {
if (errorCallback) {
errorCallback(err);
}
}
};

if (canUseDOM) {
window.SmilesDrawer = SmilesDrawer;
}

module.exports = SmilesDrawer;

// There be dragons (polyfills)

if (!Array.prototype.fill) {
Object.defineProperty(Array.prototype, 'fill', {
value: function value(_value) {
Object.defineProperty(Array.prototype, 'fill', {
value: function value(_value) {

// Steps 1-2.
if (this == null) {
throw new TypeError('this is null or not defined');
}
// Steps 1-2.
if (this == null) {
throw new TypeError('this is null or not defined');
}

var O = Object(this);
var O = Object(this);

// Steps 3-5.
var len = O.length >>> 0;
// Steps 3-5.
var len = O.length >>> 0;

// Steps 6-7.
var start = arguments[1];
var relativeStart = start >> 0;
// Steps 6-7.
var start = arguments[1];
var relativeStart = start >> 0;

// Step 8.
var k = relativeStart < 0 ? Math.max(len + relativeStart, 0) : Math.min(relativeStart, len);
// Step 8.
var k = relativeStart < 0 ? Math.max(len + relativeStart, 0) : Math.min(relativeStart, len);

// Steps 9-10.
var end = arguments[2];
var relativeEnd = end === undefined ? len : end >> 0;
// Steps 9-10.
var end = arguments[2];
var relativeEnd = end === undefined ? len : end >> 0;

// Step 11.
var final = relativeEnd < 0 ? Math.max(len + relativeEnd, 0) : Math.min(relativeEnd, len);
// Step 11.
var final = relativeEnd < 0 ? Math.max(len + relativeEnd, 0) : Math.min(relativeEnd, len);

// Step 12.
while (k < final) {
O[k] = _value;
k++;
}
// Step 12.
while (k < final) {
O[k] = _value;
k++;
}

// Step 13.
return O;
}
});
// Step 13.
return O;
}
});
}

},{"./src/Drawer":5,"./src/Parser":10}],2:[function(require,module,exports){
Expand Down
2 changes: 1 addition & 1 deletion dist/smiles-drawer.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/smiles-drawer.min.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "smiles-drawer",
"version": "1.0.3",
"version": "1.0.4",
"description": "A SMILES drawer and parser. Generate molecular structure depictions in pure JavaScript.",
"main": "./app.js",
"repository": "https://github.com/reymond-group/smilesDrawer.git",
Expand Down

0 comments on commit 21e3d85

Please sign in to comment.