// Implementation of various link editing dialogs K. Morris Feb. 2010 // ---- link - dialog for web links FIRSTCLASS.util.Message.parseSelectedLink = function(dialog) { var editor = dialog.getParentEditor(), selection = editor.getSelection(), ranges = selection.getRanges(), contents = ranges[0].cloneContents(), element = null, text = false, retval = {}; if (ranges.length == 1) { var rangeRoot = ranges[0].getCommonAncestor( true ); element = rangeRoot.getAscendant( 'a', true ); if (element && element.getAttribute('href')) { selection.selectElement(element); } else if ((element = rangeRoot.getAscendant( 'img', true )) && element.getAttribute('_cke_real_element_type') && element.getAttribute('_cke_real_element_type') == 'anchor') { dialog.fakeObj = element; element = editor.restoreRealElement(dialog.fakeObj); selection.selectElement(dialog.fakeObj); } else { element = null; if (contents && contents.$) { if (contents.$.textContent) { text = contents.$.textContent; } else if (contents.$.innerText) { text = contents.$.innerText; } } } } retval.url = element ? ( element.getAttribute( '_cke_saved_href' ) || element.getAttribute( 'href' ) ) : ''; if (element && element.innerHTML) { retval.linktext = (element && element.innerHTML); } else if (text) { retval.linktext = text; } return retval; }; FIRSTCLASS.util.Message.commitLink = function(dialog, attributes, linktext) { var editor = dialog.getParentEditor(), selection = editor.getSelection(), ranges = selection.getRanges(), link = false; if (!dialog._.selectedElement) { // Create element if (ranges.length == 1) { link = editor.document.createElement('a'); link.setAttributes(attributes); if (ranges[0].collapsed) { link.setHtml(linktext); } else { var content = ranges[0].extractContents(); content.appendTo(link); } ranges[0].insertNode(link); var br = editor.document.createElement('br'); br.insertAfter(link); ranges[0].selectNodeContents(link); selection.selectRanges(ranges); } } else { // We're only editing an existing link, so just overwrite the attributes. link = dialog._.selectedElement; link.setAttributes(attributes); delete dialog._.selectedElement; } return link; }; CKEDITOR.dialog.add( 'link', function(editor) { return { title : FIRSTCLASS.locale.editor.weblink.title, minWidth : 350, minHeight : 75, contents : [ { id : 'main', elements : [ { type : 'text', id : 'url', label : editor.lang.common.url, labelLayout: 'horizontal', widths: [ '20%', '80%' ], isChanged: false, validate: function() { var func = CKEDITOR.dialog.validate.notEmpty( editor.lang.link.noUrl ); return func.apply(this); }, setup: function(data) { if (data.url) { this.setValue(data.url); } this.select(); }, commit: function(data) { data.url = this.getValue(); } }, { type: 'text', id: 'linktext', label: FIRSTCLASS.locale.editor.weblink.text, labelLayout: 'horizontal', widths: [ '20%', '80%' ], isChanged: false, validate: function() { return true; }, setup: function(data) { if (data.linktext) { this.setValue(data.linktext); } }, commit: function(data) { data.linktext = this.getValue(); } } ] } ], onShow : function() { this.fakeObj = false; var data = FIRSTCLASS.util.Message.parseSelectedLink(this); if (!data.linktext) { data.linktext = FIRSTCLASS.locale.editor.weblink.linktext; } this.setupContent(data); }, onOk : function() { var attributes = { href : '/*' + CKEDITOR.tools.getNextNumber() + '*/' }, removeAttributes = [], data = { href : attributes.href }, me = this, editor = this.getParentEditor(); // grab the field data this.commitContent(data); // Compose the URL. var protocol = 'http://', url = data.url; attributes._cke_saved_href = (url.indexOf('://') >= 0 ) ? url : protocol + url; attributes.target = '_blank'; var linktext = data.linktext && data.linktext.length > 0 ? data.linktext : data.url; FIRSTCLASS.util.Message.commitLink(this, attributes, linktext); }, onLoad : function() { } }; }); // ---- itemlink - common function for dialogs to link to OTSM leaf items // // config: // title - dialog title // objtype - type of item to link with // objtitle - title for object picker // var itemlink = function(editor, config) { // clear item list var clearItemList = function(dialog) { var itemList = dialog.getContentElement('main', 'item'); if (itemList) { itemList.clear(); } if (dialog._dataSource && dialog._dataSource !== null) { dialog._dataSource.dispose(); dialog._dataSource = null; } }; // fill item list from passed-in row in desktop ds var fillItemList = function(dialog, row) { // ds config var dsconfig = { containerBaseUrl: FIRSTCLASS.session.baseURL + row.uri, autoLoad: false, watchForNew: false, watchForTinkle: false, messagebodies: false, fetchThreads: false, suppressCatchup: true }; var isObjTypeMatch = function(row, objtype) { var isMatch = row.typedef.objtype == objtype; switch (objtype) { case FIRSTCLASS.objTypes.message: case FIRSTCLASS.objTypes.confitem: isMatch = (row.typedef.objtype == FIRSTCLASS.objTypes.message) ||(row.typedef.objtype == FIRSTCLASS.objTypes.confitem); break; default: break; } return isMatch; }; // create data source dialog._dataSource = new FIRSTCLASS.util.DataSource(dsconfig); // add listener to fill the list dialog._dataSource.addRowListener({ fillFinished: function() { var names = []; var recs = []; var itemName; var keep; if (dialog._dataSource && dialog._dataSource._data && dialog._dataSource._data.records) { recs = dialog._dataSource._data.records; } for (var row in recs) { if (recs[row].name) { keep = true; itemName = recs[row].name; if (typeof recs[row].name === undefined) { keep = false; } if (recs[row].status.backversion !== 0) { keep = false; } if (recs[row].status.deleted) { keep = false; } if (config.objtype == FIRSTCLASS.objTypes.file) { switch(recs[row].typedef.objtype) { case FIRSTCLASS.objTypes.file: break; case FIRSTCLASS.objTypes.odocument: if (recs[row].lformid !== 20503 && !recs[row].linkinfo) { keep = false; } break; default: keep = false; } if (recs[row].name.indexOf(".tmp") != -1) { keep = false; } } else if (config.objtype == FIRSTCLASS.objTypes.odocument && isObjTypeMatch(recs[row], config.objtype)) { if (recs[row].linkinfo) { keep = false; } if (!recs[row].col8082) { keep = false; } if (recs[row].lformid === 20503) { keep = false; } } else if (config.objtype == FIRSTCLASS.objTypes.confitem && isObjTypeMatch(recs[row], config.objtype)) { var threadStrs = recs[row].threadid.split('-'); var threadNums = [+('0x'+threadStrs[0]),+('0x'+threadStrs[1])]; var headMsgId = '[' + threadNums[0] + ':' + threadNums[1] + ']'; if (recs[row].messageid == headMsgId) { itemName = recs[row].subject; } else { keep = false; } } else { keep = false; } if (keep) { names.push(itemName); } } } names.sort(); // remove duplicates for (var i in names) { if (i > 0) { while (names[i] == names[i-1]) { names.splice(i,1); if (i >= names.length) { break; } } } } // fill the list var list = dialog.getContentElement('main', 'item'); if (list) { list.clear(); for (i = 0; i < names.length; i++) { list.add(names[i]); } } } }); // activate and fetch rows dialog._dataSource.activate(); var oTypes = config.objtype; if (config.objType == FIRSTCLASS.objTypes.file) { oTypes = [FIRSTCLASS.objTypes.file, FIRSTCLASS.objTypes.odocument]; } if (config.objType == FIRSTCLASS.objTypes.confitem) { oTypes = [FIRSTCLASS.objTypes.message, FIRSTCLASS.objTypes.confitem]; } dialog._dataSource.fetchRowsByObjType(oTypes, false); }; return { title : config.title || 'Insert Item Link', minWidth: 350, maxWidth: 350, minHeight: 155, maxHeight: 155, contents : [ { id : 'main', elements : [ { type : 'select', id : 'community', label : FIRSTCLASS.locale.editor.itemlink.community, labelLayout: 'horizontal', widths: [ '20%', '80%' ], 'default': '', style: 'width : 100%;', items: [ [ FIRSTCLASS.locale.editor.itemlink.nullitem, ''] ], isChanged: false, validate: function() { var func = CKEDITOR.dialog.validate.notEmpty(FIRSTCLASS.locale.editor.itemlink.listloading); return func.apply(this); }, setup: function(data) { if (data.community) { this.setValue(data.community); } }, commit: function(data) { data.community = this.getValue(); } }, { type: 'select', id: 'item', label: config.objtitle || FIRSTCLASS.locale.editor.itemlink.objtitle, labelLayout: 'horizontal', widths: [ '20%', '80%' ], 'default' : '', 'size': '7', style: 'width: 100%;', items : [ [ FIRSTCLASS.locale.editor.itemlink.listloading, ''] ], isChanged: false, validate: function() { var func = CKEDITOR.dialog.validate.notEmpty(FIRSTCLASS.locale.editor.itemlink.noitem); return func.apply(this); }, setup: function(data) { if (data.item) { this.setValue(data.item); } }, commit: function(data) { data.item = this.getValue(); } } ] } ], onShow : function() { this.fakeObj = false; // get link object if any var editor = this.getParentEditor(), selection = editor.getSelection(), ranges = selection.getRanges(), element = null, me = this; if (ranges.length == 1) { var rangeRoot = ranges[0].getCommonAncestor( true ); element = rangeRoot.getAscendant( 'a', true ); if (element && element.getAttribute('href')) { selection.selectElement(element); } else if ((element = rangeRoot.getAscendant( 'img', true )) && element.getAttribute('_cke_real_element_type') && element.getAttribute('_cke_real_element_type') == 'anchor') { this.fakeObj = element; element = editor.restoreRealElement(this.fakeObj); selection.selectElement(this.fakeObj); } else { element = null; } } var data = FIRSTCLASS.util.Message.parseSelectedLink(this); data.community = config.linkcomm || ''; data.item = config.linkitem || ''; // get list of communities from desktop var communities = FIRSTCLASS.session.desktop._dataSource._data.records, commElement = this.getContentElement('main', 'community'), options = []; // fill in the community selector if (commElement) { commElement.clear(); for (var comm in communities) { if (communities[comm].name && FIRSTCLASS.objTypes.isContainer(communities[comm].typedef.objtype)) { commElement.add(communities[comm].name); } } } // fill in item selector from current community clearItemList(this); var commUri = FIRSTCLASS.lang.removeSlashUrl(FIRSTCLASS.session._activeObject); commUri = commUri.slice(commUri.lastIndexOf('/') + 1); var rows = FIRSTCLASS.session.desktop._dataSource.query("uri", commUri, true); if (rows && rows.length > 0) { fillItemList(this, rows[0][1]); data.community = rows[0][1].name; if (!config.currcomm) { config.currcomm = data.community; } } this.setupContent(data); // add change handler for community selector YAHOO.util.Event.on(commElement.getInputElement().$,'change', function(o) { var name = this.getValue(), rows = FIRSTCLASS.session.desktop._dataSource.query("name", name, true), dlg = this.getDialog(); if (rows && rows.length > 0) { var dsr = rows[0][1]; // fill the item list from selected row clearItemList(dlg); fillItemList(dlg, dsr); } }, commElement, true); }, onOk : function() { var attributes = { href : '/*' + CKEDITOR.tools.getNextNumber() + '*/' }, removeAttributes = [], data = { href : attributes.href }, me = this, editor = this.getParentEditor(); // grab the field data this.commitContent(data); // Compose the URL if (data.item.length > 0 && this._dataSource && this._dataSource !== null) { var column = config.objtype == FIRSTCLASS.objTypes.confitem ? 'subject' : 'name'; var rows = this._dataSource.query(column, data.item, true); if (rows && rows.length > 0) { attributes._cke_saved_href = this._dataSource.getPermalink(rows[0][1]); attributes.href = this._dataSource.getPermalink(rows[0][1]); } } // link text is community:name or just name var selected = FIRSTCLASS.util.Message.parseSelectedLink(this); var linktext = selected.linktext; if (data.item && !linktext) { linktext = data.item.length > 0 ? data.item : linktext; if (config.currcomm && data.community.length > 0 && data.community !== config.currcomm) { // add community prefix linktext = data.community + ' : ' + linktext; } } FIRSTCLASS.util.Message.commitLink(this, attributes, linktext); }, onLoad : function() { } }; }; // ---- wikilink - dialog to link to wiki pages in any subscribed community CKEDITOR.dialog.add('wikilink', function(editor) { return itemlink(editor, { title: FIRSTCLASS.locale.editor.itemlink.wikititle, objtype: FIRSTCLASS.objTypes.odocument, objtitle: FIRSTCLASS.locale.editor.itemlink.wikiobj }); }); // ---- doclink - dialog to link to documents in any subscribed community CKEDITOR.dialog.add('doclink', function(editor) { return itemlink(editor, { title: FIRSTCLASS.locale.editor.itemlink.doctitle, objtype: FIRSTCLASS.objTypes.file, objtitle: FIRSTCLASS.locale.editor.itemlink.docobj }); }); // ---- threadlink - dialog to link to discussions in any subscribed community CKEDITOR.dialog.add('threadlink', function(editor) { return itemlink(editor, { title: FIRSTCLASS.locale.editor.itemlink.threadtitle, objtype: FIRSTCLASS.objTypes.confitem, objtitle: FIRSTCLASS.locale.editor.itemlink.threadobj }); }); // ---- dirlink - common function to implement dialog for links to directory items // // config: // objtype: one of FIRSTCLASS.objTypes // title: string dialog title // srchtitle: guide text for search string input // picktitle: guide text for pick list var dirlink = function(editor,config) { // clear item list var clearEntries = function(dialog) { var list = dialog.getContentElement('main', 'match'); if (list) { list.clear(); } }; // fill item list from _matches array var fillItemList = function(dialog) { // fill the list var list = dialog.getContentElement('main', 'match'); if (list) { list.clear(); for (var item in dialog._matches) { if (dialog._matches[item].cid) { list.add(dialog._matches[item].name); } } } }; // do directory lookup var doLookup = function(dialog) { var msg = dialog.getParentEditor().message; var options = 0; if (config.objtype == FIRSTCLASS.objTypes.user) { options += 12; } else if (config.objtype == FIRSTCLASS.objTypes.conference) { options += 2; } var srchStr = dialog.getContentElement('main','search').getValue(); if (msg && srchStr) { clearEntries(dialog); var list = dialog.getContentElement('main','match'); if (list) { list.add(FIRSTCLASS.locale.editor.dirlink.search); } // strip opcode from URL if any var baseURL = msg._config.baseURL; if (baseURL.indexOf('__') >= 0) { baseURL = baseURL.slice(0,baseURL.indexOf('__')); } FIRSTCLASS.util.Form.validateName(baseURL, srchStr, 4, 1000, { onComplete: function(obj) { dialog._matches = []; switch(obj.match) { case "multi": if (obj.dataSource && obj.dataSource._data) { var recs = obj.dataSource._data.records; for (var row in recs) { if (recs[row].name) { dialog._matches[recs[row].name] = { name: recs[row].name, cid: recs[row].cid }; } } } break; case "single": dialog._matches[obj.item.name] = { name: obj.item.name, cid: obj.item.cid }; break; default: alert(FIRSTCLASS.locale.editor.dirlink.nomatch); } fillItemList(dialog); }, onFailure: function(obj) { alert(FIRSTCLASS.locale.editor.dirlink.nodir); } }, options); } }; return { title : config.title || FIRSTCLASS.locale.editor.dirlink.deftitle, minWidth: 425, maxWidth: 425, minHeight: 150, maxHeight:150, contents : [ { id : 'main', elements : [ { type : 'text', id : 'search', label : config.srchtitle || FIRSTCLASS.locale.editor.dirlink.deflabel, labelLayout: 'horizontal', widths: [ '25%', '75%' ], style: 'width : 100%;', isChanged: false, validate: function() { return true; }, setup: function(data) { }, commit: function(data) { data.search = this.getValue(); } }, { type: 'select', id: 'match', label: config.picktitle || FIRSTCLASS.locale.editor.dirlink.defpick, labelLayout: 'horizontal', widths: [ '25%', '75%' ], 'default': '', 'size': '7', style: 'width: 100%;', items : [ [ '' , ''] ], isChanged: false, validate: function() { var func = CKEDITOR.dialog.validate.notEmpty(FIRSTCLASS.locale.editor.dirlink.nochoice); return func.apply(this); }, setup: function(data) { }, commit: function(data) { data.match = this.getValue(); } } ] } ], onShow : function() { this.fakeObj = false; // get link object if any var editor = this.getParentEditor(), selection = editor.getSelection(), ranges = selection.getRanges(), element = null, me = this; if (ranges.length == 1) { var rangeRoot = ranges[0].getCommonAncestor( true ); element = rangeRoot.getAscendant( 'a', true ); if (element && element.getAttribute('href')) { selection.selectElement(element); } else if ((element = rangeRoot.getAscendant( 'img', true )) && element.getAttribute('_cke_real_element_type') && element.getAttribute('_cke_real_element_type') == 'anchor') { this.fakeObj = element; element = editor.restoreRealElement(this.fakeObj); selection.selectElement(this.fakeObj); } else { element = null; } } var data = { search: '', match: '' }; this.setupContent(data); clearEntries(this); // add key event handler for search box var srchEl = this.getContentElement('main','search'); srchEl.getInputElement().on('keydown',function(evt) { var that = this; // clear pending key delay timer if (this._keyTimer) { window.clearTimeout(this._keyTimer); this._keyTimer = null; } // on enter do search if (evt.data.getKeystroke() == 13) { doLookup(this); evt.stop(); } else { // reset timer this._keyTimer = window.setTimeout(function() { doLookup(that); that._keyTimer = null; },1000); } }, this); }, onOk : function() { var attributes = { href : '/*' + CKEDITOR.tools.getNextNumber() + '*/' }, removeAttributes = [], data = { href : attributes.href }, me = this, editor = this.getParentEditor(); // grab the field data this.commitContent(data); // Compose the URL var url = '', cid, selected = FIRSTCLASS.util.Message.parseSelectedLink(this), linktext = selected.linktext; if (data.match.length > 0 && this._matches && this._matches[data.match]) { if (config.objtype == FIRSTCLASS.objTypes.user) { cid = this._matches[data.match].cid; if (cid.indexOf('CID') < 0) { cid = 'CID' + cid; } url = "#__Open-User/" + cid; attributes.fcid = 'user'; attributes.fcattrs = data.match; attributes.uid = this._matches[data.match].cid; if (attributes.uid.indexOf('CID') >= 0) { attributes.uid = attributes.uid.slice(attributes.uid.indexOf('CID') + 3); } } else if (config.objtype == FIRSTCLASS.objTypes.conference) { cid = this._matches[data.match].cid; if (cid.indexOf('CID') < 0) { cid = 'CID' + cid; } url = "#__Open-Conf/" + cid; } if (url) { attributes._cke_saved_href = url; attributes.href = url; if (!linktext) { linktext = data.match; } } } FIRSTCLASS.util.Message.commitLink(this, attributes, linktext); }, onLoad : function() { } }; }; // ---- userlink - dialog to link to a user's profile/blog CKEDITOR.dialog.add('userlink', function(editor) { return dirlink(editor, { title: FIRSTCLASS.locale.editor.dirlink.usertitle, objtype: FIRSTCLASS.objTypes.user, srchtitle: FIRSTCLASS.locale.editor.dirlink.usersrch, picktitle: FIRSTCLASS.locale.editor.dirlink.userpick }); }); // ---- commlink - dialog to link to a community CKEDITOR.dialog.add('commlink', function(editor) { return dirlink(editor, { title: FIRSTCLASS.locale.editor.dirlink.commtitle, objtype: FIRSTCLASS.objTypes.conference, srchtitle: FIRSTCLASS.locale.editor.dirlink.commsrch, picktitle: FIRSTCLASS.locale.editor.dirlink.commpick }); }); // ---- newwiki - dialog for create-and-link wiki pages CKEDITOR.dialog.add('newwiki', function(editor) { // set status variable and status text var setStatus = function(dialog, state) { dialog._status = state; // grab and save ref to status cell if (!dialog._statusCell) { var stat = dialog.getContentElement('main','status'); dialog._statusCell = FIRSTCLASS.ui.Dom.getChildByIdName('newWikiStatusCell', stat.getInputElement().$); } var newText = ''; switch(state) { case 'load': newText = FIRSTCLASS.locale.editor.newwiki.statload; break; case 'conflict': newText = FIRSTCLASS.locale.editor.newwiki.statconflict; break; case 'ok': newText = FIRSTCLASS.locale.editor.newwiki.statok; break; default: newText = FIRSTCLASS.locale.editor.newwiki.statinitial; dialog._status = 'initial'; } dialog._statusCell.innerHTML = newText; }; // get status var getStatus = function(dialog) { if (typeof dialog._status === 'undefined') { dialog._status = 'initial'; } return dialog._status; }; // test for existing page, set status accordingly var testForPage = function(dialog) { var name = dialog.getContentElement('main','title').getValue(); var strictCompare = function(stack, needle) { return (needle == stack); }; if ((getStatus(dialog) !== 'load' && getStatus(dialog) !== 'initial') || name.length > 2) { if (dialog._dataSource) { var rows = dialog._dataSource.query('name',name, true, strictCompare); if (rows && rows.length > 0) { setStatus(dialog, 'conflict'); } else { setStatus(dialog, 'ok'); } } } return; }; // set url on link object from row var setLinkUrl = function(theDataSource, theLink, theRow) { var attrs = { href: theDataSource.getPermalink(theRow), _cke_saved_href: theDataSource.getPermalink(theRow) }; theLink.setAttributes(attrs); theDataSource.dispose(); }; // fill in dialog's data source with pages from passed-in community var getPageList = function(dialog, communityName) { var cRows = FIRSTCLASS.session.desktop._dataSource.query("name", communityName, true); setStatus(dialog, 'load'); if (cRows && cRows.length > 0) { // dispose existing data source if any if (dialog._dataSource) { dialog._dataSource.dispose(); dialog._dataSource = false; } // create new ds var dsconfig = { containerBaseUrl: FIRSTCLASS.session.baseURL + cRows[0][1].uri, autoLoad: false, watchForNew: false, watchForTinkle: false, messagebodies: false, fetchThreads: false, suppressCatchup: true }; // create data source dialog._dataSource = new FIRSTCLASS.util.DataSource(dsconfig); // add listener to fill the list dialog._dataSource.addRowListener({ fillFinished: function() { // set dialog state back to 'initial' setStatus(dialog,'initial'); // test current page testForPage(dialog); } }); // activate and fetch rows dialog._dataSource.activate(); dialog._dataSource.fetchRowsByObjType(FIRSTCLASS.objTypes.odocument, false); } }; // return the dialog definition object return { title : FIRSTCLASS.locale.editor.newwiki.title, minWidth: 350, maxWidth: 350, minHeight: 80, maxHeight: 80, contents : [ { id : 'main', elements : [ { type : 'select', id : 'community', label : FIRSTCLASS.locale.editor.newwiki.comm, labelLayout: 'horizontal', widths: [ '20%', '80%' ], 'default': '', style: 'width : 100%;', items: [ [ FIRSTCLASS.locale.editor.itemlink.nullitem , ''] ], isChanged: false, validate: function() { var func = CKEDITOR.dialog.validate.notEmpty(FIRSTCLASS.locale.editor.itemlink.listloading); return func.apply(this); }, setup: function(data) { if (data.community) { this.setValue(data.community); } }, commit: function(data) { data.community = this.getValue(); } }, { type: 'text', id: 'title', label: FIRSTCLASS.locale.editor.newwiki.page, labelLayout: 'horizontal', widths: [ '20%', '80%' ], 'default' : '', isChanged: false, validate: function() { var func = CKEDITOR.dialog.validate.notEmpty(FIRSTCLASS.locale.editor.itemlink.noitem); return func.apply(this); }, setup: function(data) { if (data.title) { this.setValue(data.title); } }, commit: function(data) { data.title = this.getValue(); } }, { type: 'html', id: 'status', html: '
 
