prismic.io
Advanced tools
Comparing version 1.0.9 to 1.0.10
{ | ||
"name": "prismic-js-SDK", | ||
"version": "1.0.9", | ||
"version": "1.0.10", | ||
"homepage": "https://github.com/prismicio/javascript-kit", | ||
@@ -5,0 +5,0 @@ "authors": [ |
@@ -8,2 +8,5 @@ (function (Global, undefined) { | ||
* | ||
* @global | ||
* @alias Api | ||
* @constructor | ||
* @param {string} url - The mandatory URL of the prismic.io API endpoint (like: https://lesbonneschoses.prismic.io/api) | ||
@@ -20,2 +23,3 @@ * @param {function} callback - Optional callback function that is called after the API was retrieved, to which you may pass two parameters: a potential error (null if no problem), and the API object | ||
}; | ||
// note that the prismic variable is later affected as "Api" while exporting | ||
@@ -53,3 +57,3 @@ // -- Request handlers | ||
// Open the XHR | ||
xhr.open('GET', url + '#json', true); | ||
xhr.open('GET', url, true); | ||
@@ -118,7 +122,3 @@ // Json request | ||
/** | ||
* The Api object you can manipulate, notably to perform queries in your repository. | ||
* Most useful fields: bookmarks, refs, types, tags, ... | ||
*/ | ||
// Defining Api's instance methods; note that the prismic variable is later affected as "Api" while exporting | ||
prismic.fn = prismic.prototype = { | ||
@@ -157,2 +157,3 @@ | ||
* @returns {Api} - The Api object that can be manipulated | ||
* @private | ||
*/ | ||
@@ -230,2 +231,3 @@ parse: function(data) { | ||
* This is for internal use, from outside this kit, you should call Prismic.Api() | ||
* @private | ||
*/ | ||
@@ -241,3 +243,4 @@ init: function(url, accessToken, maybeRequestHandler) { | ||
* @deprecated use form() now | ||
* Returns a useable form from its id, as described in the RESTful description of the API. | ||
* @param {string} formId - The id of a form, like "everything", or "products" | ||
* @returns {SearchForm} - the SearchForm that can be used. | ||
*/ | ||
@@ -253,2 +256,3 @@ forms: function(formId) { | ||
* | ||
* @param {string} formId - The id of a form, like "everything", or "products" | ||
* @returns {SearchForm} - the SearchForm that can be used. | ||
@@ -279,2 +283,3 @@ */ | ||
* | ||
* @param {string} label - the ref's label | ||
* @returns {string} | ||
@@ -296,2 +301,4 @@ */ | ||
* Embodies a submittable RESTful form as described on the API endpoint (as per RESTful standards) | ||
* @constructor | ||
* @private | ||
*/ | ||
@@ -312,2 +319,4 @@ function Form(name, fields, form_method, rel, enctype, action) { | ||
* @constructor | ||
* @global | ||
* @alias SearchForm | ||
*/ | ||
@@ -380,2 +389,32 @@ function SearchForm(api, form, data) { | ||
/** | ||
* Sets a page size to query for this SearchForm. This is an optional method. | ||
* | ||
* @param {number} pageSize - The page size | ||
* @returns {SearchForm} - The SearchForm itself | ||
*/ | ||
pageSize: function(size) { | ||
return this.set("pageSize", size); | ||
}, | ||
/** | ||
* Sets the page number to query for this SearchForm. This is an optional method. | ||
* | ||
* @param {number} page - The page number | ||
* @returns {SearchForm} - The SearchForm itself | ||
*/ | ||
page: function(p) { | ||
return this.set("page", p); | ||
}, | ||
/** | ||
* Sets the orderings to query for this SearchForm. This is an optional method. | ||
* | ||
* @param {string} orderings - The orderings | ||
* @returns {SearchForm} - The SearchForm itself | ||
*/ | ||
orderings: function(orderings) { | ||
return this.set("orderings", orderings); | ||
}, | ||
/** | ||
* Submits the query, and calls the callback function. | ||
@@ -443,3 +482,4 @@ * | ||
* An array of the fragments with the given fragment name. | ||
* The array is often a single-element array, expect when the field is a multiple field. | ||
* The array is often a single-element array, expect when the fragment is a multiple fragment. | ||
* @private | ||
*/ | ||
@@ -463,12 +503,46 @@ function getFragments(name) { | ||
* as well as the field "results", which is an array of Doc objects, the documents themselves. | ||
* @constructor | ||
* @global | ||
*/ | ||
function Documents(page, results_per_page, results_size, total_results_size, total_pages, next_page, prev_page, results) { | ||
/** | ||
* @field | ||
* @description the current page number | ||
*/ | ||
this.page = page; | ||
/** | ||
* @field | ||
* @description the number of results per page | ||
*/ | ||
this.results_per_page = results_per_page; | ||
/** | ||
* @field | ||
* @description the size of the current page | ||
*/ | ||
this.results_size = results_size; | ||
/** | ||
* @field | ||
* @description the total size of results across all pages | ||
*/ | ||
this.total_results_size = total_results_size; | ||
/** | ||
* @field | ||
* @description the total number of pages | ||
*/ | ||
this.total_pages = total_pages; | ||
/** | ||
* @field | ||
* @description the URL of the next page in the API | ||
*/ | ||
this.next_page = next_page; | ||
/** | ||
* @field | ||
* @description the URL of the previous page in the API | ||
*/ | ||
this.prev_page = prev_page; | ||
this.results = results; // an array of Doc objects | ||
/** | ||
* @field | ||
* @description the array of the {Doc} objects | ||
*/ | ||
this.results = results; | ||
} | ||
@@ -479,10 +553,37 @@ | ||
* Most useful fields: id, type, tags, slug, slugs, ... | ||
* @constructor | ||
* @global | ||
* @alias Doc | ||
*/ | ||
function Doc(id, type, href, tags, slugs, fragments) { | ||
/** | ||
* @field | ||
* @description the ID of the document | ||
*/ | ||
this.id = id; | ||
/** | ||
* @field | ||
* @description the type of the document | ||
*/ | ||
this.type = type; | ||
/** | ||
* @field | ||
* @description the URL of the document in the API | ||
*/ | ||
this.href = href; | ||
/** | ||
* @field | ||
* @description the tags of the document | ||
*/ | ||
this.tags = tags; | ||
/** | ||
* @field | ||
* @description the current slug of the document | ||
*/ | ||
this.slug = slugs ? slugs[0] : "-"; | ||
/** | ||
* @field | ||
* @description all the slugs that were ever used by this document (including the current one, at the head) | ||
*/ | ||
this.slugs = slugs; | ||
@@ -494,7 +595,7 @@ this.fragments = fragments; | ||
/** | ||
* Gets the field in the current Document object. Since you most likely know the type | ||
* of this field, it is advised that you use a dedicated method, like get StructuredText() or getDate(), | ||
* Gets the fragment in the current Document object. Since you most likely know the type | ||
* of this fragment, it is advised that you use a dedicated method, like get StructuredText() or getDate(), | ||
* for instance. | ||
* | ||
* @param {string} name - The name of the field to get, with its type; for instance, "blog-post.author" | ||
* @param {string} name - The name of the fragment to get, with its type; for instance, "blog-post.author" | ||
* @returns {object} - The JavaScript Fragment object to manipulate | ||
@@ -520,10 +621,10 @@ */ | ||
/** | ||
* Gets the image field in the current Document object, for further manipulation. | ||
* Typical use: document.getImage('blog-post.photo').asHtml(ctx.link_resolver) | ||
* Gets the image fragment in the current Document object, for further manipulation. | ||
* Typical use: document.getImage('blog-post.photo').asHtml(ctx) | ||
* | ||
* @param {string} field - The name of the field to get, with its type; for instance, "blog-post.photo" | ||
* @returns {Image} - The Image object to manipulate | ||
* @param {string} fragment - The name of the fragment to get, with its type; for instance, "blog-post.photo" | ||
* @returns {ImageEl} - The Image object to manipulate | ||
*/ | ||
getImage: function(field) { | ||
var img = this.get(field); | ||
getImage: function(fragment) { | ||
var img = this.get(fragment); | ||
if (img instanceof Global.Prismic.Fragments.Image) { | ||
@@ -539,4 +640,5 @@ return img; | ||
getAllImages: function(field) { | ||
var images = this.getAll(field); | ||
// Useful for obsolete multiples | ||
getAllImages: function(fragment) { | ||
var images = this.getAll(fragment); | ||
@@ -556,10 +658,10 @@ return images.map(function (image) { | ||
/** | ||
* Gets the view within the image field in the current Document object, for further manipulation. | ||
* Typical use: document.getImageView('blog-post.photo', 'large').asHtml(ctx.link_resolver) | ||
* Gets the view within the image fragment in the current Document object, for further manipulation. | ||
* Typical use: document.getImageView('blog-post.photo', 'large').asHtml(ctx) | ||
* | ||
* @param {string} field - The name of the field to get, with its type; for instance, "blog-post.photo" | ||
* @param {string} fragment - The name of the fragment to get, with its type; for instance, "blog-post.photo" | ||
* @returns {ImageView} - The View object to manipulate | ||
*/ | ||
getImageView: function(field, view) { | ||
var fragment = this.get(field); | ||
getImageView: function(fragment, view) { | ||
var fragment = this.get(fragment); | ||
if (fragment instanceof Global.Prismic.Fragments.Image) { | ||
@@ -578,4 +680,5 @@ return fragment.getView(view); | ||
getAllImageViews: function(field, view) { | ||
return this.getAllImages(field).map(function (image) { | ||
// Useful for obsolete multiples | ||
getAllImageViews: function(fragment, view) { | ||
return this.getAllImages(fragment).map(function (image) { | ||
return image.getView(view); | ||
@@ -586,10 +689,10 @@ }); | ||
/** | ||
* Gets the date field in the current Document object, for further manipulation. | ||
* Typical use: document.getDate('blog-post.publicationdate').asHtml(ctx.link_resolver) | ||
* Gets the date fragment in the current Document object, for further manipulation. | ||
* Typical use: document.getDate('blog-post.publicationdate').asHtml(ctx) | ||
* | ||
* @param {string} field - The name of the field to get, with its type; for instance, "blog-post.publicationdate" | ||
* @param {string} fragment - The name of the fragment to get, with its type; for instance, "blog-post.publicationdate" | ||
* @returns {Date} - The Date object to manipulate | ||
*/ | ||
getDate: function(field) { | ||
var fragment = this.get(field); | ||
getDate: function(fragment) { | ||
var fragment = this.get(fragment); | ||
@@ -602,11 +705,11 @@ if(fragment instanceof Global.Prismic.Fragments.Date) { | ||
/** | ||
* Gets the boolean field in the current Document object, for further manipulation. | ||
* Typical use: document.getBoolean('blog-post.enableComments').asHtml(ctx.link_resolver). | ||
* This works great with a Select field. The Select values that are considered true are: 'yes', 'on', and 'true'. | ||
* Gets a boolean value of the fragment in the current Document object, for further manipulation. | ||
* Typical use: document.getBoolean('blog-post.enableComments').asHtml(ctx). | ||
* This works great with a Select fragment. The Select values that are considered true are (lowercased before matching): 'yes', 'on', and 'true'. | ||
* | ||
* @param {string} field - The name of the field to get, with its type; for instance, "blog-post.enableComments" | ||
* @param {string} fragment - The name of the fragment to get, with its type; for instance, "blog-post.enableComments" | ||
* @returns {boolean} | ||
*/ | ||
getBoolean: function(field) { | ||
var fragment = this.get(field); | ||
getBoolean: function(fragment) { | ||
var fragment = this.get(fragment); | ||
return fragment.value && (fragment.value.toLowerCase() == 'yes' || fragment.value.toLowerCase() == 'on' || fragment.value.toLowerCase() == 'true'); | ||
@@ -616,11 +719,12 @@ }, | ||
/** | ||
* Gets the text field in the current Document object, for further manipulation. | ||
* Typical use: document.getText('blog-post.label').asHtml(ctx.link_resolver). | ||
* The method works with StructuredText fields, Text fields, Number fields, Select fields and Color fields. | ||
* Gets the text fragment in the current Document object, for further manipulation. | ||
* Typical use: document.getText('blog-post.label').asHtml(ctx). | ||
* The method works with StructuredText fragments, Text fragments, Number fragments, Select fragments and Color fragments. | ||
* | ||
* @param {string} field - The name of the field to get, with its type; for instance, "blog-post.label" | ||
* @param {string} fragment - The name of the fragment to get, with its type; for instance, "blog-post.label" | ||
* @param {string} after - a suffix that will be appended to the value | ||
* @returns {object} - either StructuredText, or Text, or Number, or Select, or Color. | ||
*/ | ||
getText: function(field, after) { | ||
var fragment = this.get(field); | ||
getText: function(fragmentName, after) { | ||
var fragment = this.get(fragmentName); | ||
@@ -661,10 +765,10 @@ if (fragment instanceof Global.Prismic.Fragments.StructuredText) { | ||
/** | ||
* Gets the StructuredText field in the current Document object, for further manipulation. | ||
* Typical use: document.getStructuredText('blog-post.body').asHtml(ctx.link_resolver). | ||
* Gets the StructuredText fragment in the current Document object, for further manipulation. | ||
* Typical use: document.getStructuredText('blog-post.body').asHtml(ctx). | ||
* | ||
* @param {string} field - The name of the field to get, with its type; for instance, "blog-post.body" | ||
* @returns {StructuredText} - The StructuredText field to manipulate. | ||
* @param {string} fragment - The name of the fragment to get, with its type; for instance, "blog-post.body" | ||
* @returns {StructuredText} - The StructuredText fragment to manipulate. | ||
*/ | ||
getStructuredText: function(field) { | ||
var fragment = this.get(field); | ||
getStructuredText: function(fragment) { | ||
var fragment = this.get(fragment); | ||
@@ -677,10 +781,10 @@ if (fragment instanceof Global.Prismic.Fragments.StructuredText) { | ||
/** | ||
* Gets the Number field in the current Document object, for further manipulation. | ||
* Typical use: document.getNumber('product.price').asHtml(ctx.link_resolver). | ||
* Gets the Number fragment in the current Document object, for further manipulation. | ||
* Typical use: document.getNumber('product.price').asHtml(ctx). | ||
* | ||
* @param {string} field - The name of the field to get, with its type; for instance, "product.price" | ||
* @returns {Number} - The Number field to manipulate. | ||
* @param {string} fragment - The name of the fragment to get, with its type; for instance, "product.price" | ||
* @returns {Number} - The Number fragment to manipulate. | ||
*/ | ||
getNumber: function(field) { | ||
var fragment = this.get(field); | ||
getNumber: function(fragment) { | ||
var fragment = this.get(fragment); | ||
@@ -693,11 +797,26 @@ if (fragment instanceof Global.Prismic.Fragments.Number) { | ||
/** | ||
* Shortcut to get the HTML output of the field in the current document. | ||
* This is the same as writing document.get(field).asHtml(linkResolver); | ||
* Gets the Group fragment in the current Document object, for further manipulation. | ||
* Typical use: document.getGroup('product.gallery').asHtml(ctx). | ||
* | ||
* @param {string} field - The name of the field to get, with its type; for instance, "blog-post.body" | ||
* @param {function} ctx - The ctx object that contains the context: ctx.api, ctx.ref, ctx.maybeRef, ctx.oauth(), et ctx.linkResolver() | ||
* @param {string} fragment - The name of the fragment to get, with its type; for instance, "product.gallery" | ||
* @returns {Group} - The Group fragment to manipulate. | ||
*/ | ||
getGroup: function(fragment) { | ||
var fragment = this.get(fragment); | ||
if (fragment instanceof Global.Prismic.Fragments.Group) { | ||
return fragment; | ||
} | ||
}, | ||
/** | ||
* Shortcut to get the HTML output of the fragment in the current document. | ||
* This is the same as writing document.get(fragment).asHtml(ctx); | ||
* | ||
* @param {string} fragment - The name of the fragment to get, with its type; for instance, "blog-post.body" | ||
* @param {function} ctx - The ctx object that contains the context: ctx.api, ctx.ref, ctx.maybeRef, ctx.oauth(), and ctx.linkResolver() | ||
* @returns {string} - The HTML output | ||
*/ | ||
getHtml: function(field, ctx) { | ||
var fragment = this.get(field); | ||
getHtml: function(fragment, ctx) { | ||
var fragment = this.get(fragment); | ||
@@ -710,6 +829,6 @@ if(fragment && fragment.asHtml) { | ||
/** | ||
* Transforms the whole document as an HTML output. Each field is separated by a <section> tag, | ||
* with the attribute data-field="nameoffield" | ||
* Transforms the whole document as an HTML output. Each fragment is separated by a <section> tag, | ||
* with the attribute data-field="nameoffragment" | ||
* | ||
* @param {object} ctx - The ctx object that contains the context: ctx.api, ctx.ref, ctx.maybeRef, ctx.oauth(), et ctx.linkResolver() | ||
* @param {object} ctx - The ctx object that contains the context: ctx.api, ctx.ref, ctx.maybeRef, ctx.oauth(), and ctx.linkResolver() | ||
* @returns {string} - The HTML output | ||
@@ -729,7 +848,21 @@ */ | ||
/** | ||
* Embodies a prismic.io ref (a past or future point in time you can query ) | ||
* Embodies a prismic.io ref (a past or future point in time you can query) | ||
* @constructor | ||
* @global | ||
*/ | ||
function Ref(ref, label, isMaster) { | ||
/** | ||
* @field | ||
* @description the ID of the ref | ||
*/ | ||
this.ref = ref; | ||
/** | ||
* @field | ||
* @description the label of the ref | ||
*/ | ||
this.label = label; | ||
/** | ||
* @field | ||
* @description is true if the ref is the master ref | ||
*/ | ||
this.isMaster = isMaster; | ||
@@ -753,2 +886,5 @@ } | ||
* Embodies a plain text fragment (beware: not a structured text) | ||
* @constructor | ||
* @global | ||
* @alias Fragments:Text | ||
*/ | ||
@@ -772,8 +908,20 @@ function Text(data) { | ||
* Embodies a document link fragment (a link that is internal to a prismic.io repository) | ||
* @constructor | ||
* @global | ||
* @alias Fragments:DocumentLink | ||
*/ | ||
function DocumentLink(data) { | ||
this.value = data; | ||
/** | ||
* @field | ||
* @description the document link's JSON object, exactly as is returned in the JSON responses (see API documentation: https://developers.prismic.io/documentation/UjBe8bGIJ3EKtgBZ/api-documentation#json-responses) | ||
*/ | ||
this.document = data.document; | ||
/** | ||
* @field | ||
* @description true if the link is broken, false otherwise | ||
*/ | ||
this.isBroken = data.isBroken; | ||
} | ||
DocumentLink.prototype = { | ||
@@ -792,3 +940,3 @@ /** | ||
* Returns the URL of the document link. | ||
* | ||
* | ||
* @params {object} ctx - mandatory ctx object, with a useable linkResolver function (please read prismic.io online documentation about this) | ||
@@ -804,4 +952,11 @@ * @returns {string} - the proper URL to use | ||
* Embodies a web link fragment | ||
* @constructor | ||
* @global | ||
* @alias Fragments:WebLink | ||
*/ | ||
function WebLink(data) { | ||
/** | ||
* @field | ||
* @description the JSON object exactly as is returned in the "data" field of the JSON responses (see API documentation: https://developers.prismic.io/documentation/UjBe8bGIJ3EKtgBZ/api-documentation#json-responses) | ||
*/ | ||
this.value = data; | ||
@@ -821,3 +976,3 @@ } | ||
* Returns the URL of the link. | ||
* | ||
* | ||
* @returns {string} - the proper URL to use | ||
@@ -832,4 +987,11 @@ */ | ||
* Embodies a file link fragment | ||
* @constructor | ||
* @global | ||
* @alias Fragments:FileLink | ||
*/ | ||
function FileLink(data) { | ||
/** | ||
* @field | ||
* @description the JSON object exactly as is returned in the "data" field of the JSON responses (see API documentation: https://developers.prismic.io/documentation/UjBe8bGIJ3EKtgBZ/api-documentation#json-responses) | ||
*/ | ||
this.value = data; | ||
@@ -849,3 +1011,3 @@ } | ||
* Returns the URL of the link. | ||
* | ||
* | ||
* @returns {string} - the proper URL to use | ||
@@ -860,4 +1022,11 @@ */ | ||
* Embodies an image link fragment | ||
* @constructor | ||
* @global | ||
* @alias Fragments:ImageLink | ||
*/ | ||
function ImageLink(data) { | ||
/** | ||
* @field | ||
* @description the JSON object exactly as is returned in the "data" field of the JSON responses (see API documentation: https://developers.prismic.io/documentation/UjBe8bGIJ3EKtgBZ/api-documentation#json-responses) | ||
*/ | ||
this.value = data; | ||
@@ -877,3 +1046,3 @@ } | ||
* Returns the URL of the link. | ||
* | ||
* | ||
* @returns {string} - the proper URL to use | ||
@@ -888,4 +1057,11 @@ */ | ||
* Embodies a select fragment | ||
* @constructor | ||
* @global | ||
* @alias Fragments:Select | ||
*/ | ||
function Select(data) { | ||
/** | ||
* @field | ||
* @description the text value of the fragment | ||
*/ | ||
this.value = data; | ||
@@ -907,4 +1083,11 @@ } | ||
* Embodies a color fragment | ||
* @constructor | ||
* @global | ||
* @alias Fragments:Color | ||
*/ | ||
function Color(data) { | ||
/** | ||
* @field | ||
* @description the text value of the fragment | ||
*/ | ||
this.value = data; | ||
@@ -926,4 +1109,11 @@ } | ||
* Embodies a Number fragment | ||
* @constructor | ||
* @global | ||
* @alias Fragments:Num | ||
*/ | ||
function Num(data) { | ||
/** | ||
* @field | ||
* @description the integer value of the fragment | ||
*/ | ||
this.value = data; | ||
@@ -945,4 +1135,11 @@ } | ||
* Embodies a DateTime fragment | ||
* @constructor | ||
* @global | ||
* @alias Fragments:DateTime | ||
*/ | ||
function DateTime(data) { | ||
/** | ||
* @field | ||
* @description the Date value of the fragment (as a regular JS Date object) | ||
*/ | ||
this.value = new Date(data); | ||
@@ -968,4 +1165,11 @@ } | ||
* Embodies an embed fragment | ||
* @constructor | ||
* @global | ||
* @alias Fragments:Embed | ||
*/ | ||
function Embed(data) { | ||
/** | ||
* @field | ||
* @description the JSON object exactly as is returned in the "data" field of the JSON responses (see API documentation: https://developers.prismic.io/documentation/UjBe8bGIJ3EKtgBZ/api-documentation#json-responses) | ||
*/ | ||
this.value = data; | ||
@@ -982,3 +1186,3 @@ } | ||
asHtml: function () { | ||
return this.oembed.html; | ||
return this.value.oembed.html; | ||
} | ||
@@ -989,5 +1193,16 @@ }; | ||
* Embodies an Image fragment | ||
* @constructor | ||
* @global | ||
* @alias Fragments:ImageEl | ||
*/ | ||
function ImageEl(main, views) { | ||
/** | ||
* @field | ||
* @description the main ImageView for this image | ||
*/ | ||
this.main = main; | ||
/** | ||
* @field | ||
* @description an array of all the other ImageViews for this image | ||
*/ | ||
this.views = views || {}; | ||
@@ -1022,6 +1237,21 @@ } | ||
* Embodies an image view (an image in prismic.io can be defined with several different thumbnail sizes, each size is called a "view") | ||
* @constructor | ||
* @global | ||
* @alias Fragments:ImageView | ||
*/ | ||
function ImageView(url, width, height) { | ||
/** | ||
* @field | ||
* @description the URL of the ImageView (useable as it, in a <img> tag in HTML, for instance) | ||
*/ | ||
this.url = url; | ||
/** | ||
* @field | ||
* @description the width of the ImageView | ||
*/ | ||
this.width = width; | ||
/** | ||
* @field | ||
* @description the height of the ImageView | ||
*/ | ||
this.height = height; | ||
@@ -1044,3 +1274,48 @@ } | ||
function Group(tag, blocks) { | ||
/** | ||
* Embodies a fragment of type "Group" (which is a group of subfragments) | ||
* @constructor | ||
* @global | ||
* @alias Fragments:Group | ||
*/ | ||
function Group(data) { | ||
this.value = data; | ||
} | ||
Group.prototype = { | ||
/** | ||
* Turns the fragment into a useable HTML version of it. | ||
* If the native HTML code doesn't suit your design, this function is meant to be overriden. | ||
* @params {object} ctx - mandatory ctx object, with a useable linkResolver function (please read prismic.io online documentation about this) | ||
* @returns {string} - basic HTML code for the fragment | ||
*/ | ||
asHtml: function(ctx) { | ||
var output = ""; | ||
for (var i=0; i<this.value.length; i++) { | ||
for (var fragmentName in this.value[i]) { | ||
output += '<section data-field="'+fragmentName+'">'; | ||
output += this.value[i][fragmentName].asHtml(ctx); | ||
output += '</section>'; | ||
} | ||
} | ||
return output; | ||
}, | ||
/** | ||
* Turns the Group fragment into an array in order to access its items (groups of fragments), | ||
* or to loop through them. | ||
* @params {object} ctx - mandatory ctx object, with a useable linkResolver function (please read prismic.io online documentation about this) | ||
* @returns {array} - the array of groups, each group being a JSON object with subfragment name as keys, and subfragment as values | ||
*/ | ||
toArray: function(){ | ||
return this.value; | ||
} | ||
} | ||
/** | ||
* Embodies a group of text blocks in a structured text fragment, like a group of list items. | ||
* This is only used in the serialization into HTML of structured text fragments. | ||
* @constructor | ||
* @private | ||
*/ | ||
function BlockGroup(tag, blocks) { | ||
this.tag = tag; | ||
@@ -1052,2 +1327,5 @@ this.blocks = blocks; | ||
* Embodies a structured text fragment | ||
* @constructor | ||
* @global | ||
* @alias Fragments:StructuredText | ||
*/ | ||
@@ -1111,3 +1389,3 @@ function StructuredText(blocks) { | ||
* If the native HTML code doesn't suit your design, this function is meant to be overriden. | ||
* | ||
* @params {object} ctx - mandatory ctx object, with a useable linkResolver function (please read prismic.io online documentation about this) | ||
* @returns {string} - basic HTML code for the fragment | ||
@@ -1124,2 +1402,3 @@ */ | ||
* | ||
* @private | ||
* @param {array} blocks - the array of blocks to deal with | ||
@@ -1131,4 +1410,4 @@ * @param {object} ctx - the context object, containing the linkResolver function to build links that may be in the fragment (please read prismic.io's online documentation about this) | ||
var groups = [], | ||
group, | ||
var blockGroups = [], | ||
blockGroup, | ||
block, | ||
@@ -1142,45 +1421,48 @@ html = []; | ||
if (block.type != "list-item" && block.type != "o-list-item") { // it's not a type that groups | ||
group = new Group(block.type, []); | ||
groups.push(group); | ||
blockGroup = new BlockGroup(block.type, []); | ||
blockGroups.push(blockGroup); | ||
} | ||
else if (group && group.tag != block.type) { // it's a new type | ||
group = new Group(block.type, []); | ||
groups.push(group); | ||
else if (blockGroup && blockGroup.tag != block.type) { // it's a new type | ||
blockGroup = new BlockGroup(block.type, []); | ||
blockGroups.push(blockGroup); | ||
} | ||
// else: it's the same type as before, no touching group | ||
group.blocks.push(block); | ||
blockGroup.blocks.push(block); | ||
}; | ||
groups.forEach(function (group) { | ||
blockGroups.forEach(function (blockGroup) { | ||
if(group.tag == "heading1") { | ||
html.push('<h1>' + insertSpans(group.blocks[0].text, group.blocks[0].spans, ctx) + '</h1>'); | ||
if(blockGroup.tag == "heading1") { | ||
html.push('<h1>' + insertSpans(blockGroup.blocks[0].text, blockGroup.blocks[0].spans, ctx) + '</h1>'); | ||
} | ||
else if(group.tag == "heading2") { | ||
html.push('<h2>' + insertSpans(group.blocks[0].text, group.blocks[0].spans, ctx) + '</h2>'); | ||
else if(blockGroup.tag == "heading2") { | ||
html.push('<h2>' + insertSpans(blockGroup.blocks[0].text, blockGroup.blocks[0].spans, ctx) + '</h2>'); | ||
} | ||
else if(group.tag == "heading3") { | ||
html.push('<h3>' + insertSpans(group.blocks[0].text, group.blocks[0].spans, ctx) + '</h3>'); | ||
else if(blockGroup.tag == "heading3") { | ||
html.push('<h3>' + insertSpans(blockGroup.blocks[0].text, blockGroup.blocks[0].spans, ctx) + '</h3>'); | ||
} | ||
else if(group.tag == "paragraph") { | ||
html.push('<p>' + insertSpans(group.blocks[0].text, group.blocks[0].spans, ctx) + '</p>'); | ||
else if(blockGroup.tag == "paragraph") { | ||
html.push('<p>' + insertSpans(blockGroup.blocks[0].text, blockGroup.blocks[0].spans, ctx) + '</p>'); | ||
} | ||
else if(group.tag == "image") { | ||
html.push('<p><img src="' + group.blocks[0].url + '"></p>'); | ||
else if(blockGroup.tag == "preformatted") { | ||
html.push('<pre>' + blockGroup.blocks[0].text + '</pre>'); | ||
} | ||
else if(group.tag == "embed") { | ||
html.push('<div data-oembed="'+ group.blocks[0].embed_url | ||
+ '" data-oembed-type="'+ group.blocks[0].type | ||
+ '" data-oembed-provider="'+ group.blocks[0].provider_name | ||
+ '">' + group.blocks[0].oembed.html+"</div>") | ||
else if(blockGroup.tag == "image") { | ||
html.push('<p><img src="' + blockGroup.blocks[0].url + '"></p>'); | ||
} | ||
else if(group.tag == "list-item" || group.tag == "o-list-item") { | ||
html.push(group.tag == "list-item"?'<ul>':"<ol>"); | ||
group.blocks.forEach(function(block){ | ||
else if(blockGroup.tag == "embed") { | ||
html.push('<div data-oembed="'+ blockGroup.blocks[0].embed_url | ||
+ '" data-oembed-type="'+ blockGroup.blocks[0].type | ||
+ '" data-oembed-provider="'+ blockGroup.blocks[0].provider_name | ||
+ '">' + blockGroup.blocks[0].oembed.html+"</div>") | ||
} | ||
else if(blockGroup.tag == "list-item" || blockGroup.tag == "o-list-item") { | ||
html.push(blockGroup.tag == "list-item"?'<ul>':"<ol>"); | ||
blockGroup.blocks.forEach(function(block){ | ||
html.push("<li>"+insertSpans(block.text, block.spans, ctx)+"</li>"); | ||
}); | ||
html.push(group.tag == "list-item"?'</ul>':"</ol>"); | ||
html.push(blockGroup.tag == "list-item"?'</ul>':"</ol>"); | ||
} | ||
else throw new Error(group.tag+" not implemented"); | ||
else throw new Error(blockGroup.tag+" not implemented"); | ||
}); | ||
@@ -1197,2 +1479,3 @@ | ||
* | ||
* @private | ||
* @param {string} text - the original text of the block | ||
@@ -1250,2 +1533,3 @@ * @param {object} span - the spans as returned by the API | ||
* | ||
* @private | ||
* @param {string} field - the fragment's name | ||
@@ -1325,4 +1609,18 @@ * @returns {object} - the object of the proper Fragments type. | ||
case "Group": | ||
var groups_array = []; | ||
// for each array of groups | ||
for (var i = 0; i < field.value.length; i++) { | ||
var group = {}; // recreate groups with... | ||
for (var fragmentName in field.value[i]) { | ||
// ... the same fragment name as keys, but reinitalized fragments as values | ||
group[fragmentName] = initField(field.value[i][fragmentName]); | ||
} | ||
groups_array.push(group); | ||
} | ||
output = new Group(groups_array); | ||
break; | ||
default: | ||
console.log("Link type not supported: ", field.type); | ||
console.log("Fragment type not supported: ", field.type); | ||
break; | ||
@@ -1348,2 +1646,3 @@ } | ||
FileLink: FileLink, | ||
Group: Group, | ||
initField: initField | ||
@@ -1350,0 +1649,0 @@ } |
@@ -1,2 +0,2 @@ | ||
/*! prismic.io 1.0.9 2014-02-19 */ | ||
!function(a,b){"use strict";function c(a,b,c,d,e,f){this.name=a,this.fields=b,this.form_method=c,this.rel=d,this.enctype=e,this.action=f}function d(a,b,c){this.api=a,this.form=b,this.data=c||{};for(var d in b.fields)b.fields[d].default&&(this.data[d]=[b.fields[d].default])}function e(a){return this.fragments&&this.fragments[a]?Array.isArray(this.fragments[a])?this.fragments[a]:[this.fragments[a]]:[]}function f(a,b,c,d,e,f,g,h){this.page=a,this.results_per_page=b,this.results_size=c,this.total_results_size=d,this.total_pages=e,this.next_page=f,this.prev_page=g,this.results=h}function g(a,b,c,d,e,f){this.id=a,this.type=b,this.href=c,this.tags=d,this.slug=e?e[0]:"-",this.slugs=e,this.fragments=f}function h(a,b,c){this.ref=a,this.label=b,this.isMaster=c}var i=function(a,b,c,d){var e=new i.fn.init(a,c,d);return b&&e.get(b),e},j=function(){return"undefined"!=typeof XMLHttpRequest?function(a,b){var c=new XMLHttpRequest,d=function(){b(null,JSON.parse(c.responseText))},e=function(){var d=c.status;b(new Error("Unexpected status code ["+d+"] on URL "+a))};c.onreadystatechange=function(){4===c.readyState&&(c.status&&200==c.status?d(c):e(c))},c.open("GET",a+"#json",!0),c.setRequestHeader("Accept","application/json"),c.send()}:void 0},k=function(){if("function"==typeof require&&require("http")){{var a={},c=require("http"),d=require("https"),e=require("url");require("querystring")}return function(f,g){if(a[f])g(null,a[f]);else{var h=e.parse(f),i="https:"==h.protocol?d:c,j={hostname:h.hostname,path:h.path,query:h.query,headers:{Accept:"application/json"}};i.get(j,function(c){if(c.statusCode&&200==c.statusCode){var d="";c.setEncoding("utf8"),c.on("data",function(a){d+=a}),c.on("end",function(){var e=c.headers["cache-control"],h=e&&/max-age=(\d+)/.test(e)?parseInt(/max-age=(\d+)/.exec(e)[1]):b,i=JSON.parse(d);h&&(a[f]=i),g(null,i)})}else g(new Error("Unexpected status code ["+c.statusCode+"] on URL "+f))})}}}};i.fn=i.prototype={constructor:i,data:null,get:function(a){var b=this;this.requestHandler(this.url,function(c,d){c?a(c):(b.data=b.parse(d),b.bookmarks=b.data.bookmarks,a(null,b,this))})},parse:function(a){var b,d,e,f,g,i,j,k={};for(j in a.forms)a.forms.hasOwnProperty(j)&&(i=a.forms[j],this.accessToken&&(i.fields.accessToken={type:"string","default":this.accessToken}),e=new c(i.name,i.fields,i.form_method,i.rel,i.enctype,i.action),k[j]=e);if(b=a.refs.map(function(a){return new h(a.ref,a.label,a.isMasterRef)})||[],d=b.filter(function(a){return a.isMaster===!0}),f=a.types,g=a.tags,0===d.length)throw"No master ref.";return{bookmarks:a.bookmarks||{},refs:b,forms:k,master:d[0],types:f,tags:g,oauthInitiate:a.oauth_initiate,oauthToken:a.oauth_token}},init:function(a,b,c){return this.url=a+(b?(a.indexOf("?")>-1?"&":"?")+"access_token="+b:""),this.accessToken=b,this.requestHandler=c||j()||k()||function(){throw new Error("No request handler available (tried XMLHttpRequest & NodeJS)")}(),this},forms:function(a){return this.form(a)},form:function(a){var b=this.data.forms[a];return b?new d(this,b,{}):void 0},master:function(){return this.data.master.ref},ref:function(a){for(var b=0;b<this.data.refs.length;b++)if(this.data.refs[b].label==a)return this.data.refs[b].ref}},i.fn.init.prototype=i.fn,c.prototype={},d.prototype={set:function(a,c){var d=this.form.fields[a];if(!d)throw new Error("Unknown field "+a);var e=this.data[a]||[];return(""===c||c===b)&&(c=null),d.multiple?null!=c&&e.push(c):e=null!=c&&[c],this.data[a]=e,this},ref:function(a){return this.set("ref",a)},query:function(a){return this.set("q",a)},submit:function(a){var b=this.form.action;if(this.data){var c=b.indexOf("?")>-1?"&":"?";for(var d in this.data){var e=this.data[d];if(e)for(var h=0;h<e.length;h++)b+=c+d+"="+encodeURIComponent(e[h]),c="&"}}this.api.requestHandler(b,function(b,c){if(b)return a(b),void 0;var d=c.results.map(function(a){var b={};for(var c in a.data[a.type])b[a.type+"."+c]=a.data[a.type][c];return new g(a.id,a.type,a.href,a.tags,a.slugs,b)});a(null,new f(c.page,c.results_per_page,c.results_size,c.total_results_size,c.total_pages,c.next_page,c.prev_page,d||[]))})}},g.prototype={get:function(b){var c=e.call(this,b);return c.length?a.Prismic.Fragments.initField(c[0]):null},getAll:function(b){return e.call(this,b).map(function(b){return a.Prismic.Fragments.initField(b)},this)},getImage:function(b){var c=this.get(b);return c instanceof a.Prismic.Fragments.Image?c:c instanceof a.Prismic.Fragments.StructuredText?c:null},getAllImages:function(b){var c=this.getAll(b);return c.map(function(b){if(b instanceof a.Prismic.Fragments.Image)return b;if(b instanceof a.Prismic.Fragments.StructuredText)throw new Error("Not done.");return null})},getImageView:function(b,c){var d=this.get(b);if(d instanceof a.Prismic.Fragments.Image)return d.getView(c);if(d instanceof a.Prismic.Fragments.StructuredText)for(var e=0;e<d.blocks.length;e++)if("image"==d.blocks[e].type)return d.blocks[e];return null},getAllImageViews:function(a,b){return this.getAllImages(a).map(function(a){return a.getView(b)})},getDate:function(b){var c=this.get(b);return c instanceof a.Prismic.Fragments.Date?c.value:void 0},getBoolean:function(a){var b=this.get(a);return b.value&&("yes"==b.value.toLowerCase()||"on"==b.value.toLowerCase()||"true"==b.value.toLowerCase())},getText:function(b,c){var d=this.get(b);return d instanceof a.Prismic.Fragments.StructuredText?d.blocks.map(function(a){return a.text?a.text+(c?c:""):void 0}).join("\n"):d instanceof a.Prismic.Fragments.Text&&d.value?d.value+(c?c:""):d instanceof a.Prismic.Fragments.Number&&d.value?d.value+(c?c:""):d instanceof a.Prismic.Fragments.Select&&d.value?d.value+(c?c:""):d instanceof a.Prismic.Fragments.Color&&d.value?d.value+(c?c:""):void 0},getStructuredText:function(b){var c=this.get(b);return c instanceof a.Prismic.Fragments.StructuredText?c:void 0},getNumber:function(b){var c=this.get(b);return c instanceof a.Prismic.Fragments.Number?c.value:void 0},getHtml:function(a,b){var c=this.get(a);return c&&c.asHtml?c.asHtml(b):void 0},asHtml:function(a){var b=[];for(var c in this.fragments){var d=this.get(c);b.push(d&&d.asHtml?'<section data-field="'+c+'">'+d.asHtml(a)+"</section>":"")}return b.join("")}},h.prototype={},a.Prismic={Api:i}}("object"==typeof exports&&exports?exports:"object"==typeof module&&module&&"object"==typeof module.exports?module.exports:window),function(a){"use strict";function b(a){this.value=a}function c(a){this.value=a,this.document=a.document,this.isBroken=a.isBroken}function d(a){this.value=a}function e(a){this.value=a}function f(a){this.value=a}function g(a){this.value=a}function h(a){this.value=a}function i(a){this.value=a}function j(a){this.value=new Date(a)}function k(a){this.value=a}function l(a,b){this.main=a,this.views=b||{}}function m(a,b,c){this.url=a,this.width=b,this.height=c}function n(a,b){this.tag=a,this.blocks=b}function o(a){this.blocks=a}function p(a,b){var c,d,e=[],f=[];if(Array.isArray(a)){for(var g=0;g<a.length;g++)d=a[g],"list-item"!=d.type&&"o-list-item"!=d.type?(c=new n(d.type,[]),e.push(c)):c&&c.tag!=d.type&&(c=new n(d.type,[]),e.push(c)),c.blocks.push(d);e.forEach(function(a){if("heading1"==a.tag)f.push("<h1>"+q(a.blocks[0].text,a.blocks[0].spans,b)+"</h1>");else if("heading2"==a.tag)f.push("<h2>"+q(a.blocks[0].text,a.blocks[0].spans,b)+"</h2>");else if("heading3"==a.tag)f.push("<h3>"+q(a.blocks[0].text,a.blocks[0].spans,b)+"</h3>");else if("paragraph"==a.tag)f.push("<p>"+q(a.blocks[0].text,a.blocks[0].spans,b)+"</p>");else if("image"==a.tag)f.push('<p><img src="'+a.blocks[0].url+'"></p>');else if("embed"==a.tag)f.push('<div data-oembed="'+a.blocks[0].embed_url+'" data-oembed-type="'+a.blocks[0].type+'" data-oembed-provider="'+a.blocks[0].provider_name+'">'+a.blocks[0].oembed.html+"</div>");else{if("list-item"!=a.tag&&"o-list-item"!=a.tag)throw new Error(a.tag+" not implemented");f.push("list-item"==a.tag?"<ul>":"<ol>"),a.blocks.forEach(function(a){f.push("<li>"+q(a.text,a.spans,b)+"</li>")}),f.push("list-item"==a.tag?"</ul>":"</ol>")}})}return f.join("")}function q(a,b,c){var d=[],e=[],f=0,g=[];return b.forEach(function(b){return b.end<b.start?a:b.start<f?a:(f=b.end,void 0)}),f=0,b.forEach(function(b){d.push(a.substring(0,b.start-f)),a=a.substring(b.start-f),f=b.start,d.push(a.substring(0,b.end-f)),a=a.substring(b.end-f),e.push(b),f=b.end}),d.push(a),e.forEach(function(a){g.push(d.shift()),"hyperlink"==a.type?(g.push('<a href="'+r(a.data).url(c)+'">'),g.push(d.shift()),g.push("</a>")):(g.push("<"+a.type+">"),g.push(d.shift()),g.push("</"+a.type+">"))}),g.push(d.shift()),g.join("")}function r(a){var n,p;switch(a.type){case"Color":n=new h(a.value);break;case"Number":n=new i(a.value);break;case"Date":n=new j(a.value);break;case"Text":n=new b(a.value);break;case"Embed":n=new k(a.value);break;case"Select":n=new g(a.value);break;case"Image":var p=a.value.main;n=new l(new m(p.url,p.dimensions.width,p.dimensions.height),{});for(var q in a.value.views){var p=a.value.views[q];n.views[q]=new m(p.url,p.dimensions.width,p.dimensions.height)}break;case"StructuredText":n=new o(a.value);break;case"Link.document":n=new c(a.value);break;case"Link.web":n=new d(a.value);break;case"Link.file":n=new e(a.value);break;case"Link.image":n=new f(a.value);break;default:console.log("Link type not supported: ",a.type)}return n}b.prototype={asHtml:function(){return"<span>"+this.value+"</span>"}},c.prototype={asHtml:function(a){return'<a href="'+this.url(a)+'">'+this.url(a)+"</a>"},url:function(a){return a.linkResolver(a,this.document,this.isBroken)}},d.prototype={asHtml:function(){return'<a href="'+this.url()+'">'+this.url()+"</a>"},url:function(){return this.value.url}},e.prototype={asHtml:function(){return'<a href="'+this.url()+'">'+this.value.file.name+"</a>"},url:function(){return this.value.file.url}},f.prototype={asHtml:function(){return'<a href="'+this.url()+'"><img src="'+this.url()+'"</a>'},url:function(){return this.value.image.url}},g.prototype={asHtml:function(){return"<span>"+this.value+"</span>"}},h.prototype={asHtml:function(){return"<span>"+this.value+"</span>"}},i.prototype={asHtml:function(){return"<span>"+this.value+"</span>"}},j.prototype={asText:function(){throw new Error("not implemented")},asHtml:function(){return"<time>"+this.value+"</time>"}},k.prototype={asHtml:function(){return this.oembed.html}},l.prototype={getView:function(a){return"main"===a?this.main:this.views[a]},asHtml:function(){return this.main.asHtml()}},m.prototype={ratio:function(){return this.width/this.height},asHtml:function(){return"<img src="+this.url+" width="+this.width+" height="+this.height+">"}},o.prototype={getTitle:function(){for(var a=0;a<this.blocks.length;a++){var b=this.blocks[a];if(0==b.type.indexOf("heading"))return b}},getFirstParagraph:function(){for(var a=0;a<this.blocks.length;a++){var b=this.blocks[a];if("paragraph"==b.type)return b}},getParagraphs:function(){for(var a=[],b=0;b<this.blocks.length;b++){var c=this.blocks[b];"paragraph"==c.type&&a.push(c)}return a},getParagraph:function(a){return this.getParagraphs()[a]},getFirstImage:function(){for(var a=0;a<this.blocks.length;a++){var b=this.blocks[a];if("image"==b.type)return new m(b.data.url,b.data.dimensions.width,b.data.dimensions.height)}},asHtml:function(a){return p.call(this,this.blocks,a)}},a.Prismic.Fragments={Image:l,ImageView:m,Text:b,Number:i,Date:j,Select:g,Color:h,StructuredText:o,WebLink:d,DocumentLink:c,ImageLink:f,FileLink:e,initField:r}}("object"==typeof exports&&exports?exports:"object"==typeof module&&module&&"object"==typeof module.exports?module.exports:window); | ||
/*! prismic.io 1.0.9 2014-04-21 */ | ||
!function(a,b){"use strict";function c(a,b,c,d,e,f){this.name=a,this.fields=b,this.form_method=c,this.rel=d,this.enctype=e,this.action=f}function d(a,b,c){this.api=a,this.form=b,this.data=c||{};for(var d in b.fields)b.fields[d].default&&(this.data[d]=[b.fields[d].default])}function e(a){return this.fragments&&this.fragments[a]?Array.isArray(this.fragments[a])?this.fragments[a]:[this.fragments[a]]:[]}function f(a,b,c,d,e,f,g,h){this.page=a,this.results_per_page=b,this.results_size=c,this.total_results_size=d,this.total_pages=e,this.next_page=f,this.prev_page=g,this.results=h}function g(a,b,c,d,e,f){this.id=a,this.type=b,this.href=c,this.tags=d,this.slug=e?e[0]:"-",this.slugs=e,this.fragments=f}function h(a,b,c){this.ref=a,this.label=b,this.isMaster=c}var i=function(a,b,c,d){var e=new i.fn.init(a,c,d);return b&&e.get(b),e},j=function(){return"undefined"!=typeof XMLHttpRequest?function(a,b){var c=new XMLHttpRequest,d=function(){b(null,JSON.parse(c.responseText))},e=function(){var d=c.status;b(new Error("Unexpected status code ["+d+"] on URL "+a))};c.onreadystatechange=function(){4===c.readyState&&(c.status&&200==c.status?d(c):e(c))},c.open("GET",a,!0),c.setRequestHeader("Accept","application/json"),c.send()}:void 0},k=function(){if("function"==typeof require&&require("http")){{var a={},c=require("http"),d=require("https"),e=require("url");require("querystring")}return function(f,g){if(a[f])g(null,a[f]);else{var h=e.parse(f),i="https:"==h.protocol?d:c,j={hostname:h.hostname,path:h.path,query:h.query,headers:{Accept:"application/json"}};i.get(j,function(c){if(c.statusCode&&200==c.statusCode){var d="";c.setEncoding("utf8"),c.on("data",function(a){d+=a}),c.on("end",function(){var e=c.headers["cache-control"],h=e&&/max-age=(\d+)/.test(e)?parseInt(/max-age=(\d+)/.exec(e)[1]):b,i=JSON.parse(d);h&&(a[f]=i),g(null,i)})}else g(new Error("Unexpected status code ["+c.statusCode+"] on URL "+f))})}}}};i.fn=i.prototype={constructor:i,data:null,get:function(a){var b=this;this.requestHandler(this.url,function(c,d){c?a(c):(b.data=b.parse(d),b.bookmarks=b.data.bookmarks,a(null,b,this))})},parse:function(a){var b,d,e,f,g,i,j,k={};for(j in a.forms)a.forms.hasOwnProperty(j)&&(i=a.forms[j],this.accessToken&&(i.fields.accessToken={type:"string","default":this.accessToken}),e=new c(i.name,i.fields,i.form_method,i.rel,i.enctype,i.action),k[j]=e);if(b=a.refs.map(function(a){return new h(a.ref,a.label,a.isMasterRef)})||[],d=b.filter(function(a){return a.isMaster===!0}),f=a.types,g=a.tags,0===d.length)throw"No master ref.";return{bookmarks:a.bookmarks||{},refs:b,forms:k,master:d[0],types:f,tags:g,oauthInitiate:a.oauth_initiate,oauthToken:a.oauth_token}},init:function(a,b,c){return this.url=a+(b?(a.indexOf("?")>-1?"&":"?")+"access_token="+b:""),this.accessToken=b,this.requestHandler=c||j()||k()||function(){throw new Error("No request handler available (tried XMLHttpRequest & NodeJS)")}(),this},forms:function(a){return this.form(a)},form:function(a){var b=this.data.forms[a];return b?new d(this,b,{}):void 0},master:function(){return this.data.master.ref},ref:function(a){for(var b=0;b<this.data.refs.length;b++)if(this.data.refs[b].label==a)return this.data.refs[b].ref}},i.fn.init.prototype=i.fn,c.prototype={},d.prototype={set:function(a,c){var d=this.form.fields[a];if(!d)throw new Error("Unknown field "+a);var e=this.data[a]||[];return(""===c||c===b)&&(c=null),d.multiple?null!=c&&e.push(c):e=null!=c&&[c],this.data[a]=e,this},ref:function(a){return this.set("ref",a)},query:function(a){return this.set("q",a)},pageSize:function(a){return this.set("pageSize",a)},page:function(a){return this.set("page",a)},orderings:function(a){return this.set("orderings",a)},submit:function(a){var b=this.form.action;if(this.data){var c=b.indexOf("?")>-1?"&":"?";for(var d in this.data){var e=this.data[d];if(e)for(var h=0;h<e.length;h++)b+=c+d+"="+encodeURIComponent(e[h]),c="&"}}this.api.requestHandler(b,function(b,c){if(b)return a(b),void 0;var d=c.results.map(function(a){var b={};for(var c in a.data[a.type])b[a.type+"."+c]=a.data[a.type][c];return new g(a.id,a.type,a.href,a.tags,a.slugs,b)});a(null,new f(c.page,c.results_per_page,c.results_size,c.total_results_size,c.total_pages,c.next_page,c.prev_page,d||[]))})}},g.prototype={get:function(b){var c=e.call(this,b);return c.length?a.Prismic.Fragments.initField(c[0]):null},getAll:function(b){return e.call(this,b).map(function(b){return a.Prismic.Fragments.initField(b)},this)},getImage:function(b){var c=this.get(b);return c instanceof a.Prismic.Fragments.Image?c:c instanceof a.Prismic.Fragments.StructuredText?c:null},getAllImages:function(b){var c=this.getAll(b);return c.map(function(b){if(b instanceof a.Prismic.Fragments.Image)return b;if(b instanceof a.Prismic.Fragments.StructuredText)throw new Error("Not done.");return null})},getImageView:function(b,c){var b=this.get(b);if(b instanceof a.Prismic.Fragments.Image)return b.getView(c);if(b instanceof a.Prismic.Fragments.StructuredText)for(var d=0;d<b.blocks.length;d++)if("image"==b.blocks[d].type)return b.blocks[d];return null},getAllImageViews:function(a,b){return this.getAllImages(a).map(function(a){return a.getView(b)})},getDate:function(b){var b=this.get(b);return b instanceof a.Prismic.Fragments.Date?b.value:void 0},getBoolean:function(a){var a=this.get(a);return a.value&&("yes"==a.value.toLowerCase()||"on"==a.value.toLowerCase()||"true"==a.value.toLowerCase())},getText:function(b,c){var d=this.get(b);return d instanceof a.Prismic.Fragments.StructuredText?d.blocks.map(function(a){return a.text?a.text+(c?c:""):void 0}).join("\n"):d instanceof a.Prismic.Fragments.Text&&d.value?d.value+(c?c:""):d instanceof a.Prismic.Fragments.Number&&d.value?d.value+(c?c:""):d instanceof a.Prismic.Fragments.Select&&d.value?d.value+(c?c:""):d instanceof a.Prismic.Fragments.Color&&d.value?d.value+(c?c:""):void 0},getStructuredText:function(b){var b=this.get(b);return b instanceof a.Prismic.Fragments.StructuredText?b:void 0},getNumber:function(b){var b=this.get(b);return b instanceof a.Prismic.Fragments.Number?b.value:void 0},getGroup:function(b){var b=this.get(b);return b instanceof a.Prismic.Fragments.Group?b:void 0},getHtml:function(a,b){var a=this.get(a);return a&&a.asHtml?a.asHtml(b):void 0},asHtml:function(a){var b=[];for(var c in this.fragments){var d=this.get(c);b.push(d&&d.asHtml?'<section data-field="'+c+'">'+d.asHtml(a)+"</section>":"")}return b.join("")}},h.prototype={},a.Prismic={Api:i}}("object"==typeof exports&&exports?exports:"object"==typeof module&&module&&"object"==typeof module.exports?module.exports:window),function(a){"use strict";function b(a){this.value=a}function c(a){this.value=a,this.document=a.document,this.isBroken=a.isBroken}function d(a){this.value=a}function e(a){this.value=a}function f(a){this.value=a}function g(a){this.value=a}function h(a){this.value=a}function i(a){this.value=a}function j(a){this.value=new Date(a)}function k(a){this.value=a}function l(a,b){this.main=a,this.views=b||{}}function m(a,b,c){this.url=a,this.width=b,this.height=c}function n(a){this.value=a}function o(a,b){this.tag=a,this.blocks=b}function p(a){this.blocks=a}function q(a,b){var c,d,e=[],f=[];if(Array.isArray(a)){for(var g=0;g<a.length;g++)d=a[g],"list-item"!=d.type&&"o-list-item"!=d.type?(c=new o(d.type,[]),e.push(c)):c&&c.tag!=d.type&&(c=new o(d.type,[]),e.push(c)),c.blocks.push(d);e.forEach(function(a){if("heading1"==a.tag)f.push("<h1>"+r(a.blocks[0].text,a.blocks[0].spans,b)+"</h1>");else if("heading2"==a.tag)f.push("<h2>"+r(a.blocks[0].text,a.blocks[0].spans,b)+"</h2>");else if("heading3"==a.tag)f.push("<h3>"+r(a.blocks[0].text,a.blocks[0].spans,b)+"</h3>");else if("paragraph"==a.tag)f.push("<p>"+r(a.blocks[0].text,a.blocks[0].spans,b)+"</p>");else if("preformatted"==a.tag)f.push("<pre>"+a.blocks[0].text+"</pre>");else if("image"==a.tag)f.push('<p><img src="'+a.blocks[0].url+'"></p>');else if("embed"==a.tag)f.push('<div data-oembed="'+a.blocks[0].embed_url+'" data-oembed-type="'+a.blocks[0].type+'" data-oembed-provider="'+a.blocks[0].provider_name+'">'+a.blocks[0].oembed.html+"</div>");else{if("list-item"!=a.tag&&"o-list-item"!=a.tag)throw new Error(a.tag+" not implemented");f.push("list-item"==a.tag?"<ul>":"<ol>"),a.blocks.forEach(function(a){f.push("<li>"+r(a.text,a.spans,b)+"</li>")}),f.push("list-item"==a.tag?"</ul>":"</ol>")}})}return f.join("")}function r(a,b,c){var d=[],e=[],f=0,g=[];return b.forEach(function(b){return b.end<b.start?a:b.start<f?a:(f=b.end,void 0)}),f=0,b.forEach(function(b){d.push(a.substring(0,b.start-f)),a=a.substring(b.start-f),f=b.start,d.push(a.substring(0,b.end-f)),a=a.substring(b.end-f),e.push(b),f=b.end}),d.push(a),e.forEach(function(a){g.push(d.shift()),"hyperlink"==a.type?(g.push('<a href="'+s(a.data).url(c)+'">'),g.push(d.shift()),g.push("</a>")):(g.push("<"+a.type+">"),g.push(d.shift()),g.push("</"+a.type+">"))}),g.push(d.shift()),g.join("")}function s(a){var o,q;switch(a.type){case"Color":o=new h(a.value);break;case"Number":o=new i(a.value);break;case"Date":o=new j(a.value);break;case"Text":o=new b(a.value);break;case"Embed":o=new k(a.value);break;case"Select":o=new g(a.value);break;case"Image":var q=a.value.main;o=new l(new m(q.url,q.dimensions.width,q.dimensions.height),{});for(var r in a.value.views){var q=a.value.views[r];o.views[r]=new m(q.url,q.dimensions.width,q.dimensions.height)}break;case"StructuredText":o=new p(a.value);break;case"Link.document":o=new c(a.value);break;case"Link.web":o=new d(a.value);break;case"Link.file":o=new e(a.value);break;case"Link.image":o=new f(a.value);break;case"Group":for(var t=[],u=0;u<a.value.length;u++){var v={};for(var w in a.value[u])v[w]=s(a.value[u][w]);t.push(v)}o=new n(t);break;default:console.log("Fragment type not supported: ",a.type)}return o}b.prototype={asHtml:function(){return"<span>"+this.value+"</span>"}},c.prototype={asHtml:function(a){return'<a href="'+this.url(a)+'">'+this.url(a)+"</a>"},url:function(a){return a.linkResolver(a,this.document,this.isBroken)}},d.prototype={asHtml:function(){return'<a href="'+this.url()+'">'+this.url()+"</a>"},url:function(){return this.value.url}},e.prototype={asHtml:function(){return'<a href="'+this.url()+'">'+this.value.file.name+"</a>"},url:function(){return this.value.file.url}},f.prototype={asHtml:function(){return'<a href="'+this.url()+'"><img src="'+this.url()+'"</a>'},url:function(){return this.value.image.url}},g.prototype={asHtml:function(){return"<span>"+this.value+"</span>"}},h.prototype={asHtml:function(){return"<span>"+this.value+"</span>"}},i.prototype={asHtml:function(){return"<span>"+this.value+"</span>"}},j.prototype={asText:function(){throw new Error("not implemented")},asHtml:function(){return"<time>"+this.value+"</time>"}},k.prototype={asHtml:function(){return this.value.oembed.html}},l.prototype={getView:function(a){return"main"===a?this.main:this.views[a]},asHtml:function(){return this.main.asHtml()}},m.prototype={ratio:function(){return this.width/this.height},asHtml:function(){return"<img src="+this.url+" width="+this.width+" height="+this.height+">"}},n.prototype={asHtml:function(a){for(var b="",c=0;c<this.value.length;c++)for(var d in this.value[c])b+='<section data-field="'+d+'">',b+=this.value[c][d].asHtml(a),b+="</section>";return b},toArray:function(){return this.value}},p.prototype={getTitle:function(){for(var a=0;a<this.blocks.length;a++){var b=this.blocks[a];if(0==b.type.indexOf("heading"))return b}},getFirstParagraph:function(){for(var a=0;a<this.blocks.length;a++){var b=this.blocks[a];if("paragraph"==b.type)return b}},getParagraphs:function(){for(var a=[],b=0;b<this.blocks.length;b++){var c=this.blocks[b];"paragraph"==c.type&&a.push(c)}return a},getParagraph:function(a){return this.getParagraphs()[a]},getFirstImage:function(){for(var a=0;a<this.blocks.length;a++){var b=this.blocks[a];if("image"==b.type)return new m(b.data.url,b.data.dimensions.width,b.data.dimensions.height)}},asHtml:function(a){return q.call(this,this.blocks,a)}},a.Prismic.Fragments={Image:l,ImageView:m,Text:b,Number:i,Date:j,Select:g,Color:h,StructuredText:p,WebLink:d,DocumentLink:c,ImageLink:f,FileLink:e,Group:n,initField:s}}("object"==typeof exports&&exports?exports:"object"==typeof module&&module&&"object"==typeof module.exports?module.exports:window); |
@@ -28,3 +28,3 @@ module.exports = function(grunt) { | ||
clean: { | ||
src: ['dist/prismic.io.js','dist/prismic.io.min.js'] | ||
src: ['dist/prismic.io.js','dist/prismic.io.min.js', 'doc'] | ||
}, | ||
@@ -72,2 +72,11 @@ | ||
jsdoc : { | ||
dist : { | ||
src: ['src/*.js', 'README.md'], | ||
options: { | ||
destination: 'doc' | ||
} | ||
} | ||
} | ||
}); | ||
@@ -82,2 +91,3 @@ | ||
grunt.loadNpmTasks('grunt-bump'); | ||
grunt.loadNpmTasks('grunt-jsdoc'); | ||
@@ -84,0 +94,0 @@ // Default task. |
@@ -5,4 +5,10 @@ { | ||
"url": "https://github.com/prismicio/javascript-kit", | ||
"keywords": ["prismic", "prismic.io", "cms", "content", "api"], | ||
"version": "1.0.9", | ||
"keywords": [ | ||
"prismic", | ||
"prismic.io", | ||
"cms", | ||
"content", | ||
"api" | ||
], | ||
"version": "1.0.10", | ||
"devDependencies": { | ||
@@ -15,6 +21,10 @@ "grunt": "~0.4.1", | ||
"grunt-contrib-copy": "~0.5.0", | ||
"grunt-bump":"~0.0.13" | ||
"grunt-bump": "~0.0.13", | ||
"grunt-jsdoc": "~0.5.4" | ||
}, | ||
"repository" : { "type" : "git", "url" : "http://github.com/prismicio/javascript-kit.git" }, | ||
"repository": { | ||
"type": "git", | ||
"url": "http://github.com/prismicio/javascript-kit.git" | ||
}, | ||
"main": "dist/prismic.io" | ||
} | ||
} |
@@ -7,8 +7,4 @@ ## JavaScript development kit for prismic.io | ||
You can find the minified latest (unstable) version of the library at: | ||
You can find downloadable versions of the kit on our release page: [https://github.com/prismicio/javascript-kit/releases](https://github.com/prismicio/javascript-kit/releases). | ||
``` | ||
https://raw.github.com/prismicio/javascript-kit/master/dist/prismic.io-1.0.9.min.js | ||
``` | ||
You can install a stable version using __npm__: | ||
@@ -15,0 +11,0 @@ |
265
src/api.js
@@ -8,2 +8,5 @@ (function (Global, undefined) { | ||
* | ||
* @global | ||
* @alias Api | ||
* @constructor | ||
* @param {string} url - The mandatory URL of the prismic.io API endpoint (like: https://lesbonneschoses.prismic.io/api) | ||
@@ -20,2 +23,3 @@ * @param {function} callback - Optional callback function that is called after the API was retrieved, to which you may pass two parameters: a potential error (null if no problem), and the API object | ||
}; | ||
// note that the prismic variable is later affected as "Api" while exporting | ||
@@ -53,3 +57,3 @@ // -- Request handlers | ||
// Open the XHR | ||
xhr.open('GET', url + '#json', true); | ||
xhr.open('GET', url, true); | ||
@@ -118,7 +122,3 @@ // Json request | ||
/** | ||
* The Api object you can manipulate, notably to perform queries in your repository. | ||
* Most useful fields: bookmarks, refs, types, tags, ... | ||
*/ | ||
// Defining Api's instance methods; note that the prismic variable is later affected as "Api" while exporting | ||
prismic.fn = prismic.prototype = { | ||
@@ -157,2 +157,3 @@ | ||
* @returns {Api} - The Api object that can be manipulated | ||
* @private | ||
*/ | ||
@@ -230,2 +231,3 @@ parse: function(data) { | ||
* This is for internal use, from outside this kit, you should call Prismic.Api() | ||
* @private | ||
*/ | ||
@@ -241,3 +243,4 @@ init: function(url, accessToken, maybeRequestHandler) { | ||
* @deprecated use form() now | ||
* Returns a useable form from its id, as described in the RESTful description of the API. | ||
* @param {string} formId - The id of a form, like "everything", or "products" | ||
* @returns {SearchForm} - the SearchForm that can be used. | ||
*/ | ||
@@ -253,2 +256,3 @@ forms: function(formId) { | ||
* | ||
* @param {string} formId - The id of a form, like "everything", or "products" | ||
* @returns {SearchForm} - the SearchForm that can be used. | ||
@@ -279,2 +283,3 @@ */ | ||
* | ||
* @param {string} label - the ref's label | ||
* @returns {string} | ||
@@ -296,2 +301,4 @@ */ | ||
* Embodies a submittable RESTful form as described on the API endpoint (as per RESTful standards) | ||
* @constructor | ||
* @private | ||
*/ | ||
@@ -312,2 +319,4 @@ function Form(name, fields, form_method, rel, enctype, action) { | ||
* @constructor | ||
* @global | ||
* @alias SearchForm | ||
*/ | ||
@@ -380,2 +389,32 @@ function SearchForm(api, form, data) { | ||
/** | ||
* Sets a page size to query for this SearchForm. This is an optional method. | ||
* | ||
* @param {number} pageSize - The page size | ||
* @returns {SearchForm} - The SearchForm itself | ||
*/ | ||
pageSize: function(size) { | ||
return this.set("pageSize", size); | ||
}, | ||
/** | ||
* Sets the page number to query for this SearchForm. This is an optional method. | ||
* | ||
* @param {number} page - The page number | ||
* @returns {SearchForm} - The SearchForm itself | ||
*/ | ||
page: function(p) { | ||
return this.set("page", p); | ||
}, | ||
/** | ||
* Sets the orderings to query for this SearchForm. This is an optional method. | ||
* | ||
* @param {string} orderings - The orderings | ||
* @returns {SearchForm} - The SearchForm itself | ||
*/ | ||
orderings: function(orderings) { | ||
return this.set("orderings", orderings); | ||
}, | ||
/** | ||
* Submits the query, and calls the callback function. | ||
@@ -443,3 +482,4 @@ * | ||
* An array of the fragments with the given fragment name. | ||
* The array is often a single-element array, expect when the field is a multiple field. | ||
* The array is often a single-element array, expect when the fragment is a multiple fragment. | ||
* @private | ||
*/ | ||
@@ -463,12 +503,46 @@ function getFragments(name) { | ||
* as well as the field "results", which is an array of Doc objects, the documents themselves. | ||
* @constructor | ||
* @global | ||
*/ | ||
function Documents(page, results_per_page, results_size, total_results_size, total_pages, next_page, prev_page, results) { | ||
/** | ||
* @field | ||
* @description the current page number | ||
*/ | ||
this.page = page; | ||
/** | ||
* @field | ||
* @description the number of results per page | ||
*/ | ||
this.results_per_page = results_per_page; | ||
/** | ||
* @field | ||
* @description the size of the current page | ||
*/ | ||
this.results_size = results_size; | ||
/** | ||
* @field | ||
* @description the total size of results across all pages | ||
*/ | ||
this.total_results_size = total_results_size; | ||
/** | ||
* @field | ||
* @description the total number of pages | ||
*/ | ||
this.total_pages = total_pages; | ||
/** | ||
* @field | ||
* @description the URL of the next page in the API | ||
*/ | ||
this.next_page = next_page; | ||
/** | ||
* @field | ||
* @description the URL of the previous page in the API | ||
*/ | ||
this.prev_page = prev_page; | ||
this.results = results; // an array of Doc objects | ||
/** | ||
* @field | ||
* @description the array of the {Doc} objects | ||
*/ | ||
this.results = results; | ||
} | ||
@@ -479,10 +553,37 @@ | ||
* Most useful fields: id, type, tags, slug, slugs, ... | ||
* @constructor | ||
* @global | ||
* @alias Doc | ||
*/ | ||
function Doc(id, type, href, tags, slugs, fragments) { | ||
/** | ||
* @field | ||
* @description the ID of the document | ||
*/ | ||
this.id = id; | ||
/** | ||
* @field | ||
* @description the type of the document | ||
*/ | ||
this.type = type; | ||
/** | ||
* @field | ||
* @description the URL of the document in the API | ||
*/ | ||
this.href = href; | ||
/** | ||
* @field | ||
* @description the tags of the document | ||
*/ | ||
this.tags = tags; | ||
/** | ||
* @field | ||
* @description the current slug of the document | ||
*/ | ||
this.slug = slugs ? slugs[0] : "-"; | ||
/** | ||
* @field | ||
* @description all the slugs that were ever used by this document (including the current one, at the head) | ||
*/ | ||
this.slugs = slugs; | ||
@@ -494,7 +595,7 @@ this.fragments = fragments; | ||
/** | ||
* Gets the field in the current Document object. Since you most likely know the type | ||
* of this field, it is advised that you use a dedicated method, like get StructuredText() or getDate(), | ||
* Gets the fragment in the current Document object. Since you most likely know the type | ||
* of this fragment, it is advised that you use a dedicated method, like get StructuredText() or getDate(), | ||
* for instance. | ||
* | ||
* @param {string} name - The name of the field to get, with its type; for instance, "blog-post.author" | ||
* @param {string} name - The name of the fragment to get, with its type; for instance, "blog-post.author" | ||
* @returns {object} - The JavaScript Fragment object to manipulate | ||
@@ -520,10 +621,10 @@ */ | ||
/** | ||
* Gets the image field in the current Document object, for further manipulation. | ||
* Typical use: document.getImage('blog-post.photo').asHtml(ctx.link_resolver) | ||
* Gets the image fragment in the current Document object, for further manipulation. | ||
* Typical use: document.getImage('blog-post.photo').asHtml(ctx) | ||
* | ||
* @param {string} field - The name of the field to get, with its type; for instance, "blog-post.photo" | ||
* @returns {Image} - The Image object to manipulate | ||
* @param {string} fragment - The name of the fragment to get, with its type; for instance, "blog-post.photo" | ||
* @returns {ImageEl} - The Image object to manipulate | ||
*/ | ||
getImage: function(field) { | ||
var img = this.get(field); | ||
getImage: function(fragment) { | ||
var img = this.get(fragment); | ||
if (img instanceof Global.Prismic.Fragments.Image) { | ||
@@ -539,4 +640,5 @@ return img; | ||
getAllImages: function(field) { | ||
var images = this.getAll(field); | ||
// Useful for obsolete multiples | ||
getAllImages: function(fragment) { | ||
var images = this.getAll(fragment); | ||
@@ -556,10 +658,10 @@ return images.map(function (image) { | ||
/** | ||
* Gets the view within the image field in the current Document object, for further manipulation. | ||
* Typical use: document.getImageView('blog-post.photo', 'large').asHtml(ctx.link_resolver) | ||
* Gets the view within the image fragment in the current Document object, for further manipulation. | ||
* Typical use: document.getImageView('blog-post.photo', 'large').asHtml(ctx) | ||
* | ||
* @param {string} field - The name of the field to get, with its type; for instance, "blog-post.photo" | ||
* @param {string} fragment - The name of the fragment to get, with its type; for instance, "blog-post.photo" | ||
* @returns {ImageView} - The View object to manipulate | ||
*/ | ||
getImageView: function(field, view) { | ||
var fragment = this.get(field); | ||
getImageView: function(fragment, view) { | ||
var fragment = this.get(fragment); | ||
if (fragment instanceof Global.Prismic.Fragments.Image) { | ||
@@ -578,4 +680,5 @@ return fragment.getView(view); | ||
getAllImageViews: function(field, view) { | ||
return this.getAllImages(field).map(function (image) { | ||
// Useful for obsolete multiples | ||
getAllImageViews: function(fragment, view) { | ||
return this.getAllImages(fragment).map(function (image) { | ||
return image.getView(view); | ||
@@ -586,10 +689,10 @@ }); | ||
/** | ||
* Gets the date field in the current Document object, for further manipulation. | ||
* Typical use: document.getDate('blog-post.publicationdate').asHtml(ctx.link_resolver) | ||
* Gets the date fragment in the current Document object, for further manipulation. | ||
* Typical use: document.getDate('blog-post.publicationdate').asHtml(ctx) | ||
* | ||
* @param {string} field - The name of the field to get, with its type; for instance, "blog-post.publicationdate" | ||
* @param {string} fragment - The name of the fragment to get, with its type; for instance, "blog-post.publicationdate" | ||
* @returns {Date} - The Date object to manipulate | ||
*/ | ||
getDate: function(field) { | ||
var fragment = this.get(field); | ||
getDate: function(fragment) { | ||
var fragment = this.get(fragment); | ||
@@ -602,11 +705,11 @@ if(fragment instanceof Global.Prismic.Fragments.Date) { | ||
/** | ||
* Gets the boolean field in the current Document object, for further manipulation. | ||
* Typical use: document.getBoolean('blog-post.enableComments').asHtml(ctx.link_resolver). | ||
* This works great with a Select field. The Select values that are considered true are: 'yes', 'on', and 'true'. | ||
* Gets a boolean value of the fragment in the current Document object, for further manipulation. | ||
* Typical use: document.getBoolean('blog-post.enableComments').asHtml(ctx). | ||
* This works great with a Select fragment. The Select values that are considered true are (lowercased before matching): 'yes', 'on', and 'true'. | ||
* | ||
* @param {string} field - The name of the field to get, with its type; for instance, "blog-post.enableComments" | ||
* @param {string} fragment - The name of the fragment to get, with its type; for instance, "blog-post.enableComments" | ||
* @returns {boolean} | ||
*/ | ||
getBoolean: function(field) { | ||
var fragment = this.get(field); | ||
getBoolean: function(fragment) { | ||
var fragment = this.get(fragment); | ||
return fragment.value && (fragment.value.toLowerCase() == 'yes' || fragment.value.toLowerCase() == 'on' || fragment.value.toLowerCase() == 'true'); | ||
@@ -616,11 +719,12 @@ }, | ||
/** | ||
* Gets the text field in the current Document object, for further manipulation. | ||
* Typical use: document.getText('blog-post.label').asHtml(ctx.link_resolver). | ||
* The method works with StructuredText fields, Text fields, Number fields, Select fields and Color fields. | ||
* Gets the text fragment in the current Document object, for further manipulation. | ||
* Typical use: document.getText('blog-post.label').asHtml(ctx). | ||
* The method works with StructuredText fragments, Text fragments, Number fragments, Select fragments and Color fragments. | ||
* | ||
* @param {string} field - The name of the field to get, with its type; for instance, "blog-post.label" | ||
* @param {string} fragment - The name of the fragment to get, with its type; for instance, "blog-post.label" | ||
* @param {string} after - a suffix that will be appended to the value | ||
* @returns {object} - either StructuredText, or Text, or Number, or Select, or Color. | ||
*/ | ||
getText: function(field, after) { | ||
var fragment = this.get(field); | ||
getText: function(fragmentName, after) { | ||
var fragment = this.get(fragmentName); | ||
@@ -661,10 +765,10 @@ if (fragment instanceof Global.Prismic.Fragments.StructuredText) { | ||
/** | ||
* Gets the StructuredText field in the current Document object, for further manipulation. | ||
* Typical use: document.getStructuredText('blog-post.body').asHtml(ctx.link_resolver). | ||
* Gets the StructuredText fragment in the current Document object, for further manipulation. | ||
* Typical use: document.getStructuredText('blog-post.body').asHtml(ctx). | ||
* | ||
* @param {string} field - The name of the field to get, with its type; for instance, "blog-post.body" | ||
* @returns {StructuredText} - The StructuredText field to manipulate. | ||
* @param {string} fragment - The name of the fragment to get, with its type; for instance, "blog-post.body" | ||
* @returns {StructuredText} - The StructuredText fragment to manipulate. | ||
*/ | ||
getStructuredText: function(field) { | ||
var fragment = this.get(field); | ||
getStructuredText: function(fragment) { | ||
var fragment = this.get(fragment); | ||
@@ -677,10 +781,10 @@ if (fragment instanceof Global.Prismic.Fragments.StructuredText) { | ||
/** | ||
* Gets the Number field in the current Document object, for further manipulation. | ||
* Typical use: document.getNumber('product.price').asHtml(ctx.link_resolver). | ||
* Gets the Number fragment in the current Document object, for further manipulation. | ||
* Typical use: document.getNumber('product.price').asHtml(ctx). | ||
* | ||
* @param {string} field - The name of the field to get, with its type; for instance, "product.price" | ||
* @returns {Number} - The Number field to manipulate. | ||
* @param {string} fragment - The name of the fragment to get, with its type; for instance, "product.price" | ||
* @returns {Number} - The Number fragment to manipulate. | ||
*/ | ||
getNumber: function(field) { | ||
var fragment = this.get(field); | ||
getNumber: function(fragment) { | ||
var fragment = this.get(fragment); | ||
@@ -693,11 +797,26 @@ if (fragment instanceof Global.Prismic.Fragments.Number) { | ||
/** | ||
* Shortcut to get the HTML output of the field in the current document. | ||
* This is the same as writing document.get(field).asHtml(linkResolver); | ||
* Gets the Group fragment in the current Document object, for further manipulation. | ||
* Typical use: document.getGroup('product.gallery').asHtml(ctx). | ||
* | ||
* @param {string} field - The name of the field to get, with its type; for instance, "blog-post.body" | ||
* @param {function} ctx - The ctx object that contains the context: ctx.api, ctx.ref, ctx.maybeRef, ctx.oauth(), et ctx.linkResolver() | ||
* @param {string} fragment - The name of the fragment to get, with its type; for instance, "product.gallery" | ||
* @returns {Group} - The Group fragment to manipulate. | ||
*/ | ||
getGroup: function(fragment) { | ||
var fragment = this.get(fragment); | ||
if (fragment instanceof Global.Prismic.Fragments.Group) { | ||
return fragment; | ||
} | ||
}, | ||
/** | ||
* Shortcut to get the HTML output of the fragment in the current document. | ||
* This is the same as writing document.get(fragment).asHtml(ctx); | ||
* | ||
* @param {string} fragment - The name of the fragment to get, with its type; for instance, "blog-post.body" | ||
* @param {function} ctx - The ctx object that contains the context: ctx.api, ctx.ref, ctx.maybeRef, ctx.oauth(), and ctx.linkResolver() | ||
* @returns {string} - The HTML output | ||
*/ | ||
getHtml: function(field, ctx) { | ||
var fragment = this.get(field); | ||
getHtml: function(fragment, ctx) { | ||
var fragment = this.get(fragment); | ||
@@ -710,6 +829,6 @@ if(fragment && fragment.asHtml) { | ||
/** | ||
* Transforms the whole document as an HTML output. Each field is separated by a <section> tag, | ||
* with the attribute data-field="nameoffield" | ||
* Transforms the whole document as an HTML output. Each fragment is separated by a <section> tag, | ||
* with the attribute data-field="nameoffragment" | ||
* | ||
* @param {object} ctx - The ctx object that contains the context: ctx.api, ctx.ref, ctx.maybeRef, ctx.oauth(), et ctx.linkResolver() | ||
* @param {object} ctx - The ctx object that contains the context: ctx.api, ctx.ref, ctx.maybeRef, ctx.oauth(), and ctx.linkResolver() | ||
* @returns {string} - The HTML output | ||
@@ -729,7 +848,21 @@ */ | ||
/** | ||
* Embodies a prismic.io ref (a past or future point in time you can query ) | ||
* Embodies a prismic.io ref (a past or future point in time you can query) | ||
* @constructor | ||
* @global | ||
*/ | ||
function Ref(ref, label, isMaster) { | ||
/** | ||
* @field | ||
* @description the ID of the ref | ||
*/ | ||
this.ref = ref; | ||
/** | ||
* @field | ||
* @description the label of the ref | ||
*/ | ||
this.label = label; | ||
/** | ||
* @field | ||
* @description is true if the ref is the master ref | ||
*/ | ||
this.isMaster = isMaster; | ||
@@ -736,0 +869,0 @@ } |
@@ -7,2 +7,5 @@ (function (Global, undefined) { | ||
* Embodies a plain text fragment (beware: not a structured text) | ||
* @constructor | ||
* @global | ||
* @alias Fragments:Text | ||
*/ | ||
@@ -26,8 +29,20 @@ function Text(data) { | ||
* Embodies a document link fragment (a link that is internal to a prismic.io repository) | ||
* @constructor | ||
* @global | ||
* @alias Fragments:DocumentLink | ||
*/ | ||
function DocumentLink(data) { | ||
this.value = data; | ||
/** | ||
* @field | ||
* @description the document link's JSON object, exactly as is returned in the JSON responses (see API documentation: https://developers.prismic.io/documentation/UjBe8bGIJ3EKtgBZ/api-documentation#json-responses) | ||
*/ | ||
this.document = data.document; | ||
/** | ||
* @field | ||
* @description true if the link is broken, false otherwise | ||
*/ | ||
this.isBroken = data.isBroken; | ||
} | ||
DocumentLink.prototype = { | ||
@@ -46,3 +61,3 @@ /** | ||
* Returns the URL of the document link. | ||
* | ||
* | ||
* @params {object} ctx - mandatory ctx object, with a useable linkResolver function (please read prismic.io online documentation about this) | ||
@@ -58,4 +73,11 @@ * @returns {string} - the proper URL to use | ||
* Embodies a web link fragment | ||
* @constructor | ||
* @global | ||
* @alias Fragments:WebLink | ||
*/ | ||
function WebLink(data) { | ||
/** | ||
* @field | ||
* @description the JSON object exactly as is returned in the "data" field of the JSON responses (see API documentation: https://developers.prismic.io/documentation/UjBe8bGIJ3EKtgBZ/api-documentation#json-responses) | ||
*/ | ||
this.value = data; | ||
@@ -75,3 +97,3 @@ } | ||
* Returns the URL of the link. | ||
* | ||
* | ||
* @returns {string} - the proper URL to use | ||
@@ -86,4 +108,11 @@ */ | ||
* Embodies a file link fragment | ||
* @constructor | ||
* @global | ||
* @alias Fragments:FileLink | ||
*/ | ||
function FileLink(data) { | ||
/** | ||
* @field | ||
* @description the JSON object exactly as is returned in the "data" field of the JSON responses (see API documentation: https://developers.prismic.io/documentation/UjBe8bGIJ3EKtgBZ/api-documentation#json-responses) | ||
*/ | ||
this.value = data; | ||
@@ -103,3 +132,3 @@ } | ||
* Returns the URL of the link. | ||
* | ||
* | ||
* @returns {string} - the proper URL to use | ||
@@ -114,4 +143,11 @@ */ | ||
* Embodies an image link fragment | ||
* @constructor | ||
* @global | ||
* @alias Fragments:ImageLink | ||
*/ | ||
function ImageLink(data) { | ||
/** | ||
* @field | ||
* @description the JSON object exactly as is returned in the "data" field of the JSON responses (see API documentation: https://developers.prismic.io/documentation/UjBe8bGIJ3EKtgBZ/api-documentation#json-responses) | ||
*/ | ||
this.value = data; | ||
@@ -131,3 +167,3 @@ } | ||
* Returns the URL of the link. | ||
* | ||
* | ||
* @returns {string} - the proper URL to use | ||
@@ -142,4 +178,11 @@ */ | ||
* Embodies a select fragment | ||
* @constructor | ||
* @global | ||
* @alias Fragments:Select | ||
*/ | ||
function Select(data) { | ||
/** | ||
* @field | ||
* @description the text value of the fragment | ||
*/ | ||
this.value = data; | ||
@@ -161,4 +204,11 @@ } | ||
* Embodies a color fragment | ||
* @constructor | ||
* @global | ||
* @alias Fragments:Color | ||
*/ | ||
function Color(data) { | ||
/** | ||
* @field | ||
* @description the text value of the fragment | ||
*/ | ||
this.value = data; | ||
@@ -180,4 +230,11 @@ } | ||
* Embodies a Number fragment | ||
* @constructor | ||
* @global | ||
* @alias Fragments:Num | ||
*/ | ||
function Num(data) { | ||
/** | ||
* @field | ||
* @description the integer value of the fragment | ||
*/ | ||
this.value = data; | ||
@@ -199,4 +256,11 @@ } | ||
* Embodies a DateTime fragment | ||
* @constructor | ||
* @global | ||
* @alias Fragments:DateTime | ||
*/ | ||
function DateTime(data) { | ||
/** | ||
* @field | ||
* @description the Date value of the fragment (as a regular JS Date object) | ||
*/ | ||
this.value = new Date(data); | ||
@@ -222,4 +286,11 @@ } | ||
* Embodies an embed fragment | ||
* @constructor | ||
* @global | ||
* @alias Fragments:Embed | ||
*/ | ||
function Embed(data) { | ||
/** | ||
* @field | ||
* @description the JSON object exactly as is returned in the "data" field of the JSON responses (see API documentation: https://developers.prismic.io/documentation/UjBe8bGIJ3EKtgBZ/api-documentation#json-responses) | ||
*/ | ||
this.value = data; | ||
@@ -236,3 +307,3 @@ } | ||
asHtml: function () { | ||
return this.oembed.html; | ||
return this.value.oembed.html; | ||
} | ||
@@ -243,5 +314,16 @@ }; | ||
* Embodies an Image fragment | ||
* @constructor | ||
* @global | ||
* @alias Fragments:ImageEl | ||
*/ | ||
function ImageEl(main, views) { | ||
/** | ||
* @field | ||
* @description the main ImageView for this image | ||
*/ | ||
this.main = main; | ||
/** | ||
* @field | ||
* @description an array of all the other ImageViews for this image | ||
*/ | ||
this.views = views || {}; | ||
@@ -276,6 +358,21 @@ } | ||
* Embodies an image view (an image in prismic.io can be defined with several different thumbnail sizes, each size is called a "view") | ||
* @constructor | ||
* @global | ||
* @alias Fragments:ImageView | ||
*/ | ||
function ImageView(url, width, height) { | ||
/** | ||
* @field | ||
* @description the URL of the ImageView (useable as it, in a <img> tag in HTML, for instance) | ||
*/ | ||
this.url = url; | ||
/** | ||
* @field | ||
* @description the width of the ImageView | ||
*/ | ||
this.width = width; | ||
/** | ||
* @field | ||
* @description the height of the ImageView | ||
*/ | ||
this.height = height; | ||
@@ -298,3 +395,48 @@ } | ||
function Group(tag, blocks) { | ||
/** | ||
* Embodies a fragment of type "Group" (which is a group of subfragments) | ||
* @constructor | ||
* @global | ||
* @alias Fragments:Group | ||
*/ | ||
function Group(data) { | ||
this.value = data; | ||
} | ||
Group.prototype = { | ||
/** | ||
* Turns the fragment into a useable HTML version of it. | ||
* If the native HTML code doesn't suit your design, this function is meant to be overriden. | ||
* @params {object} ctx - mandatory ctx object, with a useable linkResolver function (please read prismic.io online documentation about this) | ||
* @returns {string} - basic HTML code for the fragment | ||
*/ | ||
asHtml: function(ctx) { | ||
var output = ""; | ||
for (var i=0; i<this.value.length; i++) { | ||
for (var fragmentName in this.value[i]) { | ||
output += '<section data-field="'+fragmentName+'">'; | ||
output += this.value[i][fragmentName].asHtml(ctx); | ||
output += '</section>'; | ||
} | ||
} | ||
return output; | ||
}, | ||
/** | ||
* Turns the Group fragment into an array in order to access its items (groups of fragments), | ||
* or to loop through them. | ||
* @params {object} ctx - mandatory ctx object, with a useable linkResolver function (please read prismic.io online documentation about this) | ||
* @returns {array} - the array of groups, each group being a JSON object with subfragment name as keys, and subfragment as values | ||
*/ | ||
toArray: function(){ | ||
return this.value; | ||
} | ||
} | ||
/** | ||
* Embodies a group of text blocks in a structured text fragment, like a group of list items. | ||
* This is only used in the serialization into HTML of structured text fragments. | ||
* @constructor | ||
* @private | ||
*/ | ||
function BlockGroup(tag, blocks) { | ||
this.tag = tag; | ||
@@ -306,2 +448,5 @@ this.blocks = blocks; | ||
* Embodies a structured text fragment | ||
* @constructor | ||
* @global | ||
* @alias Fragments:StructuredText | ||
*/ | ||
@@ -365,3 +510,3 @@ function StructuredText(blocks) { | ||
* If the native HTML code doesn't suit your design, this function is meant to be overriden. | ||
* | ||
* @params {object} ctx - mandatory ctx object, with a useable linkResolver function (please read prismic.io online documentation about this) | ||
* @returns {string} - basic HTML code for the fragment | ||
@@ -378,2 +523,3 @@ */ | ||
* | ||
* @private | ||
* @param {array} blocks - the array of blocks to deal with | ||
@@ -385,4 +531,4 @@ * @param {object} ctx - the context object, containing the linkResolver function to build links that may be in the fragment (please read prismic.io's online documentation about this) | ||
var groups = [], | ||
group, | ||
var blockGroups = [], | ||
blockGroup, | ||
block, | ||
@@ -396,45 +542,48 @@ html = []; | ||
if (block.type != "list-item" && block.type != "o-list-item") { // it's not a type that groups | ||
group = new Group(block.type, []); | ||
groups.push(group); | ||
blockGroup = new BlockGroup(block.type, []); | ||
blockGroups.push(blockGroup); | ||
} | ||
else if (group && group.tag != block.type) { // it's a new type | ||
group = new Group(block.type, []); | ||
groups.push(group); | ||
else if (blockGroup && blockGroup.tag != block.type) { // it's a new type | ||
blockGroup = new BlockGroup(block.type, []); | ||
blockGroups.push(blockGroup); | ||
} | ||
// else: it's the same type as before, no touching group | ||
group.blocks.push(block); | ||
blockGroup.blocks.push(block); | ||
}; | ||
groups.forEach(function (group) { | ||
blockGroups.forEach(function (blockGroup) { | ||
if(group.tag == "heading1") { | ||
html.push('<h1>' + insertSpans(group.blocks[0].text, group.blocks[0].spans, ctx) + '</h1>'); | ||
if(blockGroup.tag == "heading1") { | ||
html.push('<h1>' + insertSpans(blockGroup.blocks[0].text, blockGroup.blocks[0].spans, ctx) + '</h1>'); | ||
} | ||
else if(group.tag == "heading2") { | ||
html.push('<h2>' + insertSpans(group.blocks[0].text, group.blocks[0].spans, ctx) + '</h2>'); | ||
else if(blockGroup.tag == "heading2") { | ||
html.push('<h2>' + insertSpans(blockGroup.blocks[0].text, blockGroup.blocks[0].spans, ctx) + '</h2>'); | ||
} | ||
else if(group.tag == "heading3") { | ||
html.push('<h3>' + insertSpans(group.blocks[0].text, group.blocks[0].spans, ctx) + '</h3>'); | ||
else if(blockGroup.tag == "heading3") { | ||
html.push('<h3>' + insertSpans(blockGroup.blocks[0].text, blockGroup.blocks[0].spans, ctx) + '</h3>'); | ||
} | ||
else if(group.tag == "paragraph") { | ||
html.push('<p>' + insertSpans(group.blocks[0].text, group.blocks[0].spans, ctx) + '</p>'); | ||
else if(blockGroup.tag == "paragraph") { | ||
html.push('<p>' + insertSpans(blockGroup.blocks[0].text, blockGroup.blocks[0].spans, ctx) + '</p>'); | ||
} | ||
else if(group.tag == "image") { | ||
html.push('<p><img src="' + group.blocks[0].url + '"></p>'); | ||
else if(blockGroup.tag == "preformatted") { | ||
html.push('<pre>' + blockGroup.blocks[0].text + '</pre>'); | ||
} | ||
else if(group.tag == "embed") { | ||
html.push('<div data-oembed="'+ group.blocks[0].embed_url | ||
+ '" data-oembed-type="'+ group.blocks[0].type | ||
+ '" data-oembed-provider="'+ group.blocks[0].provider_name | ||
+ '">' + group.blocks[0].oembed.html+"</div>") | ||
else if(blockGroup.tag == "image") { | ||
html.push('<p><img src="' + blockGroup.blocks[0].url + '"></p>'); | ||
} | ||
else if(group.tag == "list-item" || group.tag == "o-list-item") { | ||
html.push(group.tag == "list-item"?'<ul>':"<ol>"); | ||
group.blocks.forEach(function(block){ | ||
else if(blockGroup.tag == "embed") { | ||
html.push('<div data-oembed="'+ blockGroup.blocks[0].embed_url | ||
+ '" data-oembed-type="'+ blockGroup.blocks[0].type | ||
+ '" data-oembed-provider="'+ blockGroup.blocks[0].provider_name | ||
+ '">' + blockGroup.blocks[0].oembed.html+"</div>") | ||
} | ||
else if(blockGroup.tag == "list-item" || blockGroup.tag == "o-list-item") { | ||
html.push(blockGroup.tag == "list-item"?'<ul>':"<ol>"); | ||
blockGroup.blocks.forEach(function(block){ | ||
html.push("<li>"+insertSpans(block.text, block.spans, ctx)+"</li>"); | ||
}); | ||
html.push(group.tag == "list-item"?'</ul>':"</ol>"); | ||
html.push(blockGroup.tag == "list-item"?'</ul>':"</ol>"); | ||
} | ||
else throw new Error(group.tag+" not implemented"); | ||
else throw new Error(blockGroup.tag+" not implemented"); | ||
}); | ||
@@ -451,2 +600,3 @@ | ||
* | ||
* @private | ||
* @param {string} text - the original text of the block | ||
@@ -504,2 +654,3 @@ * @param {object} span - the spans as returned by the API | ||
* | ||
* @private | ||
* @param {string} field - the fragment's name | ||
@@ -579,4 +730,18 @@ * @returns {object} - the object of the proper Fragments type. | ||
case "Group": | ||
var groups_array = []; | ||
// for each array of groups | ||
for (var i = 0; i < field.value.length; i++) { | ||
var group = {}; // recreate groups with... | ||
for (var fragmentName in field.value[i]) { | ||
// ... the same fragment name as keys, but reinitalized fragments as values | ||
group[fragmentName] = initField(field.value[i][fragmentName]); | ||
} | ||
groups_array.push(group); | ||
} | ||
output = new Group(groups_array); | ||
break; | ||
default: | ||
console.log("Link type not supported: ", field.type); | ||
console.log("Fragment type not supported: ", field.type); | ||
break; | ||
@@ -602,2 +767,3 @@ } | ||
FileLink: FileLink, | ||
Group: Group, | ||
initField: initField | ||
@@ -604,0 +770,0 @@ } |
100
test/test.js
@@ -8,4 +8,17 @@ (function(Prismic) { | ||
// This token allow to preview future releases of this repository (nothing secret ;) | ||
previewToken = 'MC5VbDdXQmtuTTB6Z0hNWHF3.c--_vVbvv73vv73vv73vv71EA--_vS_vv73vv70T77-9Ke-_ve-_vWfvv70ebO-_ve-_ve-_vQN377-9ce-_vRfvv70'; | ||
previewToken = 'MC5VbDdXQmtuTTB6Z0hNWHF3.c--_vVbvv73vv73vv73vv71EA--_vS_vv73vv70T77-9Ke-_ve-_vWfvv70ebO-_ve-_ve-_vQN377-9ce-_vRfvv70', | ||
microRepository = 'https://micro.prismic.io/api', | ||
ctx = { | ||
api: undefined, | ||
ref: { ref: 'XXXXX', label: 'Future release', isMaster: false }, | ||
maybeRef: 'XXXXX', | ||
oauth: function() { }, | ||
linkResolver: function(ctx, doc, isBroken) { | ||
if (isBroken) return '#broken'; | ||
return "/testing_url/"+doc.id+"/"+doc.slug+( ctx.maybeRef ? '?ref=' + ctx.maybeRef : '' ); | ||
} | ||
}; | ||
module('Prismic.io', { | ||
@@ -78,6 +91,18 @@ setup: function() {} | ||
asyncTest('Submit the `everything` form with an ordering', 2, function() { | ||
Prismic.Api(testRepository, function(err, Api) { | ||
if (err) { console.log(err); return; } | ||
Api.form('everything').ref(Api.master()).orderings('[my.product.price desc]').submit(function(err, documents) { | ||
if (err) { console.log(err); return; } | ||
equal(documents.results.length, 20); | ||
equal(documents.results[0].id, 'UkL0gMuvzYUANCpm'); | ||
start(); | ||
}); | ||
}); | ||
}); | ||
asyncTest('Get page 2 of the `everything` form', 8, function() { | ||
Prismic.Api(testRepository, function(err, Api) { | ||
if (err) { console.log(err); return; } | ||
Api.form('everything').set("page", 2).ref(Api.master()).submit(function(err, documents) { | ||
Api.form('everything').page(2).ref(Api.master()).submit(function(err, documents) { | ||
if (err) { console.log(err); return; } | ||
@@ -100,3 +125,3 @@ equal(documents.results.length, 20); | ||
if (err) { console.log(err); return; } | ||
Api.form('everything').set("pageSize", 10).ref(Api.master()).submit(function(err, documents) { | ||
Api.form('everything').pageSize(10).ref(Api.master()).submit(function(err, documents) { | ||
if (err) { console.log(err); return; } | ||
@@ -119,3 +144,3 @@ equal(documents.results.length, 10); | ||
if (err) { console.log(err); return; } | ||
Api.form('everything').set("pageSize", 10).set("page", 2).ref(Api.master()).submit(function(err, documents) { | ||
Api.form('everything').pageSize(10).page(2).ref(Api.master()).submit(function(err, documents) { | ||
if (err) { console.log(err); return; } | ||
@@ -229,12 +254,2 @@ equal(documents.results.length, 10); | ||
asyncTest('Render a document to Html', 1, function() { | ||
var ctx = { | ||
api: undefined, | ||
ref: { ref: 'XXXXX', label: 'Future release', isMaster: false }, | ||
maybeRef: 'XXXXX', | ||
oauth: function() { }, | ||
linkResolver: function(ctx, doc, isBroken) { | ||
if (isBroken) return '#broken'; | ||
return "/testing_url/"+doc.id+"/"+doc.slug+( ctx.maybeRef ? '?ref=' + ctx.maybeRef : '' ); | ||
} | ||
}; | ||
Prismic.Api(testRepository, function(err, Api) { | ||
@@ -263,2 +278,13 @@ if (err) { console.log(err); return; } | ||
asyncTest('StructuredTexts asHtml handles preformatted', 1, function() { | ||
Prismic.Api('https://micro.prismic.io/api', function(err, Api) { | ||
if (err) { console.log(err); return; } | ||
Api.form('everything').query('[[:d = at(document.id, "UrDejAEAAFwMyrW9")]]').ref(Api.master()).submit(function(err, documents) { | ||
if (err) { console.log(err); return; } | ||
equal(documents.results[0].getStructuredText('doc.content').asHtml(ctx), '<p>Meta-micro gets installed pretty much like any javascript library:</p><ol><li><a href=\"/testing_url/U0w8OwEAACoAQEvB/download-meta-micro?ref=XXXXX\">download</a> the .js file: get the minified one, unless the framework you\'re using minifies your .js files automatically.</li><li>add a link towards the file in your webpage\'s head.</li></ol><p>The link might look like this, anywhere inside your head tag:</p><pre><script type=\"text/javascript\" src=\"meta-micro.min.js\"></script></pre><p>You\'re all set!</p>'); | ||
start(); | ||
}); | ||
}, previewToken); | ||
}); | ||
asyncTest('StructuredTexts asHtml handles spans', 1, function() { | ||
@@ -287,12 +313,2 @@ Prismic.Api(testRepository, function(err, Api) { | ||
asyncTest('StructuredTexts asHtml handles span Link.document', 1, function() { | ||
var ctx = { | ||
api: undefined, | ||
ref: { ref: 'XXXXX', label: 'Future release', isMaster: false }, | ||
maybeRef: 'XXXXX', | ||
oauth: function() { }, | ||
linkResolver: function(ctx, doc, isBroken) { | ||
if (isBroken) return '#broken'; | ||
return "/testing_url/"+doc.id+"/"+doc.slug+( ctx.maybeRef ? '?ref=' + ctx.maybeRef : '' ); | ||
} | ||
}; | ||
Prismic.Api(testRepository, function(err, Api) { | ||
@@ -310,3 +326,3 @@ if (err) { console.log(err); return; } | ||
var jsonString = '{"type":"StructuredText","value":[{"type":"paragraph","text":"2012 Annual Report","spans":[{"start":0,"end":18,"type":"hyperlink","data":{"type":"Link.file","value":{"file":{"name":"2012_annual.report.pdf","kind":"document","url":"https://prismic-io.s3.amazonaws.com/annual.report.pdf","size":"1282484"}}}}]},{"type":"paragraph","text":"2012 Annual Budget","spans":[{"start":0,"end":18,"type":"hyperlink","data":{"type":"Link.file","value":{"file":{"name":"2012_smec.annual.budget.pdf","kind":"document","url":"https://prismic-io.s3.amazonaws.com/annual.budget.pdf","size":"59229"}}}}]},{"type":"paragraph","text":"2015 Vision & Strategic Plan","spans":[{"start":0,"end":28,"type":"hyperlink","data":{"type":"Link.file","value":{"file":{"name":"2015_vision.strategic.plan_.sm_.pdf","kind":"document","url":"https://prismic-io.s3.amazonaws.com/vision.strategic.plan_.sm_.pdf","size":"1969956"}}}}]}]}'; | ||
var jsonObject = eval("(" + jsonString + ')'); | ||
var jsonObject = JSON.parse(jsonString); | ||
equal(Prismic.Fragments.initField(jsonObject).asHtml(), '<p><a href=\"https://prismic-io.s3.amazonaws.com/annual.report.pdf\">2012 Annual Report</a></p><p><a href=\"https://prismic-io.s3.amazonaws.com/annual.budget.pdf\">2012 Annual Budget</a></p><p><a href=\"https://prismic-io.s3.amazonaws.com/vision.strategic.plan_.sm_.pdf\">2015 Vision & Strategic Plan</a></p>'); | ||
@@ -317,12 +333,2 @@ start(); | ||
asyncTest('Handles multiple fields', 1, function() { | ||
var ctx = { | ||
api: undefined, | ||
ref: { ref: 'XXXXX', label: 'Future release', isMaster: false }, | ||
maybeRef: 'XXXXX', | ||
oauth: function() { }, | ||
linkResolver: function(ctx, doc, isBroken) { | ||
if (isBroken) return '#broken'; | ||
return "/testing_url/"+doc.id+"/"+doc.slug+( ctx.maybeRef ? '?ref=' + ctx.maybeRef : '' ); | ||
} | ||
}; | ||
Prismic.Api(testRepository, function(err, Api) { | ||
@@ -350,2 +356,24 @@ if (err) { console.log(err); return; } | ||
}(window.Prismic)); | ||
asyncTest('Block fragments are accessible, loopable, and serializable', 4, function() { | ||
Prismic.Api(microRepository, function(err, Api) { | ||
if (err) { console.log(err); return; } | ||
Api.form('everything').query('[[:d = at(document.id, "UrDndQEAALQMyrXF")]]').ref(Api.master()).submit(function(err, documents) { | ||
if (err) { console.log(err); return; } | ||
// Group fragments are accessible | ||
equal(documents.results[0].getGroup('docchapter.docs').toArray()[0]['linktodoc'].value.document.type, 'doc'); | ||
// Group fragments are loopable | ||
var slugs = ""; | ||
for (var i = 0; i<documents.results[0].getGroup('docchapter.docs').toArray().length; i++) { | ||
slugs += documents.results[0].getGroup('docchapter.docs').toArray()[i]['linktodoc'].value.document.slug + ' '; | ||
} | ||
equal(slugs.trim(), 'with-jquery with-bootstrap'); | ||
// Group fragments are serializable when asHtml is called directly on them | ||
equal(documents.results[0].getGroup('docchapter.docs').asHtml(ctx), '<section data-field=\"linktodoc\"><a href=\"/testing_url/UrDofwEAALAdpbNH/with-jquery?ref=XXXXX\">/testing_url/UrDofwEAALAdpbNH/with-jquery?ref=XXXXX</a></section><section data-field=\"linktodoc\"><a href=\"/testing_url/UrDp8AEAAPUdpbNL/with-bootstrap?ref=XXXXX\">/testing_url/UrDp8AEAAPUdpbNL/with-bootstrap?ref=XXXXX</a></section>'); | ||
// Group fragments are serializable when as Html is called on a document | ||
equal(documents.results[0].asHtml(ctx), '<section data-field=\"docchapter.title\"><h1>Using with other projects</h1></section><section data-field=\"docchapter.intro\"><p>As advertised, meta-micro knows how to stay out of the way of the rest of your application. Here are some cases of how to use it with some of the most used open-source projects in JavaScript.</p></section><section data-field=\"docchapter.priority\"><span>500</span></section><section data-field=\"docchapter.docs\"><section data-field=\"linktodoc\"><a href=\"/testing_url/UrDofwEAALAdpbNH/with-jquery?ref=XXXXX\">/testing_url/UrDofwEAALAdpbNH/with-jquery?ref=XXXXX</a></section><section data-field=\"linktodoc\"><a href=\"/testing_url/UrDp8AEAAPUdpbNL/with-bootstrap?ref=XXXXX\">/testing_url/UrDp8AEAAPUdpbNL/with-bootstrap?ref=XXXXX</a></section></section>'); | ||
start(); | ||
}); | ||
}, previewToken); | ||
}); | ||
}(window.Prismic)); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
No License Found
License(Experimental) License information could not be found.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Shell access
Supply chain riskThis module accesses the system shell. Accessing the system shell increases the risk of executing arbitrary code.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
0
296217
8
20
1
5447
8
77
29