define(function () { //define([], function () {//使用外部的$,才有Dialog,后期修改 var gs = {}; gs.title = 'Common'; gs.description = 'Common'; gs.version = '1.0'; (function () { 'use strict'; var iX, iY; function dragTitle(ev) { if (ev.target && ev.target.nodeName === 'EM') return false; var dialog = ev.data; window.draggingDialog = dialog; var o = getInnerOffset(dialog); iX = ev.clientX - o.left; iY = ev.clientY - o.top; $(document).mousemove(dragmove); $(document).mouseup(dragup); var _this = this; _this.setCapture && _this.setCapture(); var window_width = $(window).width(); var window_height = $(window).height(); var dragging_width = dialog.width(); var dragging_height = dialog.height(); function dragmove(e) { var e = e || window.event; var left, top; left = Math.max(Math.min((e.clientX - iX), window_width - dragging_width - 2), 0); top = Math.max(Math.min((e.clientY - iY), window_height - dragging_height - 2), 0); window.draggingDialog.css({ 'left': left, 'top': top }); }; function dragup(e) { $(document).unbind('mousemove', dragmove); $(document).unbind('mouseup', dragup); delete window.draggingDialog; _this.releaseCapture && _this.releaseCapture(); e.cancelBubble = true; }; return false; } $.fn.dialog = function (closefunc, removable) { this.children('.dialog-title').mousedown(this, dragTitle); this.find('.dialog-close').click(this, function (e) { if (removable) { e.data.remove(); } else { e.data.hideDialog(); } if (typeof closefunc === 'function') { closefunc(e); } }); this.find('input.dialog-close').keydown(resettab); var _this = this; function resettab(e) { if (e.keyCode == 9) { var input = _this.find("input:not(:disabled),select:not(:disabled)").eq(0); input.focus(); return false; } } this.css({ 'top': (document.documentElement.clientHeight - this.height()) / 2, 'left': (document.documentElement.clientWidth - this.width()) / 2 }); return this; }; $.fn.showDialog = function (selfmask) { //$('#mask_bg').height($(document).outerHeight(false)).width($(document).outerWidth(false)); if (this.attr("init") !== "1") { this.attr("init", "1"); var _this = this; if (selfmask !== false) { var mask = $('
'); this.mask = mask; this.before(mask); } $(window).resize(function () { //_this.height(_this.parent().outerHeight(false) - 64).width(_this.parent().outerWidth(false) - left - 2); if (_this.mask) _this.mask.height($(document).outerHeight(false) - 64).width($(document).outerWidth(false)); }); } if (this.mask) this.mask.show(); this.show(); var top = (document.documentElement.clientHeight - this.height()) / 2; if (top < 0) top = 0; var left = (document.documentElement.clientWidth - this.width()) / 2; if (left < 0) left = 0; this.css({ 'top': top, 'left': left }); var input = this.find("input:not(:disabled),select:not(:disabled)").eq(0); input.focus(); return this; }; $.fn.showDialogfixed = function (selfmask) { if (this.attr("init") !== "1") { this.attr("init", "1"); var _this = this; if (selfmask !== false) { var mask = $('
'); this.mask = mask; this.before(mask); } } var top = (document.documentElement.clientHeight - this.height()) / 3; if (top < 0) top = 0; var left = (document.documentElement.clientWidth - this.width()) / 2; if (left < 0) left = 0; this.css({ 'top': top, 'left': left }); if (this.mask) this.mask.show(); this.show(); var input = this.find("input:not(:disabled),select:not(:disabled)").eq(0); input.focus(); }; $.fn.hideDialog = function () { this.hide(); if (this.mask) this.mask.hide(); }; })(); gs.createDialog = function (title, content, onok, ispicture) { var d = $(''); var t = $('
'); t.find('.title').text(title); d.append(t); var c = $('
'); c.append(content); d.append(c); var funcs = $('
'); if (!ispicture) funcs.append($('')); if (!ispicture || (ispicture && !templatereadonly && editable)) funcs.append($('').click(onok)); funcs.append('
'); d.append(funcs); d.dialog(function () { d.hide(); }); $(document.body).append(d); return d; } gs.createMultiTextDialog = function (textbox, pageele) { if (pageele) { if (!pageele.IssueId || pageele.IssueId == "") editable = true; else editable = false; } var languages = [{ Key: 'fr-fr', Label: GetTextByKey("P_IPT_FRENCH", 'French') }, { Key: 'es-es', Label: GetTextByKey("P_IPT_SPANISH", 'Spanish') }]; var inputs = []; var d = $('
'); var c = $('
'); d.append(c); var tb = $('
'); c.append(tb); var tr = null; var input = null; for (var i = 0; i < languages.length; i++) { var l = languages[i]; tr = $(''); tb.append(tr); tr.append($('').text(l.Label)); input = $('').attr("tabindex", i + 1); if (textbox.attr("maxlength")) input.attr("maxlength", textbox.attr("maxlength")); if (templatereadonly || !editable) input.prop('disabled', true); inputs[l.Key] = input; var texts = textbox.data("texts") if (texts) { for (var j in texts) { var t = texts[j]; if (t.Key === l.Key) { input.val(t.Value); break; } } } tr.append($('').append(input)); } var funcs = $('
'); funcs.append($('').click(oncancel)); if (!templatereadonly && editable) funcs.append($('').click(onok)); funcs.append('
'); d.append(funcs); function onok() { var texts = []; for (var i in inputs) { var input = inputs[i]; texts.push({ Key: i, Value: input.val() }); } textbox.data("texts", texts); textbox.change(); d.remove(); } function oncancel() { d.remove(); } d.css({ 'top': textbox.offset().top + textbox.height() + 5, 'left': textbox.offset().left }); $(document.body).append(d); $(window).mousedown(function (e) { if ($(e.target).is(d)) return; else { var textdialog = $(e.target).parents(".textdialog"); if (textdialog.is(d)) return; } d.remove(); }); d.find('input.dialog-close').keydown(resettab); function resettab(e) { if (e.keyCode == 9) { var input = d.find("input:not(:disabled),select:not(:disabled)").eq(0); input.focus(); return false; } } return d; } gs.createImageDialog = function (obj, pageele) {//pageele should contain StaticPictures attr. var inputs = []; var tabindex = 0; var d = $('
'); var c = $('
'); d.append(c); var tb = $('
'); c.append(tb); var tr = null; var input = null; if (pageele && pageele.StaticPictures && pageele.StaticPictures.length > 0) { for (var i = 0; i < pageele.StaticPictures.length; i++) { var p = pageele.StaticPictures[i]; addpicturerow(p); } } else { addnopicrow(); } var nopictr = null; function addnopicrow() { nopictr = $(''); var td = $('No Picture'); nopictr.append(td); tb.append(nopictr); } function addpicturerow(p) { if (nopictr) { nopictr.remove(); nopictr = null; } tr = $(''); tb.append(tr); var td = $(''); var img = $('').attr('src', p.Url); td.append(img); tr.append(td); input = $('').attr("tabindex", ++tabindex); if (templatereadonly || !editable) input.prop('disabled', true); input.val(p.Name); input.data("pic", p); inputs.push(input); tr.append($('').append(input)); td = $(''); if (!templatereadonly && editable) { var del = $('').click(input, function (e) { showConfirm(GetTextByKey("P_IPT_DELETEPICTURECONFIRM", 'Are you sure you want to delete this picture?'), GetTextByKey("P_IPT_DELETEPICTURE", 'Delete Picture'), function () { var p = e.data.data("pic"); e.data.parent().parent().remove(); inputs.splice(inputs.indexOf(e.data), 1); //inspectionrequest("DeleteStaticPicture", p.ContentId, function (data) { //}, function (err) { //}); }); }); td.append(del) } tr.append(td); } function browsePicture() { var file = $(''); file.change(function () { var files = this.files; var file = files[0]; if (file.size == 0) { alert(GetTextByKey("P_MA_DOCUMENTTIPS", "Document size is 0kb, uploading failed.")); return false; } if (file.size > 1024 * 1024 * 2) { alert(GetTextByKey("P_MA_DOCUMENTTIPS1", "Document is too large. Maximum file size is 2MB.")); return false; } SavePicture(file); }).click(); } function SavePicture(file) { var formData = new FormData(); formData.append("pic", file); formData.append("MethodName", "UploadStaticPicture"); $.ajax({ url: 'Inspection.aspx', type: 'POST', dataType: 'json', processData: false, contentType: false, data: formData, async: true, success: function (data) { if (data) { var p = {}; p.ContentId = data.Item1; p.Url = data.Item2; p.Name = file.name; p.DataType = file.name.substring(file.name.lastIndexOf(".")); addpicturerow(p); } }, error: function (err) { } }); } if (!templatereadonly && editable) { var addpicdiv = $('
'); var addpicicon = $('Add Picture').click(function () { browsePicture(); }); addpicdiv.append(addpicicon); var addtemppicicon = $('Add Template Picture').click(function () { //browsePicture(); }); addpicdiv.append(addtemppicicon); c.append(addpicdiv); } var funcs = $('
'); funcs.append($('').click(onok)); funcs.append('
'); d.append(funcs); function onok() { if (!templatereadonly && editable) { var pics = []; for (var i in inputs) { var input = inputs[i]; var p = input.data("pic"); p.Name = input.val(); pics.push(p); } pageele.StaticPictures = pics; } d.remove(); } d.css({ 'top': obj.offset().top + obj.height() + 5, 'left': obj.offset().left }); $(document.body).append(d); $(window).mousedown(function (e) { if ($(e.target).is(d)) return; else { var textdialog = $(e.target).parents(".textdialog"); if (textdialog.is(d)) return; } d.remove(); }); d.find('input.dialog-close').keydown(resettab); function resettab(e) { if (e.keyCode == 9) { var input = d.find("input:not(:disabled),select:not(:disabled)").eq(0); input.focus(); return false; } } return d; } return gs; });