', style: 'width: 100%;', isChanged: false, validate: function() { if (this._state) { return this._state == 'conflict' || this._state == 'ok'; } }, setup: function(data) { if (data.status) { setStatus(this.getDialog(), data.status); } }, commit: function(data) { data.status = getStatus(this.getDialog()); } } ] } ], onShow : function() { this.fakeObj = false; // get link object if any var editor = this.getParentEditor(), selection = editor.getSelection(), ranges = selection.getRanges(), element = null, link = false; me = this; if (ranges.length == 1) { var rangeRoot = ranges[0].getCommonAncestor( true ); element = rangeRoot.getAscendant( 'a', true ); if (element && element.getAttribute('href')) { selection.selectElement(element); } else if ((element = rangeRoot.getAscendant( 'img', true )) && element.getAttribute('_cke_real_element_type') && element.getAttribute('_cke_real_element_type') == 'anchor') { this.fakeObj = element; element = editor.restoreRealElement(this.fakeObj); selection.selectElement(this.fakeObj); } else { element = null; } if (!element) { link = FIRSTCLASS.util.Message.parseSelectedLink(this); } } // get list of communities from desktop var communities = FIRSTCLASS.session.desktop._dataSource._data.records, commElement = this.getContentElement('main', 'community'), options = []; // fill in the community selector if (commElement) { commElement.clear(); for (var comm in communities) { if (communities[comm].name && FIRSTCLASS.objTypes.isContainer(communities[comm].typedef.objtype)) { commElement.add(communities[comm].name); } } } // fill in default values var commUri = FIRSTCLASS.lang.removeSlashUrl(FIRSTCLASS.session._activeObject); commUri = commUri.slice(commUri.lastIndexOf('/') + 1); var rows = FIRSTCLASS.session.desktop._dataSource.query("uri", commUri, true); var data = {}; if (rows && rows.length > 0) { data.community = rows[0][1].name; this._currcomm = data.community; } data.status = 'load'; data.title = ''; if (link && link.linktext) { data.title = link.linktext; } this.setupContent(data); // fill page list from current community getPageList(this, data.community); // add key event handler for page title to query container and set page status var titleEl = this.getContentElement('main','title'); titleEl.getInputElement().on('keyup',function(evt) { testForPage(this); }, this); // add change event handler to community picker to fetch wiki pages for the chosen community commElement.getInputElement().on('change', function(evt) { // look up new community var name = this.getValue(), dlg = this.getDialog(); // grab its pages getPageList(dlg, name); }, commElement); }, onOk : function() { var attributes = { href : '/*' + CKEDITOR.tools.getNextNumber() + '*/' }, removeAttributes = [], data = { href : attributes.href }, me = this, editor = this.getParentEditor(); // grab the field data this.commitContent(data); // set up the link object var linktext = data.title; if (this._currcomm && data.community.length > 0 && data.community !== this._currcomm) { // add community prefix linktext = data.community + ' : ' + linktext; } var theLink = false; theLink = FIRSTCLASS.util.Message.commitLink(this, attributes, linktext); // create page and set link url var theRow = null; if (getStatus(this) !== 'conflict') { // create the page if new var createUrl = FIRSTCLASS.lang.ensureSlashUrl(this._dataSource._config.containerBaseUrl) + FIRSTCLASS.opCodes.FormNew + "?FormID=20501&Templates=JS&Quit=1&Name=" + encodeURI(data.title); var callback = { // FIX: for now, success and failure are somewhat synonymous handleResponse: function(text) { var responseObj = FIRSTCLASS.util.net._parseJSONResponse(text); if (responseObj) { // create pseudo-row from response (just need typedef.objtype, threadid, messageid) theRow = { typedef: { objtype: FIRSTCLASS.objTypes.odocument }, threadid: responseObj.item.threadid, messageid: responseObj.item.messageid }; setLinkUrl(me._dataSource, theLink, theRow); } }, success: function(response) { if (response.responseText) { this.handleResponse(response.responseText); } }, failure: function(response) { if (response.responseText) { this.handleResponse(response.responseText); } } }; FIRSTCLASS.util.net.asyncRequest("POST",createUrl, callback); } else { // look up existing row var rows = this._dataSource.query('name', data.title, true); if (rows && rows.length > 0) { theRow = rows[0][1]; setLinkUrl(me._dataSource, theLink, theRow); } } }, onCancel: function(evt) { this.hide(); evt.stop(); } }; }); // ---- newdoc - dialog for upload-and-link documents // /* Instantiates the upload document workflow to upload into the current community, * and inserts link(s) to the new document(s) on completion */ CKEDITOR.dialog.add('newdoc', function(editor) { // create data source object for dialog based on community name var setDataSource = function(dialog, communityName) { var cRows = FIRSTCLASS.session.desktop._dataSource.query("name", communityName, true); if (cRows && cRows.length > 0) { // dispose existing data source if any if (dialog._dataSource) { dialog._dataSource.dispose(); dialog._dataSource = false; } // create new ds var dsconfig = { containerBaseUrl: FIRSTCLASS.session.baseURL + cRows[0][1].uri, autoLoad: false, watchForNew: false, watchForTinkle: false, messagebodies: false, fetchThreads: false, suppressCatchup: true }; // create data source dialog._dataSource = new FIRSTCLASS.util.DataSource(dsconfig); // activate it dialog._dataSource.activate(); // save current community name dialog._selComm = cRows[0][1].name; } }; // Create or configure a link object in the document based on the passed-in file information // Create a ds 'row' and use the current ds to generate the link URL var commitLink = function(dialog, fileInfo) { var attributes = { href : '/*' + CKEDITOR.tools.getNextNumber() + '*/' }, removeAttributes = [], editor = dialog.getParentEditor(), responseObj = fileInfo.response; /* if (responseObj) { // create pseudo-row and get url var theRow = { typedef: { objtype: FIRSTCLASS.objTypes.odocument }, threadid: responseObj.item.threadid, messageid: responseObj.item.messageid }; attributes.href = dialog._dataSource.getPermalink(theRow); attributes._cke_saved_href = dialog._dataSource.getPermalink(theRow); // set up the link object var linktext = false; if (dialog._linktext) { linktext = dialog._linktext; } else { linktext = fileInfo.name; if (dialog._currcomm && dialog._selComm.length > 0 && dialog._selComm !== dialog._currcomm) { // add community prefix linktext = dialog._selComm + ' : ' + linktext; } } */ if (responseObj) { var linktext = false; var url = FIRSTCLASS.lang.ensureSlashUrl(dialog._dataSource.getContainerUrl()); var linktext = false; if (dialog._linktext) { linktext = dialog._linktext; } else { linktext = fileInfo.name; if (dialog._currcomm && dialog._selComm.length > 0 && dialog._selComm !== dialog._currcomm) { // add community prefix linktext = dialog._selComm + ' : ' + linktext; } } url += "?Templates=JS&JSON=2&Show=0&Files=1&ColFilter=8010_(" + fileInfo.name + ")"; var cb = { success: function(o) { var theRow = false; if (o.responseJSON && o.responseJSON.records && o.responseJSON.records[0]) { theRow = o.responseJSON.records[0]; if (theRow) { attributes.href = dialog._dataSource.getPermalink(theRow); attributes._cke_saved_href = dialog._dataSource.getPermalink(theRow); FIRSTCLASS.util.Message.commitLink(dialog, attributes, linktext); } } } }; var connObj = FIRSTCLASS.util.net.asyncRequest('GET',url, cb); } }; // install uploader control into passed-in dialog var configureUploader = function(dialog) { dialog._uploadConfig = { enableMulti: dialog._fakeObj === false, ds: dialog._dataSource, contentEl: $('newLinkDocUploader'), callbacks: { onUpload: function(oFileInfo, oResponse) { commitLink(dialog, oFileInfo, oResponse); }, onComplete: function() { dialog.click('cancel'); }, onError: function(errMsg) { dialog.click('cancel'); alert(errMsg); }, onUploadReady: function() { } } }; FIRSTCLASS.apps.Workflows.uploadDocument.reconfigure(dialog._uploadConfig); dialog._uploader = FIRSTCLASS.apps.Workflows.uploadDocument; }; return { title : FIRSTCLASS.locale.editor.newdoc.title, minWidth: 450, maxWidth: 525, minHeight: 225, width: 525, contents : [ { id : 'main', elements : [ { type : 'select', id : 'community', label : FIRSTCLASS.locale.editor.newwiki.comm, labelLayout: 'horizontal', widths: [ '20%', '80%' ], 'default': '', style: 'width : 100%;', items: [ [ FIRSTCLASS.locale.editor.itemlink.nullitem , ''] ], isChanged: false, validate: function() { var func = CKEDITOR.dialog.validate.notEmpty(FIRSTCLASS.locale.editor.itemlink.listloading); return func.apply(this); }, setup: function(data) { if (data.community) { this.setValue(data.community); } } }, { type: 'html', id: 'uploader', html: '
', style: 'width: 100%;', isChanged: false, validate: function() { var dlg = this.getDialog(); if (dlg._uploader && typeof dlg._uploader.uploadFiles === 'function') { dlg._uploader.uploadFiles(); } return false; }, setup: function(data) { } } ] } ], onShow : function() { this.fakeObj = false; // get link object if any var editor = this.getParentEditor(), selection = editor.getSelection(), ranges = selection.getRanges(), element = null, link = false; me = this; if (ranges.length == 1) { var rangeRoot = ranges[0].getCommonAncestor( true ); element = rangeRoot.getAscendant( 'a', true ); if (element && element.getAttribute('href')) { selection.selectElement(element); } else if ((element = rangeRoot.getAscendant( 'img', true )) && element.getAttribute('_cke_real_element_type') && element.getAttribute('_cke_real_element_type') == 'anchor') { this.fakeObj = element; element = editor.restoreRealElement(this.fakeObj); selection.selectElement(this.fakeObj); } else { element = null; } if (!element) { link = FIRSTCLASS.util.Message.parseSelectedLink(this); this._linktext = link.linktext; } } // get list of communities from desktop var communities = FIRSTCLASS.session.desktop._dataSource._data.records, commElement = this.getContentElement('main', 'community'), options = []; // fill in the community selector if (commElement) { commElement.clear(); for (var comm in communities) { if (communities[comm].name && FIRSTCLASS.objTypes.isContainer(communities[comm].typedef.objtype)) { commElement.add(communities[comm].name); } } } // initialize to current community var commUri = FIRSTCLASS.lang.removeSlashUrl(FIRSTCLASS.session._activeObject); commUri = commUri.slice(commUri.lastIndexOf('/') + 1); var rows = FIRSTCLASS.session.desktop._dataSource.query("uri", commUri, true); var data = {}; if (rows && rows.length > 0) { data.community = rows[0][1].name; this._currcomm = this._selComm = data.community; } data.title = ''; this.setupContent(data); //initially use current community's data source setDataSource(this, this._currcomm); // install uploader control configureUploader(this); // add change event handler to community picker to reconfigure the uploader commElement.getInputElement().on('change', function(evt) { // look up new community var name = this.getValue(), dlg = this.getDialog(); // update the data source setDataSource(dlg, name); // update uploader configureUploader(dlg); }, commElement); }, onOk : function() { }, onCancel: function(evt) { this.hide(); evt.stop(); }, enableUpload: function() { } }; });