Advertisement
Guest User

Untitled

a guest
Nov 29th, 2015
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.21 KB | None | 0 0
  1. var MethodeBridge = function(){
  2. var self = {}, app;
  3. try {
  4. app = external.EomQueryInterface('Methode.Application');
  5. } catch(e) {
  6.  
  7. }
  8. if (!app) return false;
  9.  
  10.  
  11. self.assetAssociationRules = {
  12. IMAGE: {
  13. '316x237': [
  14. 'story-container1'
  15. ],
  16. '650x366': [
  17. 'story-container2',
  18. 'story-container3'
  19. ]
  20. },
  21. NEWS_STORY: {
  22. 'default': [
  23. 'story-container1',
  24. 'story-container2',
  25. 'story-container3',
  26. 'promo-text'
  27. ]
  28. }
  29. };
  30.  
  31. self.containers = {
  32. story: [
  33. 'container1',
  34. 'container2',
  35. 'container3',
  36. 'text'
  37. ],
  38. promo: [
  39. 'text'
  40. ]
  41. };
  42.  
  43. self.getCurrentDocumentType = function(){
  44. // debug(arguments);
  45. // always return story for now
  46. return 'story';
  47. };
  48.  
  49. self.getAvaliableContainers = function(documentType) {
  50. // debug(arguments);
  51. return self.containers[documentType];
  52. };
  53.  
  54. self.getAvaliableContainersForAsset = function(assetItem) {
  55. // debug(arguments);
  56. var documentType = self.getCurrentDocumentType(),
  57. rules = self.assetAssociationRules[assetItem.contentType],
  58. containers = self.getAvaliableContainers(documentType),
  59. associationKey = assetItem.contentType === 'IMAGE' ? assetItem.width + 'x' + assetItem.height : 'default';
  60. var result = rules[associationKey].map(function(containerName){
  61. return containerName.replace(documentType + '-', '');
  62. });
  63. return result;
  64. };
  65.  
  66. self.getContainerForCurrentCursorPosition = function(tagpath) {
  67. // debug(arguments);
  68. var documentType = self.getCurrentDocumentType(),
  69. containers = self.getAvaliableContainers(documentType),
  70. result;
  71. for (var i = 0; i < containers.length; i++) {
  72. if (tagpath.indexOf(containers[i]) !== -1) result = containers[i];
  73. }
  74. return result;
  75. };
  76.  
  77. self.validateInsertion = function(container, assetItem, tagpath) {
  78. // debug(arguments);
  79. var documentType = self.getCurrentDocumentType(),
  80. position = container || self.getContainerForCurrentCursorPosition(tagpath);
  81. if (!position) throw new Error('Please drag and drop in the text or in one of the container');
  82. var validContainers = self.getAvaliableContainersForAsset(assetItem);
  83. if (!validContainers) throw new Error('This object type is not valid in this position');
  84. return position;
  85. };
  86.  
  87. self.addTo = function(container, assetItem) {
  88. // debug(arguments);
  89. var tagpath = app.ActiveDocument.Selection.TextView.Tag;
  90. var position = self.validateInsertion(container, assetItem, tagpath);
  91. var tag = self.createEmbed(assetItem);
  92. container = container || position;
  93. if (container) return self.insertByContainer(container, tag);
  94. if (position.indexOf('text') === -1) return self.insertByContainer(position, tag);
  95. return self.addTagToParagraph(tag);
  96. };
  97.  
  98. self.addTagToParagraph = function(tag) {
  99. // // debug(arguments);
  100. return app.ActiveDocument.Selection.TextView.SetText('<p channel="Web">' + tag + '</p>',true,false);
  101. };
  102.  
  103. function escapeRegExp(str) {
  104. return str.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");
  105. }
  106.  
  107. function replaceAll(str, find, replace) {
  108. return str.replace(new RegExp(escapeRegExp(find), 'g'), replace);
  109. }
  110.  
  111. self.createEmbed = function(assetItem) {
  112. // debug(arguments);
  113. var layout = '<webframe class="kurator" type="oembed" title="{{title}}" url="{{url}}" width="316" height="237">' +
  114. '<p class="webframe_img_oembed"></p>' +
  115. '<p> <span class="webframe_category">TITLE: </span><span class="webframe_title">{{title}}</span></p>' +
  116. '<p><span class="webframe_category">COMMENTS: </span><span class="webframe_title">{{comments}}</span></p>';
  117. '</webframe>';
  118. var fieldsMapping = {
  119. 'title':'title',
  120. 'url':'link',
  121. 'comments':'userName'
  122. };
  123. for (var key in fieldsMapping) {
  124. layout = replaceAll(layout, '{{' + key + '}}', assetItem[fieldsMapping[key]]);
  125. }
  126. return layout;
  127. };
  128.  
  129. debug = function(arguments){
  130. try {
  131. diemofodie();
  132. } catch (e) {
  133. alert(e.stack +"\n\n" + JSON.stringify(arguments));
  134. }
  135. };
  136.  
  137. self.insertByContainer = function(container, xml) {
  138. //make tags visible to use Automation TextView API
  139. app.ActiveDocument.Selection.TextView.ViewTags = true;
  140. try {
  141. app.ActiveDocument.Selection.TextView.GoToDocBegin();
  142. if (app.ActiveDocument.Selection.TextView.GoToTagByName(container, 0)) {
  143. app.ActiveDocument.Selection.TextView.SelectElement();
  144. var contentAsString = app.ActiveDocument.Selection.TextView.GetText(0),
  145. endTagOfContainer = '</' + container + '>',
  146. indexEndTagOfContainer = contentAsString.lastIndexOf(endTagOfContainer);
  147. // container has no content, and is closing straight
  148. if (indexEndTagOfContainer === -1) {
  149. contentAsString = contentAsString.substr(0, contentAsString.length - 2) + '>' + '<p>' + xml + '</p>' + endTagOfContainer;
  150. } else {
  151. // find out what the container contains
  152. var indexOpenTagOfContainer = contentAsString.indexOf('>') + 1,
  153. containerContent = contentAsString.substring(indexOpenTagOfContainer, indexEndTagOfContainer),
  154. emptyPTagRegex = /<p[a-zA-Z\"=\ ]*\/>/, // <p/> or <p channel="Web"/>
  155. notEmptyPTagRegex = /<p[a-zA-Z\"=\ ]*\>.+<\/p>/,
  156. pTagWithDummyText = '<p><?EM-dummyText [' + container.toUpperCase() + container.substr(1, container.length) + ']?></p>';
  157.  
  158. if (containerContent === pTagWithDummyText || emptyPTagRegex.test(containerContent) && !notEmptyPTagRegex.test(containerContent)) { //only dummytext OR only a p, and is closing straight
  159. contentAsString = contentAsString.substring(0, indexOpenTagOfContainer) + '<p channel="Web">' + xml + '</p>' + '</' + container + '>';
  160. } else { // otherwise, add new xml at the end
  161. var lastIndexEndTagP = contentAsString.lastIndexOf('</p>') + 4;
  162. contentAsString = contentAsString.substring(0, lastIndexEndTagP) + '<p channel="Web">' + xml + '</p>' + '</' + container + '>';
  163. }
  164. }
  165. // insert new content
  166. app.ActiveDocument.Selection.TextView.SetText(contentAsString, true, false);
  167. }
  168. } catch (e) {
  169. throw new Error('\nError in insertByContainer(): ' + e.message);
  170.  
  171. } finally {
  172. //make tags invisible
  173. app.ActiveDocument.Selection.TextView.ViewTags = false;
  174. }
  175. }
  176.  
  177.  
  178. return self;
  179. }();
  180. window.MethodeBridge = MethodeBridge;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement