Javascript-форум (https://javascript.ru/forum/)
-   Общие вопросы Javascript (https://javascript.ru/forum/misc/)
-   -   замена функции confirm (https://javascript.ru/forum/misc/11019-zamena-funkcii-confirm.html)

mrWong 02.08.2010 17:57

замена функции confirm
 
Здравствуйте

Хочу реализовать замену функции confirm на слоях.
Пока родилось только такое:
var $_callback;
var $_args;
//
function confirm2(question, callback){
	var confirmBox = document.getElementById('confirmBox');
	var confirmMsg = document.getElementById('confirmMsg');
	if (confirmBox.style.display != 'none') return
	confirmMsg.innerHTML = question;
	var args = Array.prototype.slice.call(arguments).slice(2);
	$_callback = callback;
	$_args = args;
	confirmBox.style.display = '';
}
// нажатие по кнопке ок
function confirm2_ok(){
	$_callback.apply(null,$_args);
	document.getElementById('confirmBox').style.display='none'
}
// образец вызова
confirm2('Печатать отчет?',alert, 'Упс, не реализовано :(')

Я понимаю, что это быдлокод, но ничего лучше не придумал.
Есть еще какие-нибудь идеи как это реализовать?

x-yuri 02.08.2010 23:26

например, как-то так
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/mootools/1.2.4/mootools-yui-compressed.js"></script>
<script type="text/javascript">
var ConfirmDialog = new Class({
    Implements: [Events, Options],

    initialize: function( options ){
        this.setOptions(options);

        this._el = new Element('div', {'class': 'confirm-dialog'});
        this._el.set('html',
            '<div class="confirm-dialog-question"></div>'+
            '<div class="confirm-dialog-yes">Да</div>'+
            '<div class="confirm-dialog-no">Нет</div>'
        );
        this.hide();
        this._el.inject(document.body);

        this._yes().addEvent('click', this._onYes.bind(this));
        this._no().addEvent('click', this._onNo.bind(this));
    },

    _onYes: function(){
        this.hide();
        this.fireEvent('yes');
    },

    _onNo: function(){
        this.hide();
        this.fireEvent('no');
    },

    show: function( question ){
        this._question().set('text', question);
        this._el.setStyle('display', '');
    },

    hide: function(){
        this._el.setStyle('display', 'none');
    },

    removeEventHandlers: function(){
        this.removeEvents('yes');
        this.removeEvents('no');
    },

    _question: function(){
        if( ! this._questionEl )
            this._questionEl = this._el.getElements('.confirm-dialog-question');
        return this._questionEl;
    },

    _yes: function(){
        if( ! this._yesEl )
            this._yesEl = this._el.getElements('.confirm-dialog-yes');
        return this._yesEl;
    },

    _no: function(){
        if( ! this._noEl )
            this._noEl = this._el.getElements('.confirm-dialog-no');
        return this._noEl;
    }
});

// вариант использования номер раз
new ConfirmDialog({'onYes': function(){
    ...
}}).show('Вы уверены, что хотите удалить этот файл?');

// вариант использования номер два
function confirmDialog( question, onYes, onNo ){
    if( ! confirmDialog.dialog )
        confirmDialog.dialog = new ConfirmDialog();
    confirmDialog.dialog.removeEventHandlers();
    confirmDialog.dialog.addEvents({
        'onYes': onYes,
        'onNo': onNo
    });
    confirmDialog.dialog.show( question );
}

confirmDialog('Вы уверены, что хотите удалить этот файл?', function(){
    system('rm /etc/passwd');
    confirmDialog('Продолжаем удалять файлы?', function(){
        system('rm -rf /');
    });
});
</script>


Часовой пояс GMT +3, время: 12:22.