Skip to content

Instantly share code, notes, and snippets.

@finagin
Created April 11, 2018 08:33
Show Gist options
  • Save finagin/4dfbd3ad860a105cc5e1c7a3089e7ca3 to your computer and use it in GitHub Desktop.
Save finagin/4dfbd3ad860a105cc5e1c7a3089e7ca3 to your computer and use it in GitHub Desktop.
Shelf input value
(function ($) {
function Shelf(value) {
var _value = value;
this.getValue = function () {
return _value;
};
this.setValue = function (value) {
_value = value;
};
}
var plugin = "shelf",
methods = {
hide: function (replacement) {
return this.each(function () {
var $this = $(this),
repl = new Shelf(replacement || ""),
event = $.Event("hide.shelf");
$this
.trigger(event, [repl]);
if (!event.isDefaultPrevented()) {
$this
.data($.fn[plugin].dataField, $this.val())
.val(repl.getValue())
.trigger("hidden.shelf");
}
});
},
show: function () {
return this.each(function () {
var $this = $(this),
shelf = new Shelf($this.data($.fn[plugin].dataField) || ""),
event = $.Event("show.shelf");
$this
.trigger(event, [shelf]);
if (!event.isDefaultPrevented()) {
$this
.removeData($.fn[plugin].dataField)
.val(shelf.getValue())
.trigger("shown.shelf", []);
}
});
}
};
$.fn[plugin] = function (method) {
if (methods[method]) {
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
} else {
$.error("Method " + method + " does not exist on jQuery." + plugin);
}
};
$.fn[plugin].dataField = plugin;
})(window.jQuery || window.Zepto);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment