(function () { "use strict"; var applicationData = Windows.Storage.ApplicationData.current; var localSettings = applicationData.localSettings; var selectionRange = null; var editedpost; function ready(element, options) { WinJS.UI.Animation.enterPage(document.querySelector('.fragment.new-post'), { top: '0px', left: '50px' }); //select the title if this is a new post (visual clue for the user) var title = document.getElementById('new-post-title'); var content = document.getElementById('new-post-content'); var publishButton = document.getElementById('publish-button'); if (options && options.hasOwnProperty('editedpost')) { editedpost = JSON.parse(options.editedpost); publishButton.innerText = "Update"; } else { editedpost = null; } if ( editedpost ) { title.innerHTML = editedpost.title; } else if (localSettings.values["wpcomBlogPostTitle"]) { title.innerHTML = localSettings.values["wpcomBlogPostTitle"]; } else { title.focus(); } if ( editedpost ) { content.innerHTML = window.toStaticHTML(editedpost.content); } else if (localSettings.values["wpcomBlogPostContent"]) { content.innerHTML = localSettings.values["wpcomBlogPostContent"]; } if (title.innerHTML == 'New Post') { var range = document.body.createTextRange(); range.moveToElementText(title); range.select(); } //show the appbar if it's not visible if (document.getElementById('appbar').winControl.hidden == true) { document.getElementById('appbar').winControl.sticky = true; document.getElementById('appbar').winControl.show(); } var blogname = document.getElementById('blogname'); blogname.innerHTML = 'Publishing to ' + WPCom.getCurrentBlogURL(); title.addEventListener('click', clearTitle, false); title.addEventListener('keypress', titleChanged, true); content.addEventListener('focus', clearContent, false); content.addEventListener('mouseup', setButtonState, true); content.addEventListener('keydown', setButtonState, true); document.getElementById('linkaddress').addEventListener('keypress', createLinkKeyDown, true); document.getElementById('publish-button').addEventListener('click', publishPost, false); document.getElementById('back-button').addEventListener('click', goBack, false); document.getElementById('bold').addEventListener('click', richTextClick, true); document.getElementById('italic').addEventListener('click', richTextClick, true); document.getElementById('underline').addEventListener('click', richTextClick, true); document.getElementById('strikethrough').addEventListener('click', richTextClick, true); document.getElementById('insertunorderedlist').addEventListener('click', richTextClick, true); document.getElementById('insertorderedlist').addEventListener('click', richTextClick, true); document.getElementById('insertimage').addEventListener('click', richTextClick, true); document.getElementById('insertimagelibrary').addEventListener('click', richTextClick, true); document.getElementById('insertimagecamera').addEventListener('click', richTextClick, true); document.getElementById('justifyleft').addEventListener('click', richTextClick, true); document.getElementById('justifycenter').addEventListener('click', richTextClick, true); document.getElementById('justifyright').addEventListener('click', richTextClick, true); document.getElementById('linkflyout').addEventListener('click', richTextClick, true); document.getElementById('createlink').addEventListener('click', createLink, true); document.getElementById('back-button').addEventListener('click', goBack, false); //soft keyboard listener to adjust the content field height var origHeight = document.getElementById('new-post-content').style.height; var inputPane = Windows.UI.ViewManagement.InputPane.getForCurrentView(); inputPane.addEventListener("showing", function (e) { if (document.getElementById('new-post-content') != undefined) document.getElementById('new-post-content').style.height = '24%'; }, false); inputPane.addEventListener("hiding", function (e) { if (document.getElementById('new-post-content') != undefined) { document.getElementById('new-post-content').style.height = origHeight; document.getElementById('appbar').style.posBottom = '0'; } }, false); return; // convenient to set breakpoint :) } function richTextClick(m) { var richTextType = m.target.id; if (window.getSelection().focusNode.parentNode.id != 'new-post-title' || richTextType === 'insertimage') { document.getElementById('new-post-content').focus(); window.scrollTo(0, 0); if (richTextType === 'insertimage') { var imageFlyoutUI = document.getElementById('imageFlyout'); var imageFlyout = imageFlyoutUI.winControl; imageFlyout.show(m.target, "top"); } else if (richTextType === 'insertimagelibrary') { var picker = new Windows.Storage.Pickers.FileOpenPicker(); picker.fileTypeFilter.replaceAll([".jpg", ".png", ".gif"]); picker.viewMode = Windows.Storage.Pickers.PickerViewMode.thumbnail; picker.pickSingleFileAsync().then(function (file) { if (file) { WPCom.resizeImage(file, document, true, true); } }); } else if (richTextType === 'insertimagecamera') { var captureUI = new Windows.Media.Capture.CameraCaptureUI(); captureUI.photoSettings.allowCropping = true; captureUI.photoSettings.croppedAspectRatio.height = 3; captureUI.photoSettings.croppedAspectRatio.width = 4; captureUI.photoSettings.croppedSizeInPixels.height = 0; captureUI.photoSettings.croppedSizeInPixels.width = 0; captureUI.photoSettings.format = Windows.Media.Capture.CameraCaptureUIPhotoFormat.jpeg; captureUI.photoSettings.maxResolution = Windows.Media.Capture.CameraCaptureUIMaxPhotoResolution.highestAvailable; captureUI.captureFileAsync(Windows.Media.Capture.CameraCaptureUIMode.photo).then(function (file) { if (file) { WPCom.resizeImage(file, document, true, true); } }); } else if (richTextType === 'linkflyout') { selectionRange = document.selection.createRange(); if (selectionRange.text === '') { WPCom.displayToastMessage("Please select some text to create a link."); selectionRange = null; } else { var linkFlyoutUI = document.getElementById('linkFlyout'); var linkFlyout = linkFlyoutUI.winControl; linkFlyout.show(m.target, "top"); } } else { document.execCommand(richTextType, false, null); } if (richTextType === 'bold' || richTextType === 'italic' || richTextType === 'underline' || richTextType === 'strikethrough' || richTextType === 'indent') { if (WinJS.Utilities.hasClass(m.target, "selected")) { WinJS.Utilities.removeClass(m.target, "selected"); } else { WinJS.Utilities.addClass(m.target, "selected"); } } } } function createLink(e) { var linkaddress = document.getElementById('linkaddress').value; var linkFlyoutUI = document.getElementById('linkFlyout'); var linkFlyout = linkFlyoutUI.winControl; linkFlyout.hide(); if (!linkaddress.match('/^http.?\:\/\//')) linkaddress = 'http://' + linkaddress; selectionRange.select(); document.execCommand('createlink', false, linkaddress); selectionRange = null; document.getElementById('linkaddress').value = null; } function createLinkKeyDown(e) { if (e.charCode == 13) { event.returnValue = false; document.getElementById('createlink').click(); } } function setButtonState(e) { var range = document.selection.createRange(); if (range.queryCommandState('bold')) WinJS.Utilities.addClass(document.getElementById('bold'), "selected"); else WinJS.Utilities.removeClass(document.getElementById('bold'), "selected"); if (range.queryCommandState('italic')) WinJS.Utilities.addClass(document.getElementById('italic'), "selected"); else WinJS.Utilities.removeClass(document.getElementById('italic'), "selected"); if (range.queryCommandState('underline')) WinJS.Utilities.addClass(document.getElementById('underline'), "selected"); else WinJS.Utilities.removeClass(document.getElementById('underline'), "selected"); if (range.queryCommandState('strikethrough')) WinJS.Utilities.addClass(document.getElementById('strikethrough'), "selected"); else WinJS.Utilities.removeClass(document.getElementById('strikethrough'), "selected"); } function clearTitle() { var titleDiv = document.getElementById('new-post-title'); if (titleDiv.innerHTML == 'New Post') titleDiv.innerHTML = ''; } function clearContent() { var contentDiv = document.getElementById('new-post-content'); if (contentDiv.innerHTML == '
Tap here to type
') contentDiv.innerHTML = ''; } function titleChanged(e) { var titleDiv = document.getElementById('new-post-title'); if (e.charCode == 13 || titleDiv.innerText.length >= 100) { event.cancelBubble = true; event.returnValue = false; } } function publishPost() { var title = document.getElementById('new-post-title'); // Replace tags that were created by the editor. What is this, 1998? // This is a not-so-great solution for trac #15, hoping the source of the problem can be fixed soon. var content = document.getElementById('new-post-content'); var fonts = content.getElementsByTagName('font'); for (var i = 0; i < fonts.length; i++) { var sel = window.getSelection(); var parent = fonts[i].parentElement; var html = fonts[i].innerHTML; parent.removeChild(fonts[i]); parent.appendChild(document.createTextNode(html)); } var button = document.getElementById('publish-button'); if ((title.innerHTML != 'New Post' && content.innerHTML != 'Tap here to type') || ((title.innerHTML == 'New Post' || title.innerHTML == '') && (content.innerHTML != 'Tap here to type' && content.innerHTML != ''))) { button.innerText = 'Publishing...'; button.disabled = true; var accessToken = WPCom.getCurrentAccessToken(); var blogID = WPCom.getCurrentBlogID(); var data = new FormData(); var postContent = content.innerHTML.replace(/</g, '<').replace(/>/g, '>'); var url; data.append('title', title.innerText); data.append('content', postContent); data.append('format', 'standard'); if (!blogID || !accessToken) { return; } var progress = document.getElementById('post-progress'); progress.style.display = 'block'; if (editedpost) { url = WPCom.apiURL + '/sites/' + blogID + '/posts/' + editedpost.ID; } else { url = WPCom.apiURL + '/sites/' + blogID + '/posts/new'; } WinJS.xhr({ type: 'POST', url: url, headers: { 'Authorization': 'Bearer ' + accessToken }, data: data }).then(function (result) { progress.style.display = 'none'; var resultData = JSON.parse(result.responseText); if (resultData.ID) { localSettings.values["wpcomBlogPostTitle"] = null; localSettings.values["wpcomBlogPostContent"] = null; button.innerText = 'Published!'; var messagedialogpopup = new Windows.UI.Popups.MessageDialog('Your post was successfully published. View it now?', 'Post Published'); messagedialogpopup.commands.append(new Windows.UI.Popups.UICommand('yes', function () { document.getElementById('appbar').winControl.sticky = false; document.getElementById('appbar').winControl.hide(); WinJS.Navigation.back(); WPCom.pushWebView(resultData.short_URL, true); })); messagedialogpopup.commands.append(new Windows.UI.Popups.UICommand('no', function () { WinJS.Navigation.back(); })); var updatedialogpopup = new Windows.UI.Popups.MessageDialog('Your post was successfully updated.', 'Post Updated'); updatedialogpopup.commands.append(new Windows.UI.Popups.UICommand('ok', function () { document.getElementById('appbar').winControl.sticky = false; document.getElementById('appbar').winControl.hide(); WinJS.Navigation.back(2); })); if (editedpost) { updatedialogpopup.showAsync(); } else { messagedialogpopup.showAsync(); } } else { progress.style.display = 'none'; showErrorDialog(false); button.disabled = false; button.innerText = 'Publish'; } }, function (result) { progress.style.display = 'none'; if (result.responseText != undefined && result.responseText != '') { try { var response = JSON.parse(result.responseText); if (response.error != undefined && response.error == 'unauthorized') showErrorDialog(true); else showErrorDialog(false); } catch (e) { showErrorDialog(false); } } else { showErrorDialog(false); } button.disabled = false; button.innerText = 'Publish'; }); } } function saveDraft() { var title = document.getElementById('new-post-title'); var content = document.getElementById('new-post-content'); localSettings.values["wpcomBlogPostTitle"] = title.innerHTML; localSettings.values["wpcomBlogPostContent"] = content.innerHTML; } function showErrorDialog(isJetpackUnauthorized) { var messagedialogpopup = new Windows.UI.Popups.MessageDialog('A connection error occured. Would you like to try again?', 'Publishing Failed'); if (isJetpackUnauthorized) { messagedialogpopup = new Windows.UI.Popups.MessageDialog('Please activate the JSON API module in Jetpack to enable access.', 'Unable to Connect'); messagedialogpopup.commands.append(new Windows.UI.Popups.UICommand('save draft', function () { saveDraft(); WinJS.Navigation.back(); })); } else { messagedialogpopup.commands.append(new Windows.UI.Popups.UICommand('try again', function () { publishPost(); })); messagedialogpopup.commands.append(new Windows.UI.Popups.UICommand('save draft', function () { saveDraft(); WinJS.Navigation.back(); })); } messagedialogpopup.commands.append(new Windows.UI.Popups.UICommand('close', function () { })); messagedialogpopup.showAsync(); } function goBack() { var title = document.getElementById('new-post-title'); var content = document.getElementById('new-post-content'); document.getElementById('appbar').winControl.sticky = false; document.getElementById('appbar').winControl.hide(); if ( (title.innerHTML != 'New Post' && content.innerHTML != 'Tap here to type' && !editedpost) || ( (title.innerHTML == 'New Post' || title.innerHTML == '') && (content.innerHTML != 'Tap here to type' && content.innerHTML != '') && !editedpost ) ) { //Save prompt var messagedialogpopup = new Windows.UI.Popups.MessageDialog('Would you like to save this post?', 'Save Post'); messagedialogpopup.commands.append(new Windows.UI.Popups.UICommand('save', function () { saveDraft(); WinJS.Navigation.back(); })); messagedialogpopup.commands.append(new Windows.UI.Popups.UICommand('discard', function () { localSettings.values["wpcomBlogPostTitle"] = null; localSettings.values["wpcomBlogPostContent"] = null; WinJS.Navigation.back(); })); messagedialogpopup.showAsync(); } else { localSettings.values["wpcomBlogPostTitle"] = null; localSettings.values["wpcomBlogPostContent"] = null; editedpost = null; WinJS.Navigation.back(); } } WinJS.UI.Pages.define('/pages/new-post.html', { ready: ready }); })();