Skip to content

Commit

Permalink
feat(lib): compile src to lib
Browse files Browse the repository at this point in the history
  • Loading branch information
tomchentw committed Oct 19, 2014
1 parent cac3da7 commit 44f406d
Show file tree
Hide file tree
Showing 5 changed files with 323 additions and 0 deletions.
121 changes: 121 additions & 0 deletions lib/ToastContainer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/** @jsx React.DOM */
var React = require("react/addons");
var $__0= React.addons,update=$__0.update;

var ToastMessage = require("./ToastMessage");

function noop () {}

module.exports = React.createClass({
displayName: "ToastContainer",

error:function (message, title, optionsOverride) {
this._notify(this.props.toastType.error, message, title, optionsOverride);
},

info:function (message, title, optionsOverride) {
this._notify(this.props.toastType.info, message, title, optionsOverride);
},

success:function (message, title, optionsOverride) {
this._notify(this.props.toastType.success, message, title, optionsOverride);
},

warning:function (message, title, optionsOverride) {
this._notify(this.props.toastType.warning, message, title, optionsOverride);
},

clear:function () {
var $__0= this,refs=$__0.refs,
key;
for (key in refs) {
refs[key].hideToast(false);
}
},

getDefaultProps:function () {
return {
toastType: {
error: "error",
info: "info",
success: "success",
warning: "warning"
},
id: "toast-container",
toastMessageClass: ToastMessage,
preventDuplicates: false,
newestOnTop: true,
onClick: noop
};
},

getInitialState:function () {
return {
toasts: [],
toastId: 0,
previousMessage: null
};
},

render:function () {
var $__0= this,props=$__0.props,state=$__0.state;
return this.transferPropsTo(
React.DOM.div({'aria-live': "polite", role: "alert"},
state.toasts.map(function(toast) {
return props.toastMessageClass(toast);
})
)
);
},

_notify:function (type, message, title, optionsOverride) {
var $__0= this,props=$__0.props,state=$__0.state;
if (props.preventDuplicates) {
if (state.previousMessage === message) {
return;
}
}
var key = state.toastId++;
var newToast = update(optionsOverride || {}, {
$merge: {
type:type,
title:title,
message:message,
key:key,
ref: ("toasts__" + key),
handleOnClick: this._handle_toast_on_click,
handleRemove: this._handle_toast_remove
}
});
var toastOperation = {};
toastOperation[(( props.newestOnTop ? "$unshift" : "$push"))] = [newToast];

var newState = update(state, {
toasts: toastOperation,
previousMessage: { $set: message }
});
this.setState(newState);
},

_handle_toast_on_click:function (event) {
this.props.onClick(event);
if (event.defaultPrevented) {
return;
}
event.preventDefault();
event.stopPropagation();
},

_handle_toast_remove:function (key) {
var $__0= this,state=$__0.state;
state.toasts[(( this.props.newestOnTop ? "reduceRight" : "reduce"))](function(found, toast, index) {
if (found || toast.key !== key) {
return false;
}
this.setState(update(state, {
toasts: { $splice: [[index, 1]] }
}));
return true;
}.bind(this), false);
}
});
104 changes: 104 additions & 0 deletions lib/ToastMessage/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/** @jsx React.DOM */
var React = require("react/addons");
var $__0= React.addons,update=$__0.update;

function noop () {}

var ToastMessageSpec = {
displayName: "ToastMessage",

getDefaultProps:function () {
var iconClassNames = {
error: "toast-error",
info: "toast-info",
success: "toast-success",
warning: "toast-warning"
};

return {
className: "toast",
iconClassNames: iconClassNames,
titleClassName: "toast-title",
messageClassName: "toast-message",
tapToDismiss: true,
closeButton: false
};
},

handleOnClick:function (event) {
var $__0= this,props=$__0.props;
props.handleOnClick(event);
if (props.tapToDismiss) {
this.hideToast(true);
}
},

_handle_close_button_click:function (event) {
event.stopPropagation();
this.hideToast(true);
},

_handle_remove:function () {
var $__0= this,props=$__0.props;
props.handleRemove(props.key);
},

_render_close_button:function (props) {
return props.closeButton ? (
React.DOM.button({className: "toast-close-button", role: "button",
onClick: this._handle_close_button_click}, "×")
) : false;
},

_render_title_element:function (props) {
return props.title ? (
React.DOM.div({className: props.titleClassName},
props.title
)
) : false;
},

_render_message_element:function (props) {
return props.message ? (
React.DOM.div({className: props.messageClassName},
props.message
)
) : false;
},

render:function () {
var cx = React.addons.classSet;
var $__0= this,props=$__0.props;
var iconClassName = props.iconClassName || props.iconClassNames[props.type];

var toastClass = {};
toastClass[props.className] = true;
toastClass[iconClassName] = true;

return (
React.DOM.div({className: cx(toastClass), style: props.style || {},
onClick: this.handleOnClick,
onMouseEnter: this.handleMouseEnter,
onMouseLeave: this.handleMouseLeave},
this._render_close_button(props),
this._render_title_element(props),
this._render_message_element(props)
)
);
}
};

var jQuery = React.createClass(update(ToastMessageSpec, {
displayName: { $set: "ToastMessage.jQuery" },
mixins: { $set: [require("./jQueryMixin")] }
}));

/*
* assign default noop functions
*/
ToastMessageSpec.handleMouseEnter = noop;
ToastMessageSpec.handleMouseLeave = noop;
ToastMessageSpec.hideToast = noop;

var ToastMessage = module.exports = React.createClass(ToastMessageSpec);
ToastMessage.jQuery = jQuery;
82 changes: 82 additions & 0 deletions lib/ToastMessage/jQueryMixin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
function call_show_method ($node, props) {
$node[props.showMethod]({
duration: props.showDuration,
easing: props.showEasing
});
}

module.exports = {
getDefaultProps:function () {
return {
style: {
display: "none" // effective $.hide()
},
showMethod: "fadeIn", //, slideDown, and show are built into jQuery
showDuration: 300,
showEasing: "swing", // and linear are built into jQuery
hideMethod: "fadeOut",
hideDuration: 1000,
hideEasing: "swing",
//
timeOut: 5000,
extendedTimeOut: 1000
};
},

getInitialState:function () {
return {
intervalId: null,
isHiding: false
};
},

componentDidMount:function () {
var $__0= this,props=$__0.props;
call_show_method(this._get_$_node(), props);
if (props.timeOut > 0) {
this._set_interval_id(
setTimeout(this.hideToast, props.timeOut)
);
}
},

handleMouseEnter:function () {
clearTimeout(this.state.intervalId);
this._set_interval_id(null);

call_show_method(this._get_$_node().stop(true, true), this.props);
},

handleMouseLeave:function () {
var $__0= this,props=$__0.props;

if (!this.state.isHiding &&
(props.timeOut > 0 || props.extendedTimeOut > 0)) {
this._set_interval_id(
setTimeout(this.hideToast, props.extendedTimeOut)
);
}
},

hideToast:function (override) {
var $__0= this,state=$__0.state,props=$__0.props;
if (state.isHiding || (state.intervalId == null && !override)) return;
this.setState({isHiding: true});

this._get_$_node()[props.hideMethod]({
duration: props.hideDuration,
easing: props.hideEasing,
complete: this._handle_remove
});
},

_get_$_node:function () {
return $(this.getDOMNode());
},

_set_interval_id:function (intervalId) {
this.setState({
intervalId:intervalId
});
}
};
13 changes: 13 additions & 0 deletions lib/__tests__/index-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"use strict";
/** @jsx React.DOM */

jest.dontMock("../index.js");
describe("index", function() {
it("changes the text after click", function() {
var index = require("../index");

expect(index).toBeDefined();
expect(index.ToastContainer).toBeDefined();
expect(index.ToastMessage).toBeDefined();
});
});
3 changes: 3 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/** @jsx React.DOM */
exports.ToastContainer = require("./ToastContainer");
exports.ToastMessage = require("./ToastMessage");

0 comments on commit 44f406d

Please sign in to comment.