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 = $('
'); var ul = $(''); pagescontent.append(ul); var addli = $('
  • '); ul.append(addli); addli.append($('')); addli.append($('' + GetTextByKey("P_IPT_ADD", "Add") + '')); addli.click(function () { addTab(null, true); }); if (!templatereadonly && editable) addli.show(); else addli.hide(); ul.append($('
  • ')); 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 = $('
  • ').attr('data-href', tabName); if (tabIndex == 1) li.addClass('selected'); li.append($('').text(p.Name || GetTextByKey("P_IPT_UNNAMED", 'Unnamed'))); if (!templatereadonly && editable) { li.css('padding-right', 6); var btncopy = $(''); 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 = $(''); 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; });