Created
March 21, 2016 20:35
-
-
Save heyellieday/05694da045af7649247f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Here You can type your custom JavaScript... | |
var SemiDynamic = function(config){ | |
cache = { | |
get: function(namespace, id){ | |
return JSON.parse(localStorage.getItem(namespace + "-" + id)) || []; | |
}, | |
add: function(namespace, id, value){ | |
var array = this.get(namespace, id); | |
array.push(value); | |
localStorage.setItem(namespace + "-" + id, JSON.stringify(array)); | |
} | |
}; | |
posts = { | |
constructor: function(postData){ | |
post = { | |
id: postData.id, | |
data: postData, | |
paragraphs: postData.data.content.data.bodyModel.paragraphs, | |
registeredParagraphNames: function(){ | |
return cache.get("paragraphNames", this.id); | |
}, | |
paragraphIsRegistered: function(paragraphName){ | |
return this.registeredParagraphNames().indexOf(paragraphName) != -1; | |
}, | |
registerParagraphNames: function(){ | |
for (var i = this.paragraphs.length - 1; i >= 0; i--) { | |
var paragraphName = this.paragraphs[i].data.name; | |
if (this.paragraphs[i].data.text.indexOf("{{semi}}") != -1 && !this.paragraphIsRegistered(paragraphName)){ | |
cache.add("paragraphNames", postId, paragraphName); | |
} | |
} | |
}, | |
prepareDeltas: function(){ | |
preparedDeltas = []; | |
paragraphs = this.paragraphs | |
for (var i = paragraphs.length - 1; i >= 0; i--) { | |
paragraph = paragraphs[i]; | |
if (post.registeredParagraphNames().indexOf(paragraph.data.name) != -1){ | |
paragraphData = paragraph.data; | |
// update paragraph text dynamically with desired source, but for now, get current DateTime. | |
paragraphData.text = new Date(); | |
delta = { | |
paragraph: paragraphData, | |
index: i, | |
type: 3 // figure out what that means | |
} | |
preparedDeltas.push(delta); | |
} | |
} | |
requestBody = { | |
deltas: preparedDeltas, | |
id: post.id, | |
baseRev: post.data.latestRev++ | |
} | |
return requestBody; | |
}, | |
updateDeltas: function(){ | |
deltaRequestBody = this.prepareDeltas(); | |
if (deltaRequestBody.deltas.length > 0){ | |
$.ajax({ | |
method: "POST", | |
dataType: "json", | |
headers: { | |
"content-type": "application/json", | |
"x-obvious-cid": "web", | |
"x-xsrf-token": config.xsrfToken | |
}, | |
url: "/p/"+ post.id + "/deltas?logLockId=123", | |
data: JSON.stringify(deltaRequestBody) | |
}); | |
}else{ | |
return "No semi-dynamic paragraphs!" | |
} | |
} | |
} | |
return post; | |
}, | |
raw_data: function(){ | |
// The two letter object keys change due to minification, so something needs to account for that. | |
return window.__obv.Wj["screen-request"].fa.rc.Post; | |
}, | |
all: function(){ | |
var builtPosts = {}; | |
for (var i in this.raw_data()){ | |
if (this.raw_data()[i].data.content != null){ | |
builtPosts[i] = this.constructor(this.raw_data()[i]); | |
} | |
} | |
return builtPosts; | |
}, | |
get: function(postId){ | |
return this.all()[postId]; | |
} | |
}; | |
var constructor = { | |
cache: cache, | |
posts: posts | |
} | |
return constructor; | |
}; | |
xsrfToken = "sampleToken" // Get from random request when logged in | |
semiDynamic = new SemiDynamic({xsrfToken: xsrfToken}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment