2023-04-28 12:22:26 +08:00

164 lines
6.1 KiB
JavaScript

define(['modules/templates/page'], function (Page) {
var q = function (pages) {
this.pages = pages || [];
this.pageModules = [];
};
q.prototype.description = "Pages Module";
q.prototype.version = "1.0.0.0";
q.prototype.createContent = function () {
var _this = this;
var pagescontent = $('<div></div>');
var ul = $('<ul class="tab_header" style="padding-top: 5px;"></ul>');
pagescontent.append(ul);
var addli = $('<li data-disabled="1" data-href="add"></li>');
ul.append(addli);
addli.append($('<span class="spanbtn iconadd"></span>'));
addli.append($('<span>' + GetTextByKey("P_IPT_ADD", "Add") + '</span>'));
addli.click(function () {
addTab(null, true);
});
if (!templatereadonly && editable)
addli.show();
else
addli.hide();
ul.append($('<li style="clear: both;"></li>'));
var tabIndex = 0;
for (var i = 0; i < this.pages.length; i++) {
var p = this.pages[i];
addTab(p, false);
}
pagescontent.tab();//tab()»áÇåµôaddliµÄclickʼþ
function addTab(p, newadd) {
if (!p) {
p = {};
_this.pages.push(p);
}
var tabName = 'tab_' + tabIndex++;
var pm = new Page(p, _this);
_this.pageModules.push(pm);
var pagecontent = pm.createContent().attr('data-page', tabName);
var li = $('<li></li>').attr('data-href', tabName);
if (tabIndex == 1)
li.addClass('selected');
li.append($('<span></span>').text(p.Name || GetTextByKey("P_IPT_UNNAMED", 'Unnamed')));
if (!templatereadonly && editable) {
li.css('padding-right', 6);
var btncopy = $('<span class="spanbtn iconcopy" style="margin-left:10px;padding-left:3px;padding-right:4px;"></span>');
btncopy.click(function () {
var item = copyPage(pm.getPageValue(true, true));
_this.pages.push(item);
addTab(item, true);
return false;
}).attr('title', GetTextByKey("P_IPT_COPYPAGE", 'Copy Page'));
li.append(btncopy);
var btndelete = $('<span class="spanbtn icondelete" style="padding-left:3px;"></span>');
btndelete.click(function () {
showConfirm(GetTextByKey("P_IPT_AREYOUSUREYOUWANTTOREMOVETHISPAGE", 'Are you sure you want to remove this page?'), GetTextByKey("P_IPT_REMOVEPAGE", 'Remove Page'), function () {
var next = li.next();
if (next.is(addli))
next = li.prev();
next.click();
li.remove();
pagescontent.children('div[data-page="' + tabName + '"]').remove();
_this.pages.splice(_this.pages.indexOf(p), 1);
_this.pageModules.splice(_this.pageModules.indexOf(pm), 1);
});
}).attr('title', GetTextByKey("P_IPT_DELETEPAGE", 'Delete Page'));
li.append(btndelete);
pm.onNameChanged = function (name) {
if (name && name != '')
li.children(":first").text(name);
else
li.children(":first").text(GetTextByKey("P_IPT_UNNAMED", 'Unnamed'));
}
}
else
addli.hide();
binddrag(li, p);
addli.before(li);
if (newadd) {
pagescontent.tab();
li.click();
}
pagescontent.append(pagecontent);
}
var draggingtab = null;
var draggingpage = null;
function binddrag(li, p) {
li.attr('draggable', true);
li.bind('dragstart', function (e) {
draggingtab = $(this);
draggingpage = p;
});
li.bind('dragend', function (e) {
draggingtab = null;
draggingpage = null;
});
li.bind('dragover', function (e) {
e.originalEvent.preventDefault()
});
li.bind('drop', function (e) {
var t = $(this);
if (t.is(draggingtab))
return;
var t = $(this);
var after = e.originalEvent.clientX > t.offset().left + t.width() / 2;
if (after) {
t.after(draggingtab);
_this.pages.splice(_this.pages.indexOf(draggingpage), 1);
_this.pages.splice(_this.pages.indexOf(p) + 1, 0, draggingpage);
}
else {
t.before(draggingtab);
_this.pages.splice(_this.pages.indexOf(draggingpage), 1);
_this.pages.splice(_this.pages.indexOf(p), 0, draggingpage);
}
});
}
return pagescontent;
};
q.prototype.getPagesValue = function () {
for (var i = 0; i < this.pageModules.length; i++) {
var p = this.pageModules[i].getPageValue();//getPageValueÖ±½ÓÊÕ¼¯Êý¾Ýµ½ÒýÓöÔÏó
if (!p) return false;
}
return this.pages;
}
function copyPage(p) {
var p = JSON.parse(JSON.stringify(p));
p.Id = "";
if (p.Sections) {
for (var i = p.Sections.length - 1; i >= 0; i--) {
var s = p.Sections[i];
if (s.IsLink)
p.Sections.splice(i, 1);
}
for (var i = 0; i < p.Sections.length; i++) {
var s = p.Sections[i];
s.Id = "";
if (s.Questions) {
for (var j = 0; j < s.Questions.length; j++) {
var q = s.Questions[j];
q.Id = "";
}
}
}
}
return p;
}
return q;
});