Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix destroy method for prevent memory leaks #422

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 124 additions & 0 deletions dist/destory.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<!doctype html>
<html lang="en">
<head>
<title>Persian Datepicker</title>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">


<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
<meta name="HandheldFriendly" content="true"/>
<!-- Demo Bootstrap Style -->
<!-- ------------------------------------------------------------------------------------ -->
<!--<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>-->
<link rel="stylesheet" href="../bower_components/bootstrap/dist/css/bootstrap.min.css"/>

<!-- Demo Font Embed -->
<!-- ------------------------------------------------------------------------------------ -->
<link href="../assets/css/fontiran.css" rel="stylesheet"/>

<!-- Persian Datepicker Style -->
<!-- ------------------------------------------------------------------------------------ -->
<link id="datepickerTheme" href="./css/persian-datepicker.css" rel="stylesheet"/>

<!-- Theme Switcher -->
<!-- ------------------------------------------------------------------------------------ -->
<script type="text/javascript">
function swapStyleSheet(sheet) {
document.getElementById('datepickerTheme').setAttribute('href', sheet);
}
</script>

<!-- Demo Style -->
<!-- ------------------------------------------------------------------------------------ -->
<style type="text/css">
*,
body {
font-family: IRANSans, tahoma;
}

body {
padding-bottom: 300px;
}

.input-group-addon {
cursor: pointer;
}

.navbar-right {
padding: 10px;
}
</style>
</head>
<body>

<nav class="navbar">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" href="#">
Persian Date Picker <span class="badge">Version 1.1.3</span>
</a>
</div>
<nav class="navbar-right">
<span>
theme:
</span>
<button class="btn btn-xs btn-primary"
onclick="swapStyleSheet('css/persian-datepicker.css')">
Default
</button>
<button class="btn btn-xs btn-primary"
onclick="swapStyleSheet('css/theme/persian-datepicker-blue.min.css')">
Blue
</button>
<button class="btn btn-xs btn-primary"
onclick="swapStyleSheet('css/theme/persian-datepicker-cheerup.min.css')">
Cheerup
</button>
<button class="btn btn-xs btn-primary"
onclick="swapStyleSheet('css/theme/persian-datepicker-dark.min.css')">
Dark
</button>
<button class="btn btn-xs btn-primary"
onclick="swapStyleSheet('css/theme/persian-datepicker-redblack.min.css')">
Redblack
</button>
</nav><!--/.nav-collapse -->
</div>
</nav>

<div class="container">
<div class="row">
<div class="col-xs-12">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Destroy a Datepicker</h3>
</div>
<div class="panel-body">
<div class="form-group">
<input id="input1" class="form-control"
value="2019-02-19T14:15:16" />
<button type="button" onclick="destoryTest()">delete</button>
</div>
</div>
</div>
</div>
</div>
</div>
<script src="../node_modules/jquery/dist/jquery.min.js"></script>
<script src="../assets/persian-date.min.js"></script>
<script src="./js/persian-datepicker.js"></script>
<script type="text/javascript">
$(document).ready(function () {
window.persianDatepickerDebug = true;
window.pd = $('#input1').persianDatepicker();
});

function destoryTest() {
window.pd.destroy()
}
</script>
</body>
</html>
37 changes: 28 additions & 9 deletions dist/js/persian-datepicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,24 @@ var API = function () {
key: 'destroy',
value: function destroy() {
if (this.model) {
// hide openned popup
this.model.api.hide();

// deattach events handler
var ElemID = this.model.view.id;
$(this.model.inputElement).off();
$('#' + ElemID).off();
$(document).off('click', '#' + ElemID + ' .pwt-btn-today');
$(document).off('click', '#' + ElemID + ' .pwt-btn-calendar');
$(document).off('click', '#' + ElemID + ' .pwt-btn-submit');
$(document).off('click', '#' + ElemID + ' .pwt-btn');
$(document).off('click', '#' + ElemID + ' .up-btn');
$(document).off('click', '#' + ElemID + ' .down-btn');
$(document).off('click', '#' + ElemID + ' .datepicker-day-view td:not(.disabled)');
$(document).off('click', '#' + ElemID + ' .datepicker-month-view .month-item:not(.month-item-disable)');
$(document).off('click', '#' + ElemID + ' .datepicker-year-view .year-item:not(.year-item-disable)');
$('body').off('click', this.model.input._closePickerHandler);

this.model.view.destroy();
this.model.options.onDestroy(this.model);
delete this.model;
Expand Down Expand Up @@ -1288,15 +1306,15 @@ var Config = {
<table cellspacing="0" class="table-days">
<tbody>
{{#days.list}}
<tr>
<tr>
{{#.}}
{{#enabled}}
{{#enabled}}
<td data-unix="{{dataUnix}}" ><span class="{{#otherMonth}}other-month{{/otherMonth}} {{#selected}}selected{{/selected}}">{{title}}</span></td>
{{/enabled}}
{{^enabled}}
<td data-unix="{{dataUnix}}" class="disabled"><span class="{{#otherMonth}}other-month{{/otherMonth}}">{{title}}</span></td>
{{/enabled}}
{{/.}}
{{/.}}
</tr>
{{/days.list}}
</tbody>
Expand All @@ -1305,7 +1323,7 @@ var Config = {
</div>
{{/days.viewMode}}
{{/days.enabled}}
{{#month.enabled}}
{{#month.enabled}}
{{#month.viewMode}}
<div class="datepicker-month-view">
{{#month.list}}
Expand All @@ -1319,7 +1337,7 @@ var Config = {
</div>
{{/month.viewMode}}
{{/month.enabled}}
{{#year.enabled }}
{{#year.enabled }}
{{#year.viewMode }}
<div class="datepicker-year-view" >
{{#year.list}}
Expand All @@ -1333,7 +1351,7 @@ var Config = {
</div>
{{/year.viewMode }}
{{/year.enabled }}
</div>
</div>
{{#time}}
{{#enabled}}
<div class="datepicker-time-view">
Expand Down Expand Up @@ -1372,7 +1390,7 @@ var Config = {
</div>
{{/enabled}}
{{/time}}
{{#toolbox}}
{{#toolbox}}
{{#enabled}}
<div class="toolbox ">
<div class="btn-today">{{text.btnToday}}</div>
Expand Down Expand Up @@ -1597,14 +1615,15 @@ var Input = function () {
var closePickerHandler = function closePickerHandler(e) {
if (!$(e.target).is(that.elem) && !$(e.target).is(that.model.view.$container) && $(e.target).closest('#' + that.model.view.$container.attr('id')).length == 0 && !$(e.target).is($(that.elem).children())) {
that.model.api.hide();
$('body').unbind('click', closePickerHandler);
$('body').unbind('click', that._closePickerHandler);
}
};
that._closePickerHandler = closePickerHandler;

$(this.elem).on('focus click', Helper.debounce(function (evt) {
that.model.api.show();
if (that.model.state.ui.isInline === false) {
$('body').unbind('click', closePickerHandler).bind('click', closePickerHandler);
$('body').unbind('click', that._closePickerHandler).bind('click', that._closePickerHandler);
}
if (Helper.isMobile) {
$(this).blur();
Expand Down
4 changes: 2 additions & 2 deletions dist/js/persian-datepicker.min.js

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions src/es6/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,24 @@ class API {
*/
destroy () {
if(this.model){
// hide openned popup
this.model.api.hide();

// deattach eventHandlers
const ElemID = this.model.view.id;
$(this.model.inputElement).off();
$('#' + ElemID).off();
$(document).off('click', '#' + ElemID + ' .pwt-btn-today');
$(document).off('click', '#' + ElemID + ' .pwt-btn-calendar');
$(document).off('click', '#' + ElemID + ' .pwt-btn-submit');
$(document).off('click', '#' + ElemID + ' .pwt-btn');
$(document).off('click', '#' + ElemID + ' .up-btn');
$(document).off('click', '#' + ElemID + ' .down-btn');
$(document).off('click', '#' + ElemID + ' .datepicker-day-view td:not(.disabled)');
$(document).off('click', '#' + ElemID + ' .datepicker-month-view .month-item:not(.month-item-disable)');
$(document).off('click', '#' + ElemID + ' .datepicker-year-view .year-item:not(.year-item-disable)');
$('body').off('click', this.model.input._closePickerHandler);

this.model.view.destroy();
this.model.options.onDestroy(this.model);
delete this.model;
Expand Down
5 changes: 3 additions & 2 deletions src/es6/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,15 @@ class Input {
if (!$(e.target).is(that.elem) && !$(e.target).is(that.model.view.$container) &&
$(e.target).closest('#' + that.model.view.$container.attr('id')).length == 0 && !$(e.target).is($(that.elem).children())) {
that.model.api.hide();
$('body').unbind('click', closePickerHandler);
$('body').unbind('click', that._closePickerHandler);
}
};
that._closePickerHandler = closePickerHandler;

$(this.elem).on('focus click', Helper.debounce(function (evt) {
that.model.api.show();
if (that.model.state.ui.isInline === false) {
$('body').unbind('click', closePickerHandler).bind('click', closePickerHandler);
$('body').unbind('click', that._closePickerHandler).bind('click', that._closePickerHandler);
}
if (Helper.isMobile) {
$(this).blur();
Expand Down