Last active
January 30, 2016 01:11
-
-
Save Thanood/88123a68870e99bca580 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jQueryUiAdapter.prototype.openDialog = function (settings) { | |
var dfd = system.defer(); | |
var defaultSettings = { | |
appendTo: 'body', | |
dialogHost: 'dialogHost', | |
dialogPromise: dfd, | |
title: '', | |
cssClass: '', | |
viewmodel: null, | |
autoOpen: false, | |
modal: true, | |
height: ($(window).height() / 3) * 2,//'auto', | |
width: ($(window).width() / 3), //'auto', | |
validate: function (model) { return true; }, | |
buttons: [ | |
{ | |
text: 'Apply', | |
click: function () { | |
var validation = settings.validate(dialogModel); | |
if (validation === true) { | |
dfd.resolve(dialogModel); | |
$(this).removeClass(settings.cssClass); | |
$(this).dialog('close'); | |
} else { | |
$('.error', '#' + settings.dialogHost).removeClass('error'); | |
if (_.isArray(validation)) { | |
_.each(validation, function (selector) { | |
$(selector, '#' + settings.dialogHost).addClass('error'); | |
}); | |
} | |
} | |
} | |
}, | |
{ | |
text: 'Close', | |
click: function () { | |
dfd.resolve(); | |
$(this).removeClass(settings.cssClass); | |
$(this).dialog('close'); | |
} | |
} | |
] | |
}; | |
var settings = _.defaults(settings, defaultSettings); | |
var host = $('#' + settings.dialogHost); | |
if (host.length == 0) { | |
host = $('<div id="' + settings.dialogHost + '" title="' + settings.title + '"></div>').dialog(settings); | |
} else { | |
host.dialog('option', settings); | |
} | |
host.addClass(settings.cssClass); | |
var dialogModel = null; | |
composition.compose(host.get(0), { | |
model: settings.viewmodel, | |
binding: function (element, view, bindSettings) { | |
return system.defer(function (bindingDeferred) { | |
if (settings.data) { | |
if (_.isFunction(settings.data)) { | |
settings.data(bindSettings.model).then(function () { | |
dialogModel = bindSettings.model; | |
host.dialog('open'); | |
bindingDeferred.resolve(); | |
}); | |
} else { | |
_.each(settings.data, function (setting, key) { | |
var target = bindSettings.model[key]; | |
if (target && ko.isObservable(target)) { | |
bindSettings.model[key](setting); | |
} else { | |
bindSettings.model[key] = setting; | |
} | |
}); | |
dialogModel = bindSettings.model; | |
host.dialog('open'); | |
bindingDeferred.resolve(); | |
} | |
} else { | |
dialogModel = bindSettings.model; | |
host.dialog('open'); | |
} | |
}).promise(); | |
} | |
}); | |
return dfd.promise().then(function (_dialogModel) { | |
if (_dialogModel && _dialogModel.canDeactivate && _dialogModel.canDeactivate(true) && _dialogModel.deactivate) { | |
_dialogModel.deactivate(true); | |
} | |
return _dialogModel; | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment