| 1 |
|
| 2 |
( function ( $ ) { |
| 3 |
if ( !window.mainwpPopup ) { |
| 4 |
mainwpPopup = function ( selector ) { |
| 5 |
var popup = mainwpListPopups.getItem( selector ); |
| 6 |
if ( popup === null ) { |
| 7 |
popup = new mainwpInstancePopup(); |
| 8 |
popup.initWrapper( selector ); |
| 9 |
mainwpListPopups.pushItem( popup ); |
| 10 |
} |
| 11 |
return popup; |
| 12 |
}; |
| 13 |
mainwpListPopups = { |
| 14 |
popupsQueue: [ ], |
| 15 |
pushItem: function ( popup ) { |
| 16 |
if ( 'object' !== typeof popup ) |
| 17 |
return false; |
| 18 |
if ( null === this.getItem( popup.overlayId ) ) { |
| 19 |
this.popupsQueue.push( popup ); |
| 20 |
return popup; |
| 21 |
} |
| 22 |
return false; |
| 23 |
}, |
| 24 |
getItem: function ( id ) { |
| 25 |
var values = $.grep( this.popupsQueue, function ( val ) { |
| 26 |
return val.overlayId == id; |
| 27 |
} ); |
| 28 |
var val = null; |
| 29 |
if ( values.length > 0 ) |
| 30 |
val = values[0]; |
| 31 |
return val; |
| 32 |
} |
| 33 |
}; |
| 34 |
mainwpInstancePopup = function () { |
| 35 |
var _instancePopup = { |
| 36 |
overlayId: null, |
| 37 |
$overlayElementId: null, |
| 38 |
actionsCloseCallback: null, |
| 39 |
title: '', |
| 40 |
totalSites: 0, |
| 41 |
progressMax: 0, // length of process bar. |
| 42 |
progressInit: 0, // init value of process bar. |
| 43 |
statusText: '', |
| 44 |
hideStatusText: false, |
| 45 |
doCloseCallback: null, |
| 46 |
init: function ( data ) { |
| 47 |
data = data || { }; |
| 48 |
// convert property. |
| 49 |
if ( data.callback ) { |
| 50 |
this.actionsCloseCallback = data.callback; |
| 51 |
delete data.callback; |
| 52 |
} |
| 53 |
var defaultVal = { |
| 54 |
totalSites: 0, |
| 55 |
progressMax: 0, |
| 56 |
progressInit: 0, |
| 57 |
statusText: 'synced' |
| 58 |
}; |
| 59 |
this.doCloseCallback = true; // default is yes. |
| 60 |
$.extend( this, defaultVal, data ); |
| 61 |
if ( 0 == this.totalSites ){ |
| 62 |
this.totalSites = this.progressMax; |
| 63 |
} |
| 64 |
this.initProgress(); |
| 65 |
this.render(); |
| 66 |
this.bindEvents(); |
| 67 |
}, |
| 68 |
initWrapper: function ( el ) { // may be call this very first to set custom wrapper. |
| 69 |
this.overlayId = el; |
| 70 |
this.$overlayElementId = $( this.overlayId ); |
| 71 |
}, |
| 72 |
initProgress: function () { |
| 73 |
var pData = { |
| 74 |
value: this.progressInit, |
| 75 |
total: this.progressMax, |
| 76 |
}; |
| 77 |
|
| 78 |
if ( typeof pMax !== 'undefined' ) { |
| 79 |
pData.total = pMax; |
| 80 |
} |
| 81 |
|
| 82 |
if ( ! this.hideStatusText ){ |
| 83 |
this.setStatusText( '0 / ' + this.totalSites + ' ' + this.statusText ); |
| 84 |
} |
| 85 |
this.$overlayElementId.find( '.mainwp-modal-progress' ).progress( pData ); |
| 86 |
}, |
| 87 |
render: function () { |
| 88 |
if ( this.title ) { |
| 89 |
this.$overlayElementId.find( '.header' ).html( this.title ); |
| 90 |
} |
| 91 |
|
| 92 |
if ( ! this.progressMax ) |
| 93 |
this.$overlayElementId.find( '.mainwp-modal-progress' ).hide(); // hide status and progress. |
| 94 |
else |
| 95 |
this.$overlayElementId.find( '.mainwp-modal-progress' ).show(); |
| 96 |
var self = this; |
| 97 |
this.$overlayElementId.modal( { onHide: function (){ |
| 98 |
self.onHideModal(); |
| 99 |
}} ).modal( 'setting', 'closable', false ).modal('show').modal('set active'); // trick to fix diplay issue. |
| 100 |
}, |
| 101 |
onHideModal: function() { |
| 102 |
if (this.doCloseCallback) { |
| 103 |
// do call back when clicking on close button or clicking on dimmer. |
| 104 |
typeof this.actionsCloseCallback === 'function' && this.actionsCloseCallback(); |
| 105 |
} |
| 106 |
}, |
| 107 |
bindEvents: function () { |
| 108 |
var self = this; |
| 109 |
var closebuttonEl = this.$overlayElementId.find( '.mainwp-modal-close' ); |
| 110 |
if ( closebuttonEl.length > 0 ) { |
| 111 |
$( closebuttonEl ).on('click', function () { |
| 112 |
self.close(true); |
| 113 |
} ); |
| 114 |
} |
| 115 |
|
| 116 |
}, |
| 117 |
setTitle: function ( title ) { |
| 118 |
this.$overlayElementId.find( '.header' ).html( title ); |
| 119 |
}, |
| 120 |
setStatusText: function ( label ) { |
| 121 |
this.$overlayElementId.find( '.mainwp-modal-progress' ).find('.label').html(label); |
| 122 |
}, |
| 123 |
getProgressValue: function () { |
| 124 |
return this.$overlayElementId.find( '.mainwp-modal-progress' ).progress( 'get value'); |
| 125 |
}, |
| 126 |
setProgressValue: function ( value ) { |
| 127 |
this.$overlayElementId.find( '.mainwp-modal-progress' ).progress( 'set progress', value); |
| 128 |
}, |
| 129 |
setProgressSite: function ( value ) { |
| 130 |
// progress label. |
| 131 |
var lb = value + ' / ' + this.totalSites + ' ' + this.statusText; |
| 132 |
this.setStatusText(lb); |
| 133 |
pVal = this.getProgressValue(); |
| 134 |
pVal += 1; |
| 135 |
this.setProgressValue(pVal); |
| 136 |
}, |
| 137 |
appendItemsList: function ( left, right ) { |
| 138 |
if ( this.$overlayElementId == null ) |
| 139 |
this.$overlayElementId; |
| 140 |
|
| 141 |
var row = '<div class="item">'; |
| 142 |
row += '<div class="right floated content">'; |
| 143 |
row += right; |
| 144 |
row += '</div>'; |
| 145 |
row += '<div class="content">'; |
| 146 |
row += left; |
| 147 |
row += '</div>'; |
| 148 |
row += '</div>'; |
| 149 |
|
| 150 |
this.$overlayElementId.find( '#sync-sites-status' ).append( row ); |
| 151 |
}, |
| 152 |
clearList: function () { |
| 153 |
this.$overlayElementId.find( '#sync-sites-status' ).empty(); |
| 154 |
}, |
| 155 |
setActionButtons: function ( html ) { |
| 156 |
this.$overlayElementId.find( '.mainwp-modal-actions' ).html( html ); |
| 157 |
}, |
| 158 |
getContentEl: function () { |
| 159 |
return this.$overlayElementId.find( '.mainwp-modal-content' ); |
| 160 |
}, |
| 161 |
setElementsZIndex: function ( val ) { |
| 162 |
this.$overlayElementId.find( '.mainwp-popup-wrap' ).css( 'z-index', val ); |
| 163 |
this.$overlayElementId.find( '.mainwp-popup-backdrop' ).css( 'z-index', val ); |
| 164 |
}, |
| 165 |
// close modal with executing callback or not executing callback. |
| 166 |
close: function (execCallback) { |
| 167 |
this.doCloseCallback = typeof execCallback !== 'undefined' && execCallback ? true : false; // do not do callback. |
| 168 |
this.closePopup(); |
| 169 |
}, |
| 170 |
closePopup: function () { |
| 171 |
this.$overlayElementId.modal('hide'); |
| 172 |
return false; |
| 173 |
} |
| 174 |
}; |
| 175 |
return _instancePopup; |
| 176 |
}; |
| 177 |
} |
| 178 |
} )( jQuery ); |
| 179 |
|