Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Added option, cached thesaurus, some cleanup #4

Merged
merged 5 commits into from

2 participants

@ultimatedelman

I added a few options, namely the option to change the ajax call method. This was one problem I had getting this to work initially, as my backend does not allow POSTs on .json files, so I had to override my $.ajaxSetup() to get this working. Now it defaults to 'GET' with the option to override (although I can't see anyone overriding a GET).

I also noticed that the thesaurus was being called every time in a loop for getting sentences. This was resulting in an AJAX call every time the loop was run, which was slowing down page load a lot for a large number of blocks. I simply cached the thesaurus and removed the methods that spliced out paragraphs. I felt that the integrity of the randomness of the sentences/paragraphs/etc was less important for prototyping than the performance of this plugin, as it is, after all, just for prototyping.

I tried to conform to your coding conventions as much as I could and only touched things that I played with for my own need, so I didn't get through the whole codebase. I hope you like my changes!

ultimatedelman added some commits
@ultimatedelman ultimatedelman added options (but not yet implementation) for providing a callback a…
…fter all operations are complete and for overriding the default ajax type. modernized thesaurus ajax call
9a1cb72
@ultimatedelman ultimatedelman ok now all those things i said 1801d93
@ultimatedelman ultimatedelman cached thesaurus to prevent multiple ajax calls, removed splicing of …
…thesaurus so that future 'reset' calls to thesaurus are unnecessary. i decided to do this because after all, this is a prototyping library, so i feel that speed and performance is much more important than ensuring the integrity of the randomness of sentences. also implemented callback after all operations are complete
38ddeb6
@ultimatedelman ultimatedelman forgot to minify. minified version included now f48ab1e
@ultimatedelman ultimatedelman stripped out callback because it's not necessary, as everything is sy…
…nchronous. was initially added when I thought I could make it asynchronous, but I realize now it's not necessary :)
91bca1d
@ndreckshage
Owner

this is great. ajax change is good idea. i noticed the multiple request problem (#2) and have it fixed in current version that im working on. im actually restructuring the whole thesaurus method a bit, and going to be loading the content via jsonp apis (from random text generators with apis, for true random content), and then leave backup option to use local custom lorem ipsum library.

ive got a bit of work to do the way im setting up the next version, and ill merge these changes into mine.

@ultimatedelman

sweet! glad i could help... this plugin has been super useful for my current project while we're in the prototyping phase. you and i both should thank html5weekly :)

@ndreckshage
Owner

just pushed my main changes, but gonna sort through these tomorrow and see what i can merge in.

@ndreckshage ndreckshage merged commit 91bca1d into ndreckshage:master
@ndreckshage
Owner

a lot of our changes were similar -- tough part when working on a single file rather than a codebase

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 31, 2013
  1. @ultimatedelman

    added options (but not yet implementation) for providing a callback a…

    ultimatedelman authored
    …fter all operations are complete and for overriding the default ajax type. modernized thesaurus ajax call
  2. @ultimatedelman
  3. @ultimatedelman

    cached thesaurus to prevent multiple ajax calls, removed splicing of …

    ultimatedelman authored
    …thesaurus so that future 'reset' calls to thesaurus are unnecessary. i decided to do this because after all, this is a prototyping library, so i feel that speed and performance is much more important than ensuring the integrity of the randomness of sentences. also implemented callback after all operations are complete
  4. @ultimatedelman
  5. @ultimatedelman

    stripped out callback because it's not necessary, as everything is sy…

    ultimatedelman authored
    …nchronous. was initially added when I thought I could make it asynchronous, but I realize now it's not necessary :)
This page is out of date. Refresh to see the latest.
Showing with 25 additions and 31 deletions.
  1. +24 −30 jquery.roughdraft.0.1.0.js
  2. +1 −1  jquery.roughdraft.0.1.0.min.js
View
54 jquery.roughdraft.0.1.0.js
@@ -43,6 +43,7 @@
* -- thesaurus is location of json lorem ipsum library
* -- illustrator determines which placeholder image site is to be used
* -- calendar allows people to use different date tags in html. default uses PHP date settings
+ * -- ajaxType allows the user to override the default ajax type if necessary. default is GET
*
**********************************************/
@@ -69,7 +70,8 @@
monthTextThree : 'M',
yearNumber : 'Y',
yearNumberTwo : 'y'
- }
+ },
+ ajaxType: 'GET',
}
/**********************************************
@@ -138,6 +140,7 @@
// data-draft-text taps into lorem ipsum library in roughdraft.thesaurus.json
if ($draftText.length) {
+ this.thesaurus = this._thesaurus(); //caching thesaurus
this.plagerizer($draftText);
}
// data-draft-image taps into placeholder image library
@@ -401,22 +404,21 @@
}
// make the ajax call based on the default/user option url
- // async off since that conflicts with page load
- $.ajax({
- url: this.options.thesaurus,
+ // keep syncronous
+ $.ajax(this.options.thesaurus, {
dataType: 'json',
async: false,
- success: function(data) {
- thesaurus = data;
- }
- });
-
- // if ajax failed, log message to console
- if (thesaurus === false) {
+ type: this.options.ajaxType
+ })
+ .fail(function(){
+ // if ajax failed, log message to console
console.log('There was an issue with the AJAX request in the _thesaurus method. ' +
'Please ensure that ' + this.options.thesaurus + ' is your correct relative path.');
- } else {
- // else return the lorem ipsum data
+ })
+ .done(function(data){
+ thesaurus = data;
+ });
+ if (thesaurus) {
return thesaurus[author];
}
},
@@ -435,11 +437,10 @@
**********************************************/
_openToRandomPage: function(textCount, textType) {
- var thesaurus = this._thesaurus(),
+ var thesaurus = this.thesaurus,
paragraphSentences,
randomParagraph = new Array(),
randomResult = new String();
-
// every texttype needs a paragraph, pass the textcount, type and json library to the paragraph method
randomParagraph = this._randomParagraph(textCount, textType, thesaurus);
// count the number of sentences of the returned paragraph
@@ -482,12 +483,12 @@
*
**********************************************/
- _randomParagraph: function(textCount, textType, thesaurus) {
- var totalParagraphs = thesaurus.length,
+ _randomParagraph: function(textCount, textType) {
+ var thesaurus = this.thesaurus,
+ totalParagraphs = thesaurus.length,
randomParagraphIndex,
currentParagraph,
randomParagraph = new Array();
-
// this method is called by all text types
// if not called by a paragraph type, only 1 paragraphy needs to be returned
if (textType != this.scopeVar.paragraphType) {
@@ -496,26 +497,19 @@
randomParagraphIndex = this._randomizer(totalParagraphs);
// cherry pick the 1 paragraph based on the random index, and then select 1st index ([0], theres only 1)
// this will return a first level array of the paragraphs sentences
- randomParagraph = thesaurus.splice(randomParagraphIndex, 1)[0];
+ // kill splicing, just pick random paragraph each time
+ randomParagraph = thesaurus[randomParagraphIndex];
} else {
// if paragraphs are requested, several may need to be returned
// return a new random paragraph while the text count is > 0
- while (textCount > 0) {
-
- // if a bunch of paragraphs are requested and looped through
- // rebuild the paragraphs based on the ipsum library
- if (totalParagraphs <= 0) {
- // call the thesaurus
- thesaurus = this._thesaurus();
- // and reset the count
- totalParagraphs = thesaurus.length;
- }
+ while (textCount > 0) {
// randomize the paragraph index based on total paragraphs left
randomParagraphIndex = this._randomizer(totalParagraphs);
// pick a paragraph, and pull it out of the array
- currentParagraph = thesaurus.splice(randomParagraphIndex, 1)[0];
+ // ^ don't pull it out of the array to increase performance. we're just prototyping, after all!
+ currentParagraph = thesaurus[randomParagraphIndex];
// concatenate the current paragraph to the total list within the while loop
randomParagraph = randomParagraph.concat(currentParagraph);
View
2  jquery.roughdraft.0.1.0.min.js
@@ -10,4 +10,4 @@
/******************************************************/
/******************************************************/
-(function(e,t,n,r){e.RedPen=function(e){this._create(e);this._init()};e.RedPen.settings={author:"lebowskiipsum.com",thesaurus:"/roughdraft.thesaurus.json",illustrator:"placehold.it",calendar:{dayNumber:"j",dayNumberZeros:"d",dayText:"l",dayTextThree:"D",monthNumber:"n",monthNumberZeros:"m",monthText:"F",monthTextThree:"M",yearNumber:"Y",yearNumberTwo:"y"}};e.RedPen.scopeVar={dataTag:"data-",paragraphType:"p",sentenceType:"s",wordType:"w"};e.RedPen.prototype={_create:function(t){var n=e("[data-draft-repeat]");this.options=e.extend(true,{},e.RedPen.settings,t);this.scopeVar=e.RedPen.scopeVar;if(n.length){this.scanner(n)}},_init:function(){var t=e("[data-draft-text]"),n=e("[data-draft-image]"),r=e("[data-draft-date]");if(t.length){this.plagerizer(t)}if(n.length){this.doodler(n)}if(r.length){this.scheduler(r)}},scanner:function(t){var n,r="draft-repeat",i;for(var s=0;s<t.length;s++){n=e(t[s]);i=n.data(r);n.removeAttr(this.scopeVar.dataTag+r);for(var o=0;o<i-1;o++){n.clone(true,true).insertAfter(n)}}},plagerizer:function(t){var n,r="draft-text",i,s,o;for(var u=0;u<t.length;u++){n=e(t[u]);i=n.data(r);if(typeof i==="string"){i=i.split("/");s=parseInt(i[0]);o=i[1].toLowerCase();if(isNaN(s)||typeof o!=="string"){s=false;o=false}if(s&&o){n.removeAttr(this.scopeVar.dataTag+r);n.text(this._openToRandomPage(s,o))}else{console.log("Please ensure that you specify Paragraph/Sentence/Word in the format 3/S, for 3 sentences")}}}},doodler:function(t){var n,r="draft-image",i,s,o,u;for(var a=0;a<t.length;a++){n=e(t[a]);i=n.data(r);if(typeof i==="string"){i=i.split("/");s=parseInt(i[0]);o=parseInt(i[1]);if(isNaN(s)||isNaN(o)){s=false;o=false}if(s&&o){u=this._photoAlbum(s,o);n.attr("src",u).attr("width",s).attr("height",o).removeAttr(this.scopeVar.dataTag+r)}else{console.log("Please ensure that you specify Width/Height in the format 250/300 for 250px wide by 300px tall")}}}},scheduler:function(t){var n,r="draft-date",i,s,o=new String;for(var u=0;u<t.length;u++){n=e(t[u]);i=n.data(r);if(typeof i==="string"){i=i.split("/");dateDataLength=i.length;o="";for(var a=0;a<i.length;a++){o+=this._datePicker(i[a]);a!=i.length-1?o+=" ":""}n.text(o)}}},_randomizer:function(e){return Math.floor(Math.random()*e)},_thesaurus:function(){var t=false,n=this.options.author,r="lebowskiipsum.com",i="baconipsum.com";switch(n){case r:break;case i:break;default:n=r;break}e.ajax({url:this.options.thesaurus,dataType:"json",async:false,success:function(e){t=e}});if(t===false){console.log("There was an issue with the AJAX request in the _thesaurus method. "+"Please ensure that "+this.options.thesaurus+" is your correct relative path.")}else{return t[n]}},_openToRandomPage:function(e,t){var n=this._thesaurus(),r,i=new Array,s=new String;i=this._randomParagraph(e,t,n);r=i.length;switch(t){case this.scopeVar.paragraphType:s=i.join(" ");break;case this.scopeVar.sentenceType:s=this._randomSentence(e,i,r);break;case this.scopeVar.wordType:s=this._randomWord(e,i);break;default:break}return s},_randomParagraph:function(e,t,n){var r=n.length,i,s,o=new Array;if(t!=this.scopeVar.paragraphType){i=this._randomizer(r);o=n.splice(i,1)[0]}else{while(e>0){if(r<=0){n=this._thesaurus();r=n.length}i=this._randomizer(r);s=n.splice(i,1)[0];o=o.concat(s);r--;e--}}return o},_randomSentence:function(e,t,n){var r=e,i=this._randomizer(n),s=i+1,o,u=new String;while(e>0){if(e!=r){s++;if(s>=n){s=0}o=t[s]}else{o=t[i]}if(e<n){o=o+" "}u+=o;e--}return u},_randomWord:function(e,t){var n=t.join(" ").split(" "),r=t.length,i=this._randomizer(r),s,o=new String;while(e>0){if(i>=r){i=0}s=n[i];if(e<r){s+=" "}o+=s;i++;e--}return o},_photoAlbum:function(e,t){var n=false,r=this.options.illustrator,i="placehold.it",s="placekitten.com",o,u;switch(r){case i:break;case s:break;default:r=i;break}o=this._waterColor(r);if(r==s){u="http://placekitten.com/"+o+e+"/"+t}else{u="http://placehold.it/"+e+"x"+t+o}return u},_waterColor:function(e){var t=new Array,n="placekitten.com",r;if(e==n){t=[false,"g"]}else{t=["453f35","e7cead","b5ab94","eba434","64886c","b15c3a","b1956c"]}r=t.length;r=this._randomizer(r);r=t[r];if(r){if(e==n){r=r+"/"}else{r="/"+r+"/fff"}}else{r=""}return r},_datePicker:function(e){var t=this.options.calendar,n=new Date,r=false;if(e.split("-").length>1){e=e.split("-");if(e[1].toLowerCase()=="r"){r=true}e=e[0]}if(e==t.dayNumberZeros||e==t.dayTextThree||e==t.dayNumber||e==t.dayText){if(e==t.dayTextThree||e==t.dayText||e==t.dayTextThree){n=n.getDay();if(r===true){n=this._randomizer(6)}switch(n){case 0:n="Sunday";break;case 1:n="Monday";break;case 2:n="Tuesday";break;case 3:n="Wednesday";break;case 4:n="Thursday";break;case 5:n="Friday";break;case 6:n="Saturday";break;default:break}if(e==t.dayTextThree){n=n.substr(0,3)}}else{n=n.getDate();if(r===true){n=this._randomizer(28)+1}if(e==t.dayNumberZeros&&n<10){n="0"+n.toString()}}}else if(e==t.monthNumberZeros||e==t.monthTextThree||e==t.monthText||e==t.monthNumber){n=n.getMonth();if(r===true){n=this._randomizer(12)}if(e==t.monthText||e==t.monthTextThree){switch(n){case 0:n="January";break;case 1:n="February";break;case 2:n="March";break;case 3:n="April";break;case 4:n="May";break;case 5:n="June";break;case 6:n="July";break;case 7:n="August";break;case 8:n="September";break;case 9:n="October";break;case 10:n="November";break;case 11:n="December";break;default:break}if(e==t.monthTextThree){n=n.substr(0,3)}}else if(e==t.monthNumberZeros){n="0"+(n+1).toString()}else{n+=1}}else{n=n.getFullYear();if(r===true){n=this._randomizer(25);n<10?n="0"+n.toString():n=n.toString();n="20"+n}if(e==t.yearNumberTwo){n=n.toString().substr(2,3)}}return n}};e.fn.roughDraft=function(t){return this.each(function(){e.data(this,"roughdraft",new e.RedPen(t,this))})}})(jQuery,window,document);
+;(function(e,t,n,r){e.RedPen=function(e){this._create(e);this._init()};e.RedPen.settings={author:"lebowskiipsum.com",thesaurus:"/roughdraft.thesaurus.json",illustrator:"placehold.it",calendar:{dayNumber:"j",dayNumberZeros:"d",dayText:"l",dayTextThree:"D",monthNumber:"n",monthNumberZeros:"m",monthText:"F",monthTextThree:"M",yearNumber:"Y",yearNumberTwo:"y"},ajaxType:"GET"};e.RedPen.scopeVar={dataTag:"data-",paragraphType:"p",sentenceType:"s",wordType:"w"};e.RedPen.prototype={_create:function(t){var n=e("[data-draft-repeat]");this.options=e.extend(true,{},e.RedPen.settings,t);this.scopeVar=e.RedPen.scopeVar;if(n.length){this.scanner(n)}},_init:function(){var t=e("[data-draft-text]"),n=e("[data-draft-image]"),r=e("[data-draft-date]");if(t.length){this.thesaurus=this._thesaurus();this.plagerizer(t)}if(n.length){this.doodler(n)}if(r.length){this.scheduler(r)}},scanner:function(t){var n,r="draft-repeat",i;for(var s=0;s<t.length;s++){n=e(t[s]);i=n.data(r);n.removeAttr(this.scopeVar.dataTag+r);for(var o=0;o<i-1;o++){n.clone(true,true).insertAfter(n)}}},plagerizer:function(t){var n,r="draft-text",i,s,o;for(var u=0;u<t.length;u++){n=e(t[u]);i=n.data(r);if(typeof i==="string"){i=i.split("/");s=parseInt(i[0]);o=i[1].toLowerCase();if(isNaN(s)||typeof o!=="string"){s=false;o=false}if(s&&o){n.removeAttr(this.scopeVar.dataTag+r);n.text(this._openToRandomPage(s,o))}else{console.log("Please ensure that you specify Paragraph/Sentence/Word in the format 3/S, for 3 sentences")}}}},doodler:function(t){var n,r="draft-image",i,s,o,u;for(var a=0;a<t.length;a++){n=e(t[a]);i=n.data(r);if(typeof i==="string"){i=i.split("/");s=parseInt(i[0]);o=parseInt(i[1]);if(isNaN(s)||isNaN(o)){s=false;o=false}if(s&&o){u=this._photoAlbum(s,o);n.attr("src",u).attr("width",s).attr("height",o).removeAttr(this.scopeVar.dataTag+r)}else{console.log("Please ensure that you specify Width/Height in the format 250/300 for 250px wide by 300px tall")}}}},scheduler:function(t){var n,r="draft-date",i,s,o=new String;for(var u=0;u<t.length;u++){n=e(t[u]);i=n.data(r);if(typeof i==="string"){i=i.split("/");dateDataLength=i.length;o="";for(var a=0;a<i.length;a++){o+=this._datePicker(i[a]);a!=i.length-1?o+=" ":""}n.text(o)}}},_randomizer:function(e){return Math.floor(Math.random()*e)},_thesaurus:function(){var t=false,n=this.options.author,r="lebowskiipsum.com",i="baconipsum.com",s="veggieipsum.com";switch(n){case r:break;case i:break;case s:break;default:n=r;break}e.ajax(this.options.thesaurus,{dataType:"json",async:false,type:this.options.ajaxType}).fail(function(){console.log("There was an issue with the AJAX request in the _thesaurus method. "+"Please ensure that "+this.options.thesaurus+" is your correct relative path.")}).done(function(e){t=e});if(t){return t[n]}},_openToRandomPage:function(e,t){var n=this.thesaurus,r,i=new Array,s=new String;i=this._randomParagraph(e,t,n);r=i.length;switch(t){case this.scopeVar.paragraphType:s=i.join(" ");break;case this.scopeVar.sentenceType:s=this._randomSentence(e,i,r);break;case this.scopeVar.wordType:s=this._randomWord(e,i);break;default:break}return s},_randomParagraph:function(e,t){var n=this.thesaurus,r=n.length,i,s,o=new Array;if(t!=this.scopeVar.paragraphType){i=this._randomizer(r);o=n[i]}else{while(e>0){i=this._randomizer(r);s=n[i];o=o.concat(s);r--;e--}}return o},_randomSentence:function(e,t,n){var r=e,i=this._randomizer(n),s=i+1,o,u=new String;while(e>0){if(e!=r){s++;if(s>=n){s=0}o=t[s]}else{o=t[i]}if(e<n){o=o+" "}u+=o;e--}return u},_randomWord:function(e,t){var n=t.join(" ").split(" "),r=t.length,i=this._randomizer(r),s,o=new String;while(e>0){if(i>=r){i=0}s=n[i];if(e<r){s+=" "}o+=s;i++;e--}return o},_photoAlbum:function(e,t){var n=false,r=this.options.illustrator,i="placehold.it",s="placekitten.com",o,u;switch(r){case i:break;case s:break;default:r=i;break}o=this._waterColor(r);if(r==s){u="http://placekitten.com/"+o+e+"/"+t}else{u="http://placehold.it/"+e+"x"+t+o}return u},_waterColor:function(e){var t=new Array,n="placekitten.com",r;if(e==n){t=[false,"g"]}else{t=["453f35","e7cead","b5ab94","eba434","64886c","b15c3a","b1956c"]}r=t.length;r=this._randomizer(r);r=t[r];if(r){if(e==n){r=r+"/"}else{r="/"+r+"/fff"}}else{r=""}return r},_datePicker:function(e){var t=this.options.calendar,n=new Date,r=false;if(e.split("-").length>1){e=e.split("-");if(e[1].toLowerCase()=="r"){r=true}e=e[0]}if(e==t.dayNumberZeros||e==t.dayTextThree||e==t.dayNumber||e==t.dayText){if(e==t.dayTextThree||e==t.dayText||e==t.dayTextThree){n=n.getDay();if(r===true){n=this._randomizer(6)}switch(n){case 0:n="Sunday";break;case 1:n="Monday";break;case 2:n="Tuesday";break;case 3:n="Wednesday";break;case 4:n="Thursday";break;case 5:n="Friday";break;case 6:n="Saturday";break;default:break}if(e==t.dayTextThree){n=n.substr(0,3)}}else{n=n.getDate();if(r===true){n=this._randomizer(28)+1}if(e==t.dayNumberZeros&&n<10){n="0"+n.toString()}}}else if(e==t.monthNumberZeros||e==t.monthTextThree||e==t.monthText||e==t.monthNumber){n=n.getMonth();if(r===true){n=this._randomizer(12)}if(e==t.monthText||e==t.monthTextThree){switch(n){case 0:n="January";break;case 1:n="February";break;case 2:n="March";break;case 3:n="April";break;case 4:n="May";break;case 5:n="June";break;case 6:n="July";break;case 7:n="August";break;case 8:n="September";break;case 9:n="October";break;case 10:n="November";break;case 11:n="December";break;default:break}if(e==t.monthTextThree){n=n.substr(0,3)}}else if(e==t.monthNumberZeros){n="0"+(n+1).toString()}else{n+=1}}else{n=n.getFullYear();if(r===true){n=this._randomizer(25);n<10?n="0"+n.toString():n=n.toString();n="20"+n}if(e==t.yearNumberTwo){n=n.toString().substr(2,3)}}return n}};e.fn.roughDraft=function(t){return this.each(function(){e.data(this,"roughdraft",new e.RedPen(t,this))})}})(jQuery,window,document);
Something went wrong with that request. Please try again.