(function($) { tinymce.PluginManager.add( 'apester_shortcode_handler', function( editor, url ) { var PROTOCOL = 'https:'; var CONST = { displayUrlPrefix: PROTOCOL + '//display.apester.com/interactions/', displayUrlSufffix: '/display', imagesPath:'public/img/' }; // get the plugin path from outside the tinyMCE iframe var apesterPluginPath = window.parent.apester_plugin_path; /** * get an attribute's value from a string of html (e.g: '
') * @param attr * @param stringToIterate * @returns string - the value of the attribute passed */ var getAttrValueInString = function(attr, stringToIterate) { var result; var propertyRegex = new RegExp(attr + '=\"(.*?)\"','g'); var found = propertyRegex.exec(stringToIterate); if ( found ) { result = found[1]; } return result; }; /** * returns the properties object for event sending * @param node - the html element that has the id/token property on it * @returns {{}} - object that meets the event.properties form */ var getEventPropsFromElement = function(node) { var $content = $(node).find('.apester-media-wrapper'); var id = $content.attr('id'); var idComponents = id.split('-'); var mediaType = idComponents[1]; var mediaId = idComponents[2]; var properties = {}; // in case of specific interaction we use interactionId if (mediaType === 'media') { properties.interactionId = mediaId; } // in case of random playlist interaction we use channelToken else { properties.channelToken = mediaId; } return properties; }; var sendEvent = function(eventDataObject) { var baseUrl = PROTOCOL + '//events.apester.com/event'; var payload = { event: '', properties: { pluginProvider: 'WordPress', pluginVersion: window.apester_plugin_version, phpVersion: window.php_version, wpVersion: window.wp_version }, metadata: { referrer: encodeURIComponent(document.referrer), current_url: encodeURIComponent(window.location.href), screen_height: window.screen.height.toString(), screen_width: window.screen.width.toString(), language: window.navigator.userLanguage || window.navigator.language }}; var eventData = $.extend(true, {}, payload, eventDataObject); $.ajax({ type: 'POST', url: baseUrl, contentType: 'application/json; charset=UTF-8', data: JSON.stringify(eventData), async: false }); }; // replace apester shortcode with a media preview editor.on( 'BeforeSetContent', function( event ) { event.content = event.content.replace( /\[interaction([^\]]*)\]/g, function( all, attr, con ) { var idAttr = 'id="', idAttrIndex = attr.indexOf(idAttr); var mediaId = attr.slice(idAttrIndex + idAttr.length, -1); // get interactions dimensions $.ajax({ url : CONST.displayUrlPrefix + mediaId + CONST.displayUrlSufffix, type : "get", dataType : "json", success : function( data ) { var mediaDimensions = data.payload.data.size; // create a style tag inside the tinyMCE editor iframe so when user deletes a media and un-does the deletion, the media dimensions will be correct again var css = '#apester-media-' + mediaId + ' iframe {' +'height:' + mediaDimensions.height +'px;' +'max-width: ' + mediaDimensions.width + 'px;' + 'border: none;' +'}'; var styleTag = document.createElement('style'); styleTag.type = 'text/css'; styleTag.id = 'apester-media-' + mediaId + '-css'; var editorDocument = tinyMCE.activeEditor.dom.doc; if (styleTag.styleSheet) { styleTag.styleSheet.cssText = css; } else { styleTag.appendChild(document.createTextNode(css)); } editorDocument.head.appendChild(styleTag); } }); // create the shortcode-replacement HTML elements var wrapper, mediaWrapper, iframe, mediaOverlay, closeBtn; wrapper = $("