Core.UI = Core.UI || {}; Core.UI.LockWidget = class extends _core { /** * Get class name. */ static getClassName() { return 'Core.UI.LockWidget'; } /** * Check segment for lock widgets and initialize them. * @param {jQuery} $segment */ static init($segment) { $segment.find('.lock-widget').each(function () { new LockWidget($(this)); }); } /** * Initialize lock widget. * @param {jQuery} $item */ constructor($item) { super(); this.$item = $item; this.haveLock = Boolean($item.data('have-lock')); this.type = $item.data('type'); this.id = $item.data('id'); this.method = $item.data('method'); this.lockedBy = $item.find('.locked-by').html(); let me = this; $item.find('.action.button').actionButton({ 'take-lock': function () { me.takeLock(); } }); this.updateVisibility(); this.poll(); } /** * Update widget visibility; */ updateVisibility() { if (this.haveLock) { this.hide(); } else { this.show(); } this.$item.find('.locked-by').html(this.lockedBy.capitalize()); } /** * Hide widget from view. */ hide() { if (!this.$item.isVisible()) { return; } if (this.type == 'button') { this.$item.transition('slide up out'); } else { this.$item.hide(); } } /** * Show widget. */ show() { if (this.$item.isVisible()) { return; } if (this.type == 'button') { this.$item.transition('slide up in'); } else { this.$item.show(); } } /** * Check to see if we still have the lock. */ poll() { if (!this.$item.exists()) { return; } let me = this; Core.User.lockObject( this.method, this.id, false, function (locked, lockedBy) { me.haveLock = locked; me.lockedBy = lockedBy; me.updateVisibility(); setTimeout( function() { me.poll(); }, 5000 ); } ); } /** * Take the lock. */ takeLock() { let me = this; Core.User.lockObject( this.method, this.id, true, function (locked, lockedBy) { me.haveLock = locked; me.lockedBy = lockedBy; me.updateVisibility(); } ); } };