Dashboard sipadu mbip
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

jquery.postitall.js 169KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800
  1. /**
  2. * jquery.postitall.js v1.0
  3. * jQuery Post It All Plugin - released under MIT License
  4. * Author: Javi Filella <txusko@gmail.com>
  5. * http://github.com/txusko/PostItAll
  6. * Copyright (c) 2015 Javi Filella
  7. *
  8. * Permission is hereby granted, free of charge, to any person
  9. * obtaining a copy of this software and associated documentation
  10. * files (the "Software"), to deal in the Software without
  11. * restriction, including without limitation the rights to use,
  12. * copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the
  14. * Software is furnished To Do so, subject to the following
  15. * conditions:
  16. *
  17. * The above copyright notice and this permission notice shall be
  18. * included in all copies or substantial portions of the Software.
  19. *
  20. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  22. * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  24. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  25. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  26. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  27. * OTHER DEALINGS IN THE SOFTWARE.
  28. *
  29. */
  30. //Delay repetitive actions
  31. var delay = (function(){
  32. var timer = 0;
  33. return function(callback, ms){
  34. clearTimeout (timer);
  35. timer = setTimeout(callback, ms);
  36. };
  37. })();
  38. (function ($, $localStorage) {
  39. "use strict";
  40. // Debug
  41. var debugging = true; // or true
  42. if (typeof console === "undefined") {
  43. console = {
  44. log: function () { return undefined; }
  45. };
  46. } else if (!debugging || console.log === undefined) {
  47. console.log = function () { return undefined; };
  48. }
  49. //Date
  50. Date.prototype.addDays = function(days) {
  51. var dat = new Date(this.valueOf());
  52. dat.setDate(dat.getDate() + days);
  53. return dat;
  54. };
  55. Date.prototype.addHours = function(hours) {
  56. var dat = new Date(this.valueOf());
  57. dat.setTime(dat.getTime() + (hours*60*60*1000));
  58. return dat;
  59. };
  60. Date.prototype.addMinutes = function(minutes) {
  61. var dat = new Date(this.valueOf());
  62. dat.setTime(dat.getTime() + (minutes*60*1000));
  63. return dat;
  64. };
  65. Date.prototype.addSeconds = function(seconds) {
  66. var dat = new Date(this.valueOf());
  67. dat.setTime(dat.getTime() + (seconds*1000));
  68. return dat;
  69. };
  70. Date.prototype.toFormat = function (f) {
  71. var nm = this.getMonthName();
  72. var nd = this.getDayName();
  73. f = f.replace(/yyyy/g, this.getFullYear());
  74. f = f.replace(/yy/g, String(this.getFullYear()).substr(2,2));
  75. f = f.replace(/MMM/g, nm.substr(0,3).toUpperCase());
  76. f = f.replace(/Mmm/g, nm.substr(0,3));
  77. f = f.replace(/MM\*/g, nm.toUpperCase());
  78. f = f.replace(/Mm\*/g, nm);
  79. f = f.replace(/mm/g, String(this.getMonth()+1).padLeft('0',2));
  80. f = f.replace(/DDD/g, nd.substr(0,3).toUpperCase());
  81. f = f.replace(/Ddd/g, nd.substr(0,3));
  82. f = f.replace(/DD\*/g, nd.toUpperCase());
  83. f = f.replace(/Dd\*/g, nd);
  84. f = f.replace(/dd/g, String(this.getDate()).padLeft('0',2));
  85. f = f.replace(/d\*/g, this.getDate());
  86. return f;
  87. };
  88. Date.prototype.getMonthName = function () {
  89. return this.toLocaleString().replace(/[^a-z]/gi,'');
  90. };
  91. //n.b. this is sooo not i18n safe :)
  92. Date.prototype.getDayName = function () {
  93. switch(this.getDay())
  94. {
  95. case 0: return 'Sunday';
  96. case 1: return 'Monday';
  97. case 2: return 'Tuesday';
  98. case 3: return 'Wednesday';
  99. case 4: return 'Thursday';
  100. case 5: return 'Friday';
  101. case 6: return 'Saturday';
  102. }
  103. };
  104. String.prototype.padLeft = function (value, size) {
  105. var x = this;
  106. while (x.length < size) {x = value + x;}
  107. return x;
  108. };
  109. // PLUGIN Public methods
  110. $.extend($.fn, {
  111. postitall: function (method, data, callback) {
  112. var t = new PostItAll();
  113. var elem = $('.PIAeditable').find(this);
  114. //.filter(function(){ return !$(this).parents('#the_notes').length })
  115. if(elem.length <= 0)
  116. elem = $(this);
  117. elem = elem.filter(function(){ return !$(this).parents('#the_lights').length })
  118. switch (method) {
  119. // Destroy the control
  120. case 'destroy':
  121. elem.each(function (i, e) {
  122. if($(e).hasClass('PIApostit')) {
  123. t.destroy($(e));
  124. } else if($(e).attr('PIA-original') !== undefined) {
  125. t.destroy($(e).parent().parent().parent().parent());
  126. }
  127. });
  128. return $(this);
  129. // Get/set options on the fly
  130. case 'options':
  131. // Setter
  132. //console.log('options', elem.length, data);
  133. var obj = undefined;
  134. elem.each(function (i, e) {
  135. if($(e).hasClass('PIApostit')) {
  136. if (data === undefined) {
  137. obj = $(e).data('PIA-options');
  138. return false;
  139. } else {
  140. t.destroy($(e), false);
  141. var tempoptions = $(e).data('PIA-options') || {};
  142. setTimeout(function() {$.PostItAll.new($.extend(true, tempoptions, data));}, 400);
  143. }
  144. } else if($(e).attr('PIA-original') !== undefined) {
  145. var oldObj = $(e).parent().parent().parent().parent();
  146. if (data === undefined) {
  147. obj = oldObj.data('PIA-options');
  148. return false;
  149. } else {
  150. t.destroy(oldObj, false);
  151. var tempoptions = oldObj.data('PIA-options') || {};
  152. setTimeout(function() { $.PostItAll.new($.extend(true, tempoptions, data)); }, 400);
  153. }
  154. }
  155. });
  156. if(obj !== undefined) {
  157. if($(obj).length == 1)
  158. return $(obj)[0];
  159. return $(obj);
  160. }
  161. return $(this);
  162. // hide note/s
  163. case 'hide':
  164. elem.each(function (i, e) {
  165. if($(e).hasClass('PIApostit')) {
  166. t.hide($(e).data('PIA-id'));
  167. } else if($(e).attr('PIA-original') !== undefined) {
  168. t.hide($(e).parent().parent().parent().parent().data('PIA-id'));
  169. }
  170. });
  171. return $(this);
  172. //show note/s
  173. case 'show':
  174. elem.each(function (i, e) {
  175. if($(e).hasClass('PIApostit')) {
  176. t.show($(e).data('PIA-id'));
  177. } else if($(e).attr('PIA-original') !== undefined) {
  178. t.show($(e).parent().parent().parent().parent().data('PIA-id'));
  179. }
  180. });
  181. return $(this);
  182. // Save object
  183. case 'save':
  184. elem.each(function (i, e) {
  185. if($(e).hasClass('PIApostit')) {
  186. t.save($(e));
  187. } else if($(e).attr('PIA-original') !== undefined) {
  188. t.save($(e).parent().parent().parent().parent());
  189. }
  190. });
  191. return $(this);
  192. // Initializes the control
  193. case 'new':
  194. default:
  195. var posX = 0, posY = 0, paso = false;
  196. if (method !== 'new') {
  197. data = method;
  198. method = "new";
  199. }
  200. if(data === undefined || typeof data !== 'object') data = { };
  201. //Position defined by the user
  202. if(!paso) {
  203. if(data.posX !== undefined) {
  204. posX = data.posX;
  205. paso = true;
  206. }
  207. if(data.posY !== undefined) {
  208. posY = data.posY;
  209. paso = true;
  210. }
  211. }
  212. //Check if initialized
  213. var initialized = false;
  214. $.each($(this).filter(function(){ return !$(this).parents('#the_notes').length }), function (i,e) {
  215. if($(e).attr('PIA-original') !== undefined) {
  216. initialized = true;
  217. return false;
  218. }
  219. });
  220. if(!initialized) {
  221. //Create the element/s
  222. $.each($(this).filter(function(){ return !$(this).parents('#the_notes').length }), function (i,e) {
  223. //Position relative to the element
  224. if(!paso) {
  225. posX = $(this).offset().left;
  226. posY = $(this).offset().top;
  227. }
  228. $.extend(data, { posX: posX, posY: posY }, true);
  229. //console.log('create', i, e);
  230. $.PostItAll.new('', data, $(e), callback);
  231. });
  232. } else {
  233. //Show previously initialized, show the notes
  234. $.PostItAll.show();
  235. }
  236. return $(this);
  237. }
  238. }
  239. });
  240. //Global vars : enable and disable features and change the notes behaviour
  241. $.fn.postitall.globals = {
  242. PIApostit_ : '#PIApostit_',//Id note prefixe
  243. filter : 'domain', //Options: domain, page, all
  244. savable : false, //Save postit in storage
  245. randomColor : true, //Random color in new postits
  246. toolbar : true, //Show or hide toolbar
  247. autoHideToolBar : true, //Animation efect on hover over postit shoing/hiding toolbar options
  248. removable : true, //Set removable feature on or off
  249. askOnDelete : true, //Confirmation before note remove
  250. draggable : true, //Set draggable feature on or off
  251. resizable : true, //Set resizable feature on or off
  252. editable : true, //Set contenteditable and enable changing note content
  253. changeoptions : true, //Set options feature on or off
  254. blocked : true, //Postit can not be modified
  255. hidden : true, //The note can be hidden
  256. minimized : true, //true = minimized, false = maximixed
  257. expand : true, //Expand note
  258. fixed : true, //Allow to fix the note in page
  259. addNew : true, //Create a new postit
  260. showInfo : true, //Show info icon (info tab)
  261. showMeta : true, //Show info icon (meta tab)
  262. pasteHtml : true, //Allow paste html in contenteditor
  263. htmlEditor : true, //Html editor (trumbowyg)
  264. autoPosition : true, //Automatic reposition of the notes when user resize screen
  265. addArrow : 'back', //Add arrow to notes : none, front, back, all
  266. askOnHide : true, //Show configuration hideUntil back-panel (getBackPanelHideUntil)
  267. hideUntil : null, //Note will be hidden since that datetime
  268. export : false //Note can be exported
  269. };
  270. //Copy of the original global configuration
  271. $.fn.postitall.globalscopy = $.extend({}, $.fn.postitall.globals, true);
  272. //Note global vars : Properties, style, features and events of the note
  273. $.fn.postitall.defaults = {
  274. //Note properties
  275. id : "", //Note id
  276. created : Date.now(), //Creation date
  277. domain : window.location.origin, //Domain in the url
  278. page : window.location.pathname, //Page in the url
  279. osname : navigator.appVersion, //Browser informtion & OS name,
  280. content : '', //Content of the note (text or html)
  281. position : 'absolute', //Position relative, fixed or absolute
  282. posX : '10px', //x coordinate (from left)
  283. posY : '10px', //y coordinate (from top)
  284. right : '', //x coordinate (from right). This property invalidate posX
  285. height : 200, //Note total height
  286. width : 160, //Note total width
  287. minHeight : 200, //Note resizable min-width
  288. minWidth : 160, //Note resizable min-height
  289. oldPosition : {}, //Position when minimized/collapsed (internal use)
  290. //Config note style
  291. style : {
  292. tresd : true, //General style in 3d format
  293. backgroundcolor : '#FFFA3C', //Background color in new postits when randomColor = false
  294. textcolor : '#333333', //Text color
  295. textshadow : true, //Shadow in the text
  296. fontfamily : 'Open Sans', //Default font
  297. fontsize : 'medium', //Default font size
  298. arrow : 'none', //Default arrow : none, top, right, bottom, left
  299. },
  300. //Enable / Disable features
  301. features : $.extend({}, $.fn.postitall.globals, true),
  302. //Note flags
  303. flags : {
  304. blocked : false, //If true, the note cannot be edited
  305. minimized : false, //true = Collapsed note / false = maximixed
  306. expand : false, //true = Expanded note / false = normal
  307. fixed : false, //Set position fixed
  308. highlight : false, //Higlight note
  309. hidden : false, //Hidden note
  310. },
  311. //Attach the note to al html element
  312. attachedTo : {
  313. element : '', //Where to attach (string or object / '#idObject' or $('#idObject'))
  314. position : 'right', //Position relative to elemente : top, right, bottom or left
  315. fixed : true, //Fix note to element when resize screen
  316. arrow : true, //Show an arrow in the inverse position
  317. },
  318. //Meta data
  319. meta: {
  320. 'Title': {
  321. 'type': 'input',
  322. 'maxlength': '20',
  323. 'value': '',
  324. 'placeholder': 'Note title'
  325. },
  326. 'Category': {
  327. 'type': 'combo',
  328. 'value': '0',
  329. 'values': {
  330. '0': 'Select a category',
  331. '1': 'Personal',
  332. '2': 'Work',
  333. '3': 'Other'
  334. }
  335. },
  336. 'Observations': {
  337. 'type': 'textarea',
  338. 'value': '',
  339. 'placeholder': 'Other considerations ...'
  340. }
  341. },
  342. // Callbacks / Event Handlers
  343. onCreated: function(id, options, obj) { return undefined; }, //Triggered after note creation
  344. onChange: function (id) { return undefined; }, //Triggered on each change
  345. onSelect: function (id) { return undefined; }, //Triggered when note is clicked, dragged or resized
  346. onDblClick: function (id) { return undefined; }, //Triggered on double click
  347. onRelease: function (id) { return undefined; }, //Triggered on the end of dragging and resizing of a note
  348. onDelete: function (id) { return undefined; } //Triggered when a note is deleted
  349. };
  350. //Copy of the original note configuration
  351. $.fn.postitall.defaultscopy = $.extend({}, $.fn.postitall.defaults, true);
  352. $.fn.postitall.defaultscopy.style = $.extend({}, $.fn.postitall.defaults.style, true);
  353. $.fn.postitall.defaultscopy.features = $.extend({}, $.fn.postitall.defaults.features, true);
  354. $.fn.postitall.defaultscopy.flags = $.extend({}, $.fn.postitall.defaults.flags, true);
  355. $.fn.postitall.defaultscopy.attachedTo = $.extend({}, $.fn.postitall.defaults.attachedTo, true);
  356. //Global functions
  357. jQuery.PostItAll = {
  358. //Initialize environement
  359. __initialize : function() {
  360. //The notes
  361. if($('#the_notes').length <= 0) {
  362. $('<div id="the_notes"></div>').appendTo($('.sticky-card'));
  363. }
  364. //The ligths
  365. if($('#the_lights').length <= 0) {
  366. $('<div id="the_lights"><div id="the_lights_close"></div></div>').appendTo($('.card-block'));
  367. $('#the_lights').click(function() {
  368. var note = new PostItAll();
  369. note.switchOnLights();
  370. });
  371. }
  372. //Upload note
  373. if($('#the_imports').length <= 0) {
  374. //Import file
  375. var importFile = function() {
  376. //Check support
  377. if (!window.FileReader) {
  378. console.log('Browser do not support FileReader.')
  379. return;
  380. }
  381. //Get file
  382. var input = $('#idImportFile').get(0);
  383. if (input.files.length) {
  384. //Get text
  385. var textFile = input.files[0];
  386. if(textFile.size > 0 && textFile.type == "text/plain") {
  387. //Create a new FileReader & read content
  388. var reader = new FileReader();
  389. reader.readAsText(textFile);
  390. $(reader).on('load', function(e) {
  391. //Text readed
  392. var file = e.target.result,results;
  393. if (file && file.length) {
  394. //Check object
  395. var obj = JSON.parse(file);
  396. if(typeof obj === 'object') {
  397. //create new note
  398. var newNote = function(obj) {
  399. delete obj.id; //reset id
  400. //TODO : reset domain&page or redirect?
  401. delete obj.domain;
  402. delete obj.page;
  403. $.PostItAll.new(obj);
  404. }
  405. if(obj.id !== undefined) {
  406. //One note
  407. newNote(obj);
  408. } else if($(obj).length > 0) {
  409. //Various notes
  410. $(obj).each(function(n1,obj2) {
  411. if(obj2.id !== undefined) {
  412. setTimeout(function() {
  413. newNote(obj2);
  414. }, 250 + ( n1 * 250 ));
  415. }
  416. });
  417. } else {
  418. alert('No notes to import');
  419. }
  420. } else {
  421. alert('Invalid file content');
  422. }
  423. }
  424. });
  425. } else {
  426. alert('The file is empty or is not a text file.');
  427. }
  428. } else {
  429. alert('Please upload a file before continuing.')
  430. }
  431. };
  432. //The imports
  433. var updString = $("<div />", { 'the_imports': '', 'style' : 'display:none;' });
  434. //Input fle
  435. var impFile = $('<input />', { id : "idImportFile", type : "file", name : "files" }).on("change", function () {
  436. $('#idImportUpload').click();
  437. $('#idImportFile').val("");
  438. });
  439. //Input button
  440. var impBut = $('<button />', { id : "idImportUpload", type : "file", name : "files" }).on("click", importFile);
  441. updString.append(impFile).append(impBut).prependTo($('body'));
  442. }
  443. },
  444. //Change configuration : type (global, note), opt (object)
  445. changeConfig : function(type, opt) {
  446. if(typeof type === 'string') {
  447. if(type == "global") {
  448. if(typeof opt === 'object')
  449. $.extend($.fn.postitall.globals, opt, true);
  450. return $.fn.postitall.globals;
  451. } else if(type == "note") {
  452. if(typeof opt === 'object') {
  453. if(opt.style !== undefined) {
  454. $.extend($.fn.postitall.defaults.style, opt.style, true);
  455. delete opt.style;
  456. }
  457. if(opt.features !== undefined) {
  458. $.extend($.fn.postitall.defaults.features, opt.features, true);
  459. delete opt.features;
  460. }
  461. if(opt.flags !== undefined) {
  462. $.extend($.fn.postitall.defaults.flags, opt.flags, true);
  463. delete opt.flags;
  464. }
  465. if(opt.attachedTo !== undefined) {
  466. $.extend($.fn.postitall.defaults.attachedTo, opt.attachedTo, true);
  467. delete opt.attachedTo;
  468. }
  469. $.extend($.fn.postitall.defaults, opt, true);
  470. }
  471. return $.fn.postitall.defaults;
  472. }
  473. }
  474. return null;
  475. },
  476. //Retore configuration to factory defaults : type (all, global, note. default: all)
  477. restoreConfig : function(type) {
  478. if(type === undefined)
  479. type = "all";
  480. if(typeof type === "string") {
  481. if(type == "global" || type == "all") {
  482. $.extend($.fn.postitall.globals, $.fn.postitall.globalscopy, true);
  483. return $.fn.postitall.globals;
  484. }
  485. if(type == "note" || type == "all") {
  486. $.extend($.fn.postitall.defaults, $.fn.postitall.defaultscopy, true);
  487. return $.fn.postitall.defaults;
  488. }
  489. }
  490. return null;
  491. },
  492. storageManager : function(callback) {
  493. storageManager.getStorageManager(function(e) {
  494. callback(e);
  495. });
  496. },
  497. //New note
  498. new : function(content, opt, obj, callback) {
  499. var ok = false;
  500. if(callback !== undefined) {
  501. ok = true;
  502. }
  503. if(!ok && obj !== undefined && typeof obj === 'function') {
  504. callback = obj;
  505. obj = undefined;
  506. ok = true;
  507. }
  508. if(!ok && opt !== undefined && typeof opt === 'function' && content !== undefined && typeof content === 'object') {
  509. callback = opt;
  510. opt = content;
  511. content = "";
  512. ok = true;
  513. }
  514. if(!ok && opt !== undefined && typeof opt === 'function' && content !== undefined && typeof content === 'string') {
  515. callback = opt;
  516. opt = undefined;
  517. ok = true;
  518. }
  519. if (!ok && typeof content === 'object') {
  520. callback = obj;
  521. obj = opt;
  522. opt = content;
  523. content = "";
  524. ok = true;
  525. }
  526. if (!ok && typeof content === 'function') {
  527. callback = content;
  528. content = "";
  529. ok = true;
  530. }
  531. if(!ok && content === undefined) {
  532. content = "";
  533. ok = true;
  534. }
  535. //Position
  536. var optPos = {};
  537. optPos.posX = parseInt(window.pageXOffset, 10);
  538. optPos.posY = parseInt(window.pageYOffset, 10);
  539. optPos.use = false;
  540. //console.log('init opt', $.fn.postitall.defaults);
  541. if(opt === undefined) {
  542. opt = $.extend(true, {}, $.fn.postitall.defaults);
  543. opt.posX = optPos.posX;
  544. opt.posY = optPos.posY;
  545. optPos.use = true;
  546. } else {
  547. if(opt.posX === undefined && opt.posX === undefined) {
  548. opt.posX = optPos.posX;
  549. opt.posY = optPos.posY;
  550. optPos.use = true;
  551. }
  552. opt = $.extend(true, {}, $.fn.postitall.defaults, opt);
  553. }
  554. if(opt.position == "relative" || opt.position == "fixed") {
  555. if(!optPos.use) {
  556. opt.posX = parseInt(opt.posX, 10) + parseInt(optPos.posX, 10);
  557. opt.posY = parseInt(opt.posY, 10) + parseInt(optPos.posY, 10);
  558. }
  559. if(opt.position == "fixed") {
  560. opt.flags.fixed = true;
  561. }
  562. opt.position = "absolute";
  563. }
  564. if(optPos.use) {
  565. opt.posX = optPos.posX + parseInt($.fn.postitall.defaults.posX, 10);
  566. opt.posY = optPos.posY + parseInt($.fn.postitall.defaults.posX, 10);
  567. }
  568. opt.posX = parseInt(opt.posX, 10) + "px";
  569. opt.posY = parseInt(opt.posY, 10) + "px";
  570. //New note object
  571. var note = new PostItAll();
  572. if(obj === undefined) {
  573. obj = $('<div />', {
  574. html: (content !== undefined ? content : '')
  575. }).hide();
  576. } else {
  577. var oldObj = obj;
  578. $(oldObj).attr('PIA-original', '1');
  579. var newObj = $('<div />').append(oldObj);
  580. $(newObj).attr('PIA-original', '1');
  581. obj = newObj;
  582. }
  583. $('#the_notes').append(obj);
  584. //Random color
  585. var randCol = function(opts) {
  586. //Random bg & textcolor
  587. if($.fn.postitall.globals.randomColor && opts.features.randomColor) {
  588. if(opts.style.backgroundcolor === $.fn.postitall.defaults.style.backgroundcolor) {
  589. opts.style.backgroundcolor = note.getRandomColor();
  590. }
  591. if(opts.style.textcolor === $.fn.postitall.defaults.style.textcolor) {
  592. opts.style.textcolor = note.getTextColor(opts.style.backgroundcolor);
  593. }
  594. opts.features.randomColor = false;
  595. }
  596. return opts;
  597. };
  598. //Check if we have the id
  599. var options = opt;
  600. //console.log('options', options);
  601. if(options.id !== "") {
  602. //Random bg & textcolor
  603. options = randCol(options);
  604. //Initialize
  605. setTimeout(function() { note.init(obj, options); if(callback !== undefined) callback($.fn.postitall.globals.prefix + options.id, options, obj[0]); }, 100);
  606. } else {
  607. //Get new id
  608. //console.log('paso');
  609. note.getIndex(($.fn.postitall.globals.savable || options.features.savable), function(index) {
  610. //Id
  611. options.id = index;
  612. //Random bg & textcolor
  613. options = randCol(options);
  614. //Initialize
  615. setTimeout(function() { note.init(obj, options); if(callback !== undefined) callback($.fn.postitall.globals.prefix + options.id, options, obj[0]); }, 100);
  616. });
  617. }
  618. },
  619. options : function(id, opt) {
  620. if(typeof id === 'object') {
  621. //Change options for all notes
  622. $('.PIApostit').postitall('options', id);
  623. } else if (typeof opt === 'object') {
  624. //Change options for specific notes
  625. $(id).postitall('options', opt);
  626. } else {
  627. return $(id).postitall('options');
  628. }
  629. },
  630. //Hide all
  631. hide : function(id) {
  632. this.toggle(id, 'hide');
  633. },
  634. //Show all
  635. show : function(id) {
  636. this.toggle(id, 'show');
  637. },
  638. //hide/show all
  639. toggle : function(id, action) {
  640. var paso = false;
  641. if(action === undefined) action = "show";
  642. if(id !== undefined && typeof id === 'string') {
  643. if($($.fn.postitall.globals.prefix + id).length > 0) {
  644. $($.fn.postitall.globals.prefix + id).postitall(action);
  645. paso = true;
  646. } else if($(id.toString()).length) {
  647. $(id.toString()).postitall(action);
  648. paso = true;
  649. }
  650. }
  651. if(!paso) {
  652. $('.PIApostit').each(function () {
  653. $(this).postitall(action);
  654. });
  655. }
  656. },
  657. //Get all notes
  658. getNotes : function(callback, filtered) {
  659. var len = -1;
  660. var iteration = 0;
  661. var finded = false;
  662. var notes = [];
  663. if(typeof filtered === 'undefined')
  664. filtered = $.fn.postitall.globals.filter;
  665. storageManager.getlength(function(len) {
  666. if(!len) {
  667. if(typeof callback === 'function') callback(notes);
  668. return;
  669. }
  670. for (var i = 1; i <= len; i++) {
  671. storageManager.key(i, function(key) {
  672. storageManager.getByKey(key, function(o) {
  673. if (o != null) {
  674. if(filtered == "domain")
  675. finded = (o.domain === window.location.origin);
  676. else if(filtered == "page")
  677. finded = (o.domain === window.location.origin && o.page === window.location.pathname);
  678. else
  679. finded = true;
  680. if(finded) {
  681. notes.push(o);
  682. }
  683. }
  684. if(iteration == (len - 1) && callback != null) {
  685. if(typeof callback === 'function') callback(notes);
  686. callback = null;
  687. }
  688. iteration++;
  689. });
  690. });
  691. }
  692. });
  693. },
  694. //Load all (from storage)
  695. load : function(callback, callbacks, highlight) {
  696. var len = -1;
  697. var iteration = 0;
  698. $.PostItAll.getNotes(function(notes) {
  699. if(notes.length > 0) {
  700. len = notes.length;
  701. $(notes).each(function(i,o) {
  702. if($($.fn.postitall.globals.prefix + o.id).length <= 0) {
  703. if(typeof callbacks === 'object') {
  704. //console.log(callbacks);
  705. if(callbacks.onCreated !== undefined) {
  706. o.onCreated = callbacks.onCreated;
  707. }
  708. if(callbacks.onChange !== undefined) {
  709. o.onChange = callbacks.onChange;
  710. }
  711. if(callbacks.onSelect !== undefined) {
  712. o.onSelect = callbacks.onSelect;
  713. }
  714. if(callbacks.onDblClick !== undefined) {
  715. o.onDblClick = callbacks.onDblClick;
  716. }
  717. if(callbacks.onRelease !== undefined) {
  718. o.onRelease = callbacks.onRelease;
  719. }
  720. if(callbacks.onDelete !== undefined) {
  721. o.onDelete = callbacks.onDelete;
  722. }
  723. }
  724. if(o.flags !== undefined) {
  725. o.flags.highlight = false;
  726. if(highlight !== undefined && o.id == highlight) {
  727. //console.log('highlight note', highlight);
  728. o.flags.highlight = true;
  729. }
  730. }
  731. $.PostItAll.new(o);
  732. }
  733. if(iteration == (len - 1) && callback != null) {
  734. if(typeof callback === 'function') callback();
  735. callback = null;
  736. }
  737. iteration++;
  738. });
  739. } else {
  740. if(typeof callback === 'function') callback();
  741. return;
  742. }
  743. });
  744. },
  745. //Save all (to storage)
  746. save : function() {
  747. //if(!$.fn.postitall.globals.savable && )
  748. // return;
  749. var options;
  750. var id;
  751. $('.PIApostit').each(function(i,e) {
  752. id = $(e).data('PIA-id');
  753. options = $(e).data('PIA-options');
  754. if(id !== undefined && options !== undefined && ($.fn.postitall.globals.savable || options.features.savable)) {
  755. $(this).postitall('save');
  756. }
  757. });
  758. },
  759. //Get number of notes (only in storage)
  760. length : function(callback) {
  761. $.PostItAll.getNotes(function(notes) {
  762. if(callback !== undefined) callback(notes.length);
  763. });
  764. },
  765. //Remove all notes
  766. remove : function(delInline, delStorage, delDomain) {
  767. this.destroy(false, delInline, delStorage, delDomain);
  768. },
  769. delete : function(delInline, delStorage, delDomain) {
  770. this.destroy(false, delInline, delStorage, delDomain);
  771. },
  772. destroy : function(id, delInline, delStorage, delDomain) {
  773. if(delInline == undefined)
  774. delInline = true;
  775. if(delStorage == undefined)
  776. delStorage = true;
  777. if(delInline) {
  778. //Visible notes
  779. if(id !== undefined && typeof id === "string") {
  780. //Specific note
  781. var opt = $(id).postitall('options');
  782. if(delStorage) { // && ($.fn.postitall.globals.savable || opt.features.savable)) {
  783. $(id).postitall('destroy');
  784. } else {
  785. $(id).postitall('hide');
  786. }
  787. } else {
  788. //All notes
  789. $('.PIApostit').each(function () {
  790. var opt = $(this).postitall('options');
  791. if(delStorage) { // && ($.fn.postitall.globals.savable || opt.features.savable)) {
  792. $(this).postitall('destroy');
  793. } else {
  794. $(this).postitall('hide');
  795. }
  796. });
  797. }
  798. }
  799. //TODO : Revisar
  800. if(delStorage && delDomain !== "" && delDomain !== undefined) { //&& $.fn.postitall.globals.savable
  801. //Storage notes
  802. console.log('clearStorage',delDomain);
  803. $.PostItAll.clearStorage(delDomain);
  804. }
  805. },
  806. clearStorage : function(delDomain, callback) {
  807. if(delDomain !== undefined) {
  808. //Delete notes of an specific domain
  809. storageManager.removeDom({ domain : delDomain }, function() {
  810. console.log("Storage cleared for domain", delDomain);
  811. if(callback !== undefined) callback();
  812. });
  813. } else {
  814. //Delete all notes
  815. storageManager.clear(function() {
  816. console.log("Storage cleared");
  817. if(callback !== undefined) callback();
  818. });
  819. }
  820. },
  821. //Import note
  822. import : function() {
  823. //Show upload dialog
  824. $('#idImportFile').trigger('click');
  825. },
  826. //Export note
  827. export : function(opt) {
  828. var obj = null;
  829. if(typeof opt === 'undefined') {
  830. opt = "loaded";
  831. }
  832. if(typeof opt === 'string') {
  833. switch(opt) {
  834. case "all":
  835. case "domain":
  836. case "page":
  837. obj = [];
  838. $.PostItAll.getNotes(function(notes, opt) {
  839. if(notes.length > 0) {
  840. $(notes).each(function(i,o) {
  841. var obj2 = $.PostItAll.options($.fn.postitall.globals.prefix + o.id);
  842. obj.push(obj2);
  843. });
  844. }
  845. });
  846. break;
  847. case "loaded":
  848. obj = [];
  849. $('.PIApostit').each(function(i,e) {
  850. var obj2 = $.PostItAll.options($.fn.postitall.globals.prefix + $(e).data('PIA-id'));
  851. obj.push(obj2);
  852. });
  853. break;
  854. default:
  855. //Get options
  856. obj = $.PostItAll.options($.fn.postitall.globals.prefix + opt);
  857. break;
  858. }
  859. setTimeout(function() {
  860. if(obj != null && (obj.id !== undefined || obj.length > 0)) {
  861. var dat = new Date();
  862. //Create element
  863. var element = document.createElement('a');
  864. element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(JSON.stringify(obj)));
  865. element.setAttribute('download', dat.toFormat("yyyymmdd") + $.fn.postitall.globals.prefix + opt + '.txt');
  866. element.style.display = 'none';
  867. //Append element to body
  868. document.body.appendChild(element);
  869. //Trigger download
  870. element.click();
  871. //Remove temporal element
  872. document.body.removeChild(element);
  873. } else {
  874. alert('No notes to export');
  875. console.log(obj);
  876. }
  877. }, 250);
  878. }
  879. }
  880. };
  881. //Note class
  882. var PostItAll = function(obj, opt) {
  883. this.options = {};
  884. this.hoverState = false;
  885. };
  886. //Definition
  887. PostItAll.prototype = {
  888. // Initialize elements
  889. init : function(obj, opt) {
  890. //Default options
  891. this.options = $.extend({}, $.fn.postitall.defaults);
  892. //Set options
  893. if (typeof opt !== 'object') {
  894. opt = {};
  895. }
  896. this.setOptions(opt);
  897. // Do nothing if already initialized
  898. if (obj.data('PIA-initialized') || $($.fn.postitall.globals.prefix + opt.id).length > 0) {
  899. return;
  900. }
  901. //Modify page content
  902. opt = $.extend(this.options, opt);
  903. this.setOptions(opt);
  904. //obj id
  905. obj.attr('id', $.fn.postitall.globals.prefix.substring(1) + opt.id);
  906. //console.log('init');
  907. //this.attachedTo();
  908. //create stuff
  909. var newObj = this.create(obj);
  910. //Set on resize action
  911. if($.fn.postitall.globals.autoPosition && opt.features.autoPosition)
  912. $(window).on('resize', $.proxy(this.relativePosition, this));
  913. //return obj
  914. return newObj;
  915. },
  916. //Attach the note to a dom object
  917. attachedTo : function(options) {
  918. var t = this;
  919. var data = options;
  920. if(data === undefined) {
  921. data = t.options;
  922. }
  923. var id = data.id;
  924. var obj = $($.fn.postitall.globals.prefix + id);
  925. //Recovers object selector (http://stackoverflow.com/questions/2420970/how-can-i-get-selector-from-jquery-object/14093425#14093425)
  926. var get_selector = function (element) {
  927. var $el = element;
  928. var id = $el.attr("id");
  929. if (id) { //"should" only be one of these if theres an ID
  930. return "#"+ id;
  931. }
  932. var selector = $el.parents()
  933. .map(function() { return this.tagName; })
  934. .get().reverse().join(" ");
  935. if (selector) {
  936. selector += " "+ $el[0].nodeName;
  937. }
  938. var classNames = $el.attr("class");
  939. if (classNames) {
  940. selector += "." + $.trim(classNames).replace(/\s/gi, ".");
  941. }
  942. var name = $el.attr('name');
  943. if (name) {
  944. selector += "[name='" + name + "']";
  945. }
  946. if (!name){
  947. var index = $el.index();
  948. if (index) {
  949. index = index + 1;
  950. selector += ":nth-child(" + index + ")";
  951. }
  952. }
  953. return selector;
  954. };
  955. //Position relative to a dom object
  956. if(data.attachedTo === undefined || typeof data.attachedTo !== 'object') data.attachedTo = { };
  957. //Dom object to attach (mandatory)
  958. var objToAttach
  959. if(data.attachedTo.element !== undefined) {
  960. //DOM object
  961. if(typeof data.attachedTo.element === 'object') {
  962. //Get object selector
  963. data.attachedTo.element = get_selector(data.attachedTo.element);
  964. }
  965. //String object
  966. if(typeof data.attachedTo.element === 'string' && data.attachedTo.element !== "") {
  967. if($(''+data.attachedTo.element).length > 0) {
  968. objToAttach = $(''+data.attachedTo.element).first();
  969. } else {
  970. console.log('Object to attach not found in DOM.');
  971. return;
  972. }
  973. } else {
  974. console.log('Incorrect object to attach. Define a jQuery object or a DOM selector for the element property.');
  975. return;
  976. }
  977. } else {
  978. return;
  979. }
  980. var position = {};
  981. if(this.elementOrParentIsFixed(data.attachedTo.element))
  982. position = objToAttach.position();
  983. else
  984. position = objToAttach.offset();
  985. if(data.attachedTo.arrow === undefined)
  986. data.attachedTo.arrow = true;
  987. if(data.attachedTo.position === undefined || data.attachedTo.position === "")
  988. data.attachedTo.position = "right middle";
  989. var tmpPos = data.attachedTo.position.split(" ");
  990. var pos1 = tmpPos[0], pos2 = tmpPos[1];
  991. var objWidth = objToAttach.width() + parseInt(objToAttach.css('padding-left'),10) + parseInt(objToAttach.css('padding-right'),10);
  992. var objHeight = objToAttach.height() + parseInt(objToAttach.css('padding-top'),10) + parseInt(objToAttach.css('padding-bottom'),10);
  993. var noteWidth = parseInt(obj.width(), 10) + parseInt(obj.css('padding-left'),10) + parseInt(obj.css('padding-right'),10);
  994. var noteHeight = parseInt(obj.height(), 10) + parseInt(obj.css('padding-top'),10) + parseInt(obj.css('padding-bottom'),10);
  995. //Breakpoints / responsive behaviour (https://github.com/txusko/PostItAll/issues/11)
  996. var fixVertical = function(callback) {
  997. var scrollPosition = self.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;
  998. //console.log(data.posY, scrollPosition, (data.posY + data.height), (scrollPosition + $(window).height()));
  999. if(data.posY < scrollPosition) {
  1000. data.posY = position.top + 20;
  1001. if(data.attachedTo.arrow && data.style.arrow != "top") {
  1002. data.style.arrow = "top";
  1003. t.hideArrow(data.id);
  1004. }
  1005. if(callback != null) setTimeout(function() { callback(); }, 100);
  1006. } else if((data.posY + data.height) > (scrollPosition + $(window).height())) {
  1007. data.posY = position.top + objHeight - noteHeight - 20;
  1008. if(data.attachedTo.arrow && data.style.arrow != "bottom") {
  1009. data.style.arrow = "bottom";
  1010. t.hideArrow(data.id);
  1011. }
  1012. if(callback != null) setTimeout(function() { callback(); }, 100);
  1013. }
  1014. };
  1015. var fixHorizontal = function(callback) {
  1016. var dataPosX = data.posX;
  1017. if(pos1 == "right")
  1018. dataPosX = dataPosX + noteWidth;
  1019. //console.log(dataPosX, data.posX, $(window).width());
  1020. if(dataPosX < 0) {
  1021. data.posX = position.left + 20;
  1022. if(data.attachedTo.arrow && data.style.arrow != "left") {
  1023. data.style.arrow = "left";
  1024. t.hideArrow(data.id);
  1025. }
  1026. if(callback != null) setTimeout(function() { callback(); }, 100);
  1027. } else if(dataPosX > $(window).width()) {
  1028. data.posX = (position.left + objWidth) - data.width - 20 - getScrollWidth();
  1029. if(data.attachedTo.arrow && data.style.arrow != "right") {
  1030. data.style.arrow = "right";
  1031. t.hideArrow(data.id);
  1032. }
  1033. if(callback != null) setTimeout(function() { callback(); }, 100);
  1034. }
  1035. };
  1036. var getScrollWidth = function() {
  1037. var outer = $('<div>').css({visibility: 'hidden', width: 94, overflow: 'scroll'}).appendTo('body');
  1038. var widthWithScroll = $('<div>').css({width: '100%'}).appendTo(outer).outerWidth();
  1039. outer.remove();
  1040. return 100 - widthWithScroll;
  1041. }
  1042. var updateNote = function() {
  1043. if($($.fn.postitall.globals.prefix + data.id).length > 0) {
  1044. $($.fn.postitall.globals.prefix + data.id).animate({
  1045. 'top': data.posY,
  1046. 'left': data.posX
  1047. }, 200, function() {
  1048. if(data.style.arrow != "none") {
  1049. //t.hideArrow(data.id, function() {
  1050. t.showArrow(data.id, data);
  1051. //});
  1052. }
  1053. });
  1054. }
  1055. }
  1056. switch(pos1) {
  1057. case 'top':
  1058. data.posY = position.top - noteHeight - 20;
  1059. if(pos2 == "left") {
  1060. data.posX = (position.left + (objWidth * 0.1)) - (data.width / 2);
  1061. } else if(pos2 == "right") {
  1062. data.posX = (position.left + (objWidth - (objWidth * 0.1))) - (data.width / 2);
  1063. } else {
  1064. data.posX = (position.left + (objWidth / 2)) - (data.width / 2);
  1065. }
  1066. if(data.attachedTo.arrow && data.style.arrow != "bottom") {
  1067. data.style.arrow = "bottom";
  1068. t.hideArrow(data.id);
  1069. }
  1070. break;
  1071. case 'right':
  1072. default:
  1073. data.posX = position.left + objWidth + 20;
  1074. if(pos2 == "top") {
  1075. data.posY = (position.top + (objHeight * 0.1)) - (data.height / 2);
  1076. } else if(pos2 == "bottom") {
  1077. data.posY = (position.top + (objHeight - (objHeight * 0.1))) - (data.height / 2);
  1078. } else {
  1079. data.posY = (position.top + (objHeight / 2)) - (data.height / 2);
  1080. }
  1081. if(data.attachedTo.arrow && data.style.arrow != "left") {
  1082. data.style.arrow = "left";
  1083. t.hideArrow(data.id);
  1084. }
  1085. break;
  1086. case 'bottom':
  1087. data.posY = position.top + objHeight + 20;
  1088. if(pos2 == "left") {
  1089. data.posX = (position.left + (objWidth * 0.1)) - (data.width / 2);
  1090. } else if(pos2 == "right") {
  1091. data.posX = (position.left + (objWidth - (objWidth * 0.1))) - (data.width / 2);
  1092. } else {
  1093. data.posX = (position.left + (objWidth / 2)) - (data.width / 2);
  1094. }
  1095. if(data.attachedTo.arrow && data.style.arrow != "top") {
  1096. data.style.arrow = "top";
  1097. t.hideArrow(data.id);
  1098. }
  1099. break;
  1100. case 'left':
  1101. data.posX = position.left - noteWidth - 20;
  1102. if(pos2 == "top") {
  1103. data.posY = (position.top + (objHeight * 0.1)) - (data.height / 2);
  1104. } else if(pos2 == "bottom") {
  1105. data.posY = (position.top + (objHeight - (objHeight * 0.1))) - (data.height / 2);
  1106. } else {
  1107. data.posY = (position.top + (objHeight / 2)) - (data.height / 2);
  1108. }
  1109. if(data.attachedTo.arrow && data.style.arrow != "right") {
  1110. data.style.arrow = "right";
  1111. t.hideArrow(data.id);
  1112. }
  1113. break;
  1114. }
  1115. if(pos1 === "left" || pos1 === "right") {
  1116. fixHorizontal(function() {
  1117. fixVertical();
  1118. });
  1119. } else if(pos1 === "top" || pos1 === "bottom") {
  1120. fixVertical(function() {
  1121. fixHorizontal();
  1122. });
  1123. }
  1124. if(data.attachedTo.fixed === undefined)
  1125. data.attachedTo.fixed = true;
  1126. setTimeout(function() { updateNote(); }, 200);
  1127. },
  1128. //Save object
  1129. save : function(obj, callback) {
  1130. var options = obj.data('PIA-options');
  1131. if(!$.fn.postitall.globals.savable && !options.features.savable)
  1132. return;
  1133. //console.log('save', options);
  1134. options.features.savable = true;
  1135. this.saveOptions(options, callback);
  1136. },
  1137. //Save options
  1138. saveOptions : function(options, callback) {
  1139. if(options === undefined)
  1140. options = this.options;
  1141. //console.log('saveOptions', options.posX, options.posY, options.width, options.height);
  1142. if ($.fn.postitall.globals.savable || options.features.savable) {
  1143. //console.log(options);
  1144. storageManager.add(options, function(error) {
  1145. if(error != "") {
  1146. if(callback != null) callback(error);
  1147. else alert('Error saving options: ' + error);
  1148. } else {
  1149. if(callback != null) callback();
  1150. }
  1151. });
  1152. }
  1153. options.onChange($.fn.postitall.globals.prefix + options.id);
  1154. },
  1155. //Destroy and remove
  1156. destroy : function(obj, callOnDelete) {
  1157. //Swith on lights
  1158. //this.switchOnLights();
  1159. //this.disableKeyboardNav();
  1160. //console.log(obj);
  1161. if(obj === undefined)
  1162. obj = $($.fn.postitall.globals.prefix + this.options.id);
  1163. if(callOnDelete === undefined)
  1164. callOnDelete = true;
  1165. var options = obj.data('PIA-options');
  1166. //console.log(this,options, obj);
  1167. var id = options.id;
  1168. //console.log('destroy', id, $.fn.postitall.globals.savable, options.features.savable);
  1169. //Remove from localstorage
  1170. if ($.fn.postitall.globals.savable || options.features.savable) {
  1171. if(options.features.savable) {
  1172. storageManager.remove(id);
  1173. options.onChange($.fn.postitall.globals.prefix + id);
  1174. } else {
  1175. storageManager.get(id, function(varvalue) {
  1176. if(varvalue != null && varvalue != "")
  1177. storageManager.remove(id);
  1178. options.onChange($.fn.postitall.globals.prefix + id);
  1179. });
  1180. }
  1181. }
  1182. //Destroy object
  1183. this.remove(options);
  1184. //Event handler on delete
  1185. if(callOnDelete) {
  1186. options.onDelete($.fn.postitall.globals.prefix + id);
  1187. }
  1188. },
  1189. //Hide note
  1190. remove : function(options) {
  1191. //hide object
  1192. var id = options.id;
  1193. //console.log('remove', id);
  1194. $($.fn.postitall.globals.prefix + id).removeData('PIA-id')
  1195. .removeData('PIA-initialized')
  1196. .removeData('PIA-settings')
  1197. .animate({
  1198. opacity: 0,
  1199. height: 0
  1200. }, function() {
  1201. $(this).remove();
  1202. });
  1203. /*.hide("slow", function () {
  1204. $(this).remove();
  1205. });*/
  1206. if(this.length <= 0)
  1207. $(window).off('resize');
  1208. },
  1209. hide : function(id) {
  1210. //hide object
  1211. if($($.fn.postitall.globals.prefix + id).length) {
  1212. var options = $($.fn.postitall.globals.prefix + id).data('PIA-options');
  1213. options.flags.hidden = true;
  1214. $($.fn.postitall.globals.prefix + id).slideUp();
  1215. this.saveOptions(options);
  1216. }
  1217. },
  1218. show : function(id) {
  1219. //show object
  1220. if($($.fn.postitall.globals.prefix + id).length) {
  1221. var options = $($.fn.postitall.globals.prefix + id).data('PIA-options');
  1222. options.flags.hidden = false;
  1223. options.features.hideUntil = null;
  1224. $($.fn.postitall.globals.prefix + id).slideDown();
  1225. this.saveOptions(options);
  1226. }
  1227. },
  1228. //Show the note in a specific date
  1229. showAgain : function(index, dat) {
  1230. var t = this;
  1231. var setToHappen = function(fn, date){
  1232. var now = new Date().getTime();
  1233. var diff = date.getTime() - now;
  1234. return setTimeout(fn, diff);
  1235. };
  1236. var dateFinish = new Date(dat);
  1237. if(dateFinish > Date.now()) {
  1238. console.log('show again on ' + dateFinish, Date.now() );
  1239. setToHappen(function() {
  1240. t.show(index);
  1241. }, dateFinish);
  1242. } else {
  1243. t.show(index);
  1244. }
  1245. },
  1246. //When user change arrow
  1247. arrowChangeOption : function(value) {
  1248. var index = this.options.id;
  1249. var options = this.options;
  1250. if($.fn.postitall.globals.addArrow == "none" && options.features == "none") {
  1251. return;
  1252. }
  1253. //console.log('arrowChangeOption', options.style.arrow, value);
  1254. //Get new position
  1255. if(options.style.arrow == value || value == 'none') {
  1256. //If this position is the same as before, remove arrow and show selectors
  1257. options.style.arrow = 'none';
  1258. $('.selectedArrow_'+index).show();
  1259. $('.selectedArrow_'+index).find('span').show();
  1260. } else {
  1261. //Set arrow and hide selectors
  1262. options.style.arrow = value;
  1263. $('.selectedArrow_'+index).hide();
  1264. }
  1265. //Change options arrow select
  1266. $('#idAddArrow_'+index).val(options.style.arrow);
  1267. this.hideArrow(index);
  1268. this.showArrow(index, options);
  1269. this.saveOptions(options);
  1270. return options;
  1271. },
  1272. //Hide arrow & icons
  1273. hideArrow : function(ind, callback) {
  1274. var index = this.options.id;
  1275. if(ind !== undefined)
  1276. index = ind;
  1277. //Remove previous arrow
  1278. $($.fn.postitall.globals.prefix + index).removeClass('arrow_box_top arrow_box_right arrow_box_bottom arrow_box_left', 1000, "easeInElastic");
  1279. $($.fn.postitall.globals.prefix + index).find('.icon_box').hide();
  1280. if(!$.ui) $($.fn.postitall.globals.prefix + index).css('overflow', 'hidden').css('resize', 'both');
  1281. //console.log('hide');
  1282. if(callback !== undefined) setTimeout(function() { callback(); }, 100);
  1283. },
  1284. //Show arrow and icons
  1285. showArrow : function(ind, opt) {
  1286. var index = this.options.id;
  1287. if(ind !== undefined)
  1288. index = ind;
  1289. var options = this.options;
  1290. if(opt !== undefined)
  1291. options = opt;
  1292. //delay(function() {
  1293. //Add arrow
  1294. //console.log(options.style.arrow, ($.fn.postitall.globals.prefix + index));
  1295. switch(options.style.arrow) {
  1296. case 'top':
  1297. $($.fn.postitall.globals.prefix + index).addClass('arrow_box_top', 1000, "easeInElastic").css('overflow', '').css('resize', '');
  1298. break;
  1299. case 'right':
  1300. $($.fn.postitall.globals.prefix + index).addClass('arrow_box_right', 1000, "easeInElastic").css('overflow', '').css('resize', '');
  1301. break;
  1302. case 'bottom':
  1303. $($.fn.postitall.globals.prefix + index).addClass('arrow_box_bottom', 1000, "easeInElastic").css('overflow', '').css('resize', '');
  1304. break;
  1305. case 'left':
  1306. $($.fn.postitall.globals.prefix + index).addClass('arrow_box_left', 1000, "easeInElastic").css('overflow', '').css('resize', '');
  1307. break;
  1308. }
  1309. //console.log('options.style.arrow',options.style.arrow);
  1310. if(options.style.arrow == 'none')
  1311. $($.fn.postitall.globals.prefix + index).find('.icon_box').show();
  1312. var icon = $($.fn.postitall.globals.prefix + index).find('div[data-value="'+options.style.arrow+'"]');
  1313. icon.show();
  1314. icon.find('span').hide();
  1315. //console.log('show');
  1316. //}, 250);
  1317. },
  1318. //Autoresize note
  1319. autoresize : function() {
  1320. var t = this;
  1321. var id = this.options.id;
  1322. var options = this.options;
  1323. var obj = $($.fn.postitall.globals.prefix + id);
  1324. if(options.flags.minimized || options.flags.expand)
  1325. return;
  1326. var toolBarHeight = parseInt(obj.find('.PIAtoolbar').height(), 10);
  1327. var contentHeight = parseInt(obj.find('.PIAeditable').height(), 10) + toolBarHeight,
  1328. posX = obj.css('left'),
  1329. posY = obj.css('top'),
  1330. divWidth = parseInt(obj.css('width'), 10),//parseInt(obj.width(), 10) + parseInt(obj.css('padding-left'),10) + parseInt(obj.css('padding-right'),10) + 2,
  1331. divHeight = parseInt(obj.css('height'), 10),
  1332. minDivHeight = options.minHeight;
  1333. var htmlEditorBarHeight = parseInt(obj.find('.trumbowyg-button-pane').height(), 10);
  1334. if(isNaN(htmlEditorBarHeight) || htmlEditorBarHeight <= 30) {
  1335. htmlEditorBarHeight = 35;
  1336. }
  1337. //console.log('divHeight',divHeight,'contentHeight',contentHeight);
  1338. if(contentHeight > divHeight - 30) {
  1339. divHeight = contentHeight + htmlEditorBarHeight;
  1340. } else if(contentHeight > options.minHeight) {
  1341. //Comment this line if we want to preserve user height
  1342. divHeight = contentHeight + htmlEditorBarHeight;
  1343. }
  1344. //console.log('newHeight',divHeight);
  1345. options.height = divHeight;
  1346. //obj.animate({ 'height': divHeight }, 250);
  1347. obj.css('height', divHeight);
  1348. //console.log('auto', divWidth);
  1349. options.width = divWidth;
  1350. if(!$( "#pia_editable_" + id ).is(':focus')) {
  1351. if(options.attachedTo.element !== "" && options.attachedTo.fixed)
  1352. t.attachedTo();
  1353. }
  1354. },
  1355. //Get next note Id
  1356. getIndex : function(savable, callback) {
  1357. if(!savable) {
  1358. callback(this.guid());
  1359. return;
  1360. }
  1361. var len = 0;
  1362. var content = "";
  1363. var paso = false;
  1364. storageManager.getlength(function(len) {
  1365. //console.log('getIndex.len', len);
  1366. var loadedItems = $('.PIApostit').length;
  1367. var items = len + loadedItems + 1;
  1368. //console.log('getIndex.items', items);
  1369. for(var i = 1; i <= items; i++) {
  1370. (function(i) {
  1371. storageManager.get(i, function(content) {
  1372. //console.log('getIndex.get', paso, i, content);
  1373. if(!paso && (content == null || content == "") && $( "#idPostIt_" + i ).length <= 0) {
  1374. //console.log('nou index', i);
  1375. paso = true;
  1376. }
  1377. if(callback != null && (paso || i >= items)) {
  1378. callback(i);
  1379. callback = null;
  1380. }
  1381. });
  1382. })(i);
  1383. }
  1384. });
  1385. },
  1386. // Set options
  1387. setOptions : function(opt, save) {
  1388. var t = this;
  1389. if (typeof opt !== 'object') {
  1390. opt = {};
  1391. }
  1392. if (save === undefined) {
  1393. save = false;
  1394. }
  1395. t.options = $.extend(t.options, opt);
  1396. /*jslint unparam: true*/
  1397. $.each(['onChange', 'onSelect', 'onRelease', 'onDblClick'], function (i, e) {
  1398. if (typeof t.options[e] !== 'function') {
  1399. t.options[e] = function () { return undefined; };
  1400. }
  1401. });
  1402. /*jslint unparam: false*/
  1403. if (save) {
  1404. //console.log('setOptions save', t.options);
  1405. this.saveOptions(t.options);
  1406. }
  1407. },
  1408. // Get user selection text on page
  1409. getSelectedText : function() {
  1410. var text = "";
  1411. if (window.getSelection) {
  1412. text = window.getSelection();
  1413. } else if (document.selection) {
  1414. text = document.selection.createRange().text;
  1415. }
  1416. return text;
  1417. },
  1418. // Get user selection html on page
  1419. getSelectedHtml : function() {
  1420. var html = "";
  1421. if (typeof window.getSelection != "undefined") {
  1422. var sel = window.getSelection();
  1423. if (sel.rangeCount) {
  1424. var container = document.createElement("div");
  1425. for (var i = 0, len = sel.rangeCount; i < len; ++i) {
  1426. container.appendChild(sel.getRangeAt(i).cloneContents());
  1427. }
  1428. html = container.innerHTML;
  1429. }
  1430. } else if (typeof document.selection != "undefined") {
  1431. if (document.selection.type == "Text") {
  1432. html = document.selection.createRange().htmlText;
  1433. }
  1434. }
  1435. return html;
  1436. },
  1437. //Recover client OS name
  1438. getOSName : function() {
  1439. var OSName="Unknown OS";
  1440. var browserNav = this.options.osname;
  1441. if (browserNav.indexOf("Win")!=-1) OSName="Windows";
  1442. if (browserNav.indexOf("Mac")!=-1) OSName="MacOS";
  1443. if (browserNav.indexOf("X11")!=-1) OSName="UNIX";
  1444. if (browserNav.indexOf("Linux")!=-1) OSName="Linux";
  1445. return OSName;
  1446. },
  1447. //flip cover to back
  1448. switchBackNoteOn : function(flipClass) {
  1449. var id = this.options.id;
  1450. $('#the_lights').data('highlightedId', id);
  1451. this.enableKeyboardNav();
  1452. $('#idPostIt_' + id + ' > .PIAback').css('visibility', 'visible');
  1453. $($.fn.postitall.globals.prefix + id).addClass('PIAflip ' + flipClass, function () {
  1454. $($.fn.postitall.globals.prefix + id + ' > .PIAfront').css('visibility', 'hidden');
  1455. $($.fn.postitall.globals.prefix + id + ' > .ui-resizable-handle').css('visibility', 'hidden');
  1456. });
  1457. if($.fn.postitall.globals.resizable && $.ui) $($.fn.postitall.globals.prefix + id).resizable("disable");
  1458. if($.fn.postitall.globals.draggable && $.ui) {
  1459. setTimeout(function() {
  1460. $($.fn.postitall.globals.prefix + id).draggable({disabled: true});
  1461. }, 500);
  1462. }
  1463. //$(this).parent().parent().parent().parent().addClass('PIAflip');
  1464. },
  1465. //flip back to cover
  1466. switchBackNoteOff : function(flipClass) {
  1467. var id = this.options.id;
  1468. $('#the_lights_close').click();
  1469. this.disableKeyboardNav();
  1470. $('#idPostIt_' + id + ' > .PIAfront').css('visibility', 'visible');
  1471. $($.fn.postitall.globals.prefix + id).removeClass('PIAflip ' + flipClass, function () {
  1472. $($.fn.postitall.globals.prefix + id + ' > .PIAback').css('visibility', 'hidden');
  1473. $($.fn.postitall.globals.prefix + id + ' > .ui-resizable-handle').css('visibility', '');
  1474. });
  1475. if($.fn.postitall.globals.resizable && $.ui) $($.fn.postitall.globals.prefix + id).resizable("enable");
  1476. if($.fn.postitall.globals.draggable && $.ui) {
  1477. setTimeout(function() {
  1478. //Enable draggable
  1479. $($.fn.postitall.globals.prefix + id).draggable({disabled: false});
  1480. //Show back-date
  1481. $('#idDateBackToolbar_' + id).show();
  1482. }, 500);
  1483. }
  1484. },
  1485. //add transparency to note
  1486. switchTrasparentNoteOn : function() {
  1487. var id = this.options.id;
  1488. var options = this.options;
  1489. var components = this.getRGBComponents(options.style.backgroundcolor);
  1490. //$($.fn.postitall.globals.prefix + id).css('background', 'rgb('+components.R+', '+components.G+', '+components.B+')');
  1491. $($.fn.postitall.globals.prefix + id).css('background-color', 'rgba('+components.R+', '+components.G+', '+components.B+', 0.8)');
  1492. },
  1493. //remove transparency to note
  1494. switchTrasparentNoteOff : function() {
  1495. var id = this.options.id;
  1496. var options = this.options;
  1497. var components = this.getRGBComponents(options.style.backgroundcolor);
  1498. //$($.fn.postitall.globals.prefix + id).css('background', 'rgb('+components.R+', '+components.G+', '+components.B+')');
  1499. $($.fn.postitall.globals.prefix + id).css('background-color', 'rgba('+components.R+', '+components.G+', '+components.B+', 1)');
  1500. },
  1501. //Switch off lights and highlight note
  1502. switchOffLights : function() {
  1503. var t = this;
  1504. var id = t.options.id;
  1505. if(id !== undefined) {
  1506. $($.fn.postitall.globals.prefix + id).css({
  1507. 'z-index': 999999,
  1508. 'border': '1px solid rgb(236, 236, 0)',
  1509. 'box-shadow': 'rgb(192, 195, 155) 1px 1px 10px 3px',
  1510. });
  1511. }
  1512. setTimeout(function() {
  1513. $("#the_lights").fadeTo("fast", 0.6, function() {
  1514. $("#the_lights").css('display','block');
  1515. $("#the_lights").css({'height':($(document).height())+'px'});
  1516. $("#the_lights").css({'width':($(document).width())+'px'});
  1517. $("#the_lights").data('highlightedId', id);
  1518. // lock scroll position, but retain settings for later
  1519. var scrollPosition = [
  1520. self.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft,
  1521. self.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop
  1522. ];
  1523. $('html').data('scroll-position', scrollPosition);
  1524. $('html').data('previous-overflow', $('html').css('overflow'));
  1525. $('html').css('overflow', 'hidden');
  1526. $(window).on('resize', $.proxy(t.resizeAction, t));
  1527. window.scrollTo(scrollPosition[0], scrollPosition[1]);
  1528. });
  1529. }, 500);
  1530. },
  1531. //Switch lights on & remove highlighted note
  1532. switchOnLights : function() {
  1533. var id = $("#the_lights").data('highlightedId');
  1534. var options = $($.fn.postitall.globals.prefix + id).data('PIA-options');
  1535. var t = this;
  1536. if(id !== "" && options !== null && options !== undefined) {
  1537. $("#the_lights").data('highlightedId', '');
  1538. $($.fn.postitall.globals.prefix + id).css({
  1539. 'z-index': 999995,
  1540. 'border': '1px solid ' + $($.fn.postitall.globals.prefix + id).css('background-color'),
  1541. 'box-shadow': '',
  1542. });
  1543. if(options.flags.expand) {
  1544. $('#pia_expand_'+id).click();
  1545. } else {
  1546. $('#pia_close_'+id).click();
  1547. }
  1548. }
  1549. if($("#the_lights").css('display') != "none") {
  1550. $("#the_lights").css('display','block');
  1551. $("#the_lights").fadeTo("slow",0, function() {
  1552. $("#the_lights").css('display','none');
  1553. });
  1554. // un-lock scroll position
  1555. var scrollPosition = $('html').data('scroll-position');
  1556. if(scrollPosition != undefined) {
  1557. //console.log('unlock', scrollPosition);
  1558. var tmpOvf = $('html').data('previous-overflow');
  1559. $('html').css('overflow', (tmpOvf != "hidden") ? tmpOvf : "visible" );
  1560. window.scrollTo(scrollPosition[0], scrollPosition[1]);
  1561. }
  1562. $(window).off('resize');
  1563. //Set on resize action
  1564. if($.fn.postitall.globals.autoPosition) // && options.features.autoPosition)
  1565. $(window).on('resize', $.proxy(t.relativePosition, t));
  1566. }
  1567. },
  1568. //On resize when lights are off
  1569. switchOffLightsResize : function() {
  1570. $("#the_lights").css({'height':($(document).height())+'px'});
  1571. },
  1572. //Enable keyboard actions
  1573. enableKeyboardNav : function(callback) {
  1574. //console.log('enableKeyboardNav');
  1575. this.callback = callback;
  1576. $(document).on('keyup.keyboard', $.proxy(this.keyboardAction, this));
  1577. },
  1578. //Disable keybord actions
  1579. disableKeyboardNav : function() {
  1580. //console.log('disableKeyboardNav');
  1581. $(document).off('keyup.keyboard');
  1582. },
  1583. //Keyboard actions
  1584. keyboardAction : function(event) {
  1585. //console.log('keyboardAction', event);
  1586. var KEYCODE_ESC = 27;
  1587. var keycode = event.keyCode;
  1588. var key = String.fromCharCode(keycode).toLowerCase();
  1589. //On keypress ESC
  1590. if (keycode === KEYCODE_ESC) { // || key.match(/x|o|c/)) {
  1591. //console.log('key press ESC');
  1592. if(this.callback != undefined) this.callback();
  1593. this.switchOnLights();
  1594. this.disableKeyboardNav();
  1595. }
  1596. },
  1597. //Resize expanded note when screen size changes
  1598. resizeAction : function(event) {
  1599. var t = this;
  1600. delay(function(){
  1601. //console.log('resizeAction', t.options.id);
  1602. //Lights switched off
  1603. var highlightedId = $("#the_lights").data('highlightedId');
  1604. if(highlightedId !== undefined && highlightedId !== ""){
  1605. t.switchOffLightsResize();
  1606. var options = $($.fn.postitall.globals.prefix + highlightedId).data('PIA-options');
  1607. if(options.flags.expand) {
  1608. $($.fn.postitall.globals.prefix + highlightedId).css({
  1609. top:'10px'
  1610. }).animate({
  1611. 'height': $(window).height() - 30,
  1612. 'width': $(window).width() - 30
  1613. });
  1614. }
  1615. }
  1616. }, 500);
  1617. },
  1618. //On screen resize, notes will preserve relative position to new width screen
  1619. relativePosition : function(event) {
  1620. var t = this;
  1621. if(!$.fn.postitall.globals.autoPosition || !t.options.features.autoPosition) {
  1622. $(window).off('resize');
  1623. return;
  1624. }
  1625. var obj, options;
  1626. delay(function(){
  1627. $('.PIApostit').each(function(i,e) {
  1628. obj = $($.fn.postitall.globals.prefix + $(e).data('PIA-id'));
  1629. options = $($.fn.postitall.globals.prefix + $(e).data('PIA-id')).data('PIA-options');
  1630. //console.log(i,options,obj);
  1631. if(options !== undefined && options.attachedTo.element !== undefined && options.attachedTo.element !== "") {
  1632. //Attached elements
  1633. if(options.attachedTo.fixed !== undefined && options.attachedTo.fixed)
  1634. t.attachedTo(options);
  1635. } else {
  1636. //Floating elements
  1637. //console.log('new pos for note ', i, e);
  1638. var noteLoc = obj.offset();
  1639. var screenLoc = { 'height': $(window).height(), 'width': $(window).width() };
  1640. var top = noteLoc.top;
  1641. var left = noteLoc.left;
  1642. var width = parseInt($(e).css('width'), 10) + parseInt($(e).css('padding-left'), 10) + parseInt($(e).css('padding-right'), 10);
  1643. var height = parseInt($(e).css('height'), 10) + parseInt($(e).css('padding-top'), 10) + parseInt($(e).css('padding-bottom'), 10);
  1644. var x1 = left, x2 = (left + width), y1 = top, y2 = (top + height);
  1645. var relTop = (y1 / screenLoc.height) * 100;
  1646. var relLeft = (x1 / screenLoc.width) * 100;
  1647. var relWidth = ((x2 - x1) / screenLoc.width) * 100;
  1648. var relHeight = ((y2 - y1) / screenLoc.height) * 100;
  1649. $(e).css({
  1650. //'top': relTop + "%",
  1651. 'left': relLeft + "%",
  1652. //'width': relWidth + "%",
  1653. //'height': relHeight + "%"
  1654. });
  1655. options.posX = obj.offset().left;
  1656. }
  1657. //Save
  1658. obj.data('PIA-options', options);
  1659. //console.log('options', options);
  1660. });
  1661. //$.PostItAll.save();
  1662. }, 100);
  1663. },
  1664. //Save current note position in options.oldPosition
  1665. saveOldPosition : function() {
  1666. var obj = $($.fn.postitall.globals.prefix + this.options.id);
  1667. var leftMinimized = obj.css('left');
  1668. if(this.options.oldPosition !== undefined && this.options.oldPosition.leftMinimized !== undefined)
  1669. leftMinimized = this.options.oldPosition.leftMinimized;
  1670. var propCss = {
  1671. 'position': obj.css('position'),
  1672. 'left': obj.css('left'),
  1673. 'top': obj.css('top'),
  1674. 'height': obj.css('height'),
  1675. 'width': obj.css('width'),
  1676. 'leftMinimized': leftMinimized,
  1677. };
  1678. this.options.oldPosition = propCss;
  1679. //console.log('saveOldPosition', this.options.oldPosition);
  1680. this.setOptions(this.options, true);
  1681. },
  1682. //Scroll to note position
  1683. scrollToNotePosition : function(tmpTop, tmpHeight, tmpLeft, tmpWidth, callback) {
  1684. var scrollTop1 = self.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;
  1685. var scrollBottom1 = scrollTop1 + $(window).height();
  1686. var scrollTop2 = parseInt(tmpTop, 10);
  1687. var scrollBottom2 = scrollTop2 + parseInt(tmpHeight,10);
  1688. var scrollLeft1 = self.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft;
  1689. var scrollRight1 = scrollLeft1 + $(window).width();
  1690. var scrollLeft2 = parseInt(tmpLeft, 10);
  1691. var scrollRight2 = scrollLeft2 + parseInt(tmpWidth,10);
  1692. //console.log(scrollTop2,scrollTop1,scrollBottom2,scrollBottom1);
  1693. if(scrollTop2 > scrollTop1 && scrollBottom2 < scrollBottom1 && scrollLeft2 > scrollLeft1 && scrollRight2 < scrollRight1) {
  1694. if(callback !== undefined) callback();
  1695. } else {
  1696. $('html, body').animate({
  1697. scrollTop: scrollTop2 - parseInt(tmpHeight,10),
  1698. scrollLeft: scrollLeft2 - parseInt(tmpWidth,10)
  1699. }, 400);
  1700. setTimeout(function() {
  1701. if(callback !== undefined) callback();
  1702. }, 500);
  1703. }
  1704. },
  1705. //Restore note with options.oldPosition
  1706. restoreOldPosition : function(scrollToNote, callback) {
  1707. if(scrollToNote === undefined)
  1708. scrollToNote = false;
  1709. var t = this;
  1710. var options = t.options;
  1711. var id = options.id;
  1712. var showNote = function() {
  1713. if(options.oldPosition.position == "absolute") {
  1714. $($.fn.postitall.globals.prefix + id).css({
  1715. 'top': parseInt($($.fn.postitall.globals.prefix + id).css('top'), 10) + $(document).scrollTop()
  1716. });
  1717. } else {
  1718. $($.fn.postitall.globals.prefix + id).css({
  1719. 'top': $($.fn.postitall.globals.prefix + id).css('top'),
  1720. });
  1721. }
  1722. $($.fn.postitall.globals.prefix + id).css({
  1723. 'position': 'absolute',
  1724. 'bottom': 'auto',
  1725. });
  1726. setTimeout(function(){
  1727. $($.fn.postitall.globals.prefix + id).animate({
  1728. 'top': options.oldPosition.top,
  1729. 'left': options.oldPosition.left,
  1730. 'width': options.oldPosition.width,
  1731. 'height': options.oldPosition.height,
  1732. }, 500, function() {
  1733. t.showArrow(id, options);
  1734. $(this).find( ".PIAeditable" ).css('height', 'auto');
  1735. t.autoresize();
  1736. //animate resize
  1737. if(options.flags.blocked) {
  1738. options.flags.blocked = false;
  1739. t.blockNote();
  1740. } else {
  1741. t.hoverOptions(id, true);
  1742. }
  1743. t.switchTrasparentNoteOff();
  1744. t.switchOnLights();
  1745. if(callback !== undefined) callback();
  1746. }).css({
  1747. 'position': options.oldPosition.position,
  1748. });
  1749. }, 100);
  1750. };
  1751. if(scrollToNote && options.position != "fixed") {
  1752. t.scrollToNotePosition(options.oldPosition.top, options.height, options.oldPosition.left, options.width, showNote);
  1753. } else {
  1754. showNote();
  1755. }
  1756. },
  1757. //hide/show divList objects
  1758. toogleToolbar : function(action, divlist) {
  1759. var t = this;
  1760. var options = t.options;
  1761. var index = options.id;
  1762. var type = "";
  1763. var fadeOuTime = 200;
  1764. for(var i = 0; i < divlist.length; i++) {
  1765. type = divlist[i].substring(0,1);
  1766. if(type != "#" && type != ".") {
  1767. type = "#";
  1768. } else {
  1769. divlist[i] = divlist[i].substring(1);
  1770. }
  1771. if(action == "hide") {
  1772. $(type + divlist[i] + index).fadeTo(0, 0, function() {
  1773. $(this).hide();
  1774. });
  1775. } else {
  1776. $(type + divlist[i] + index).fadeTo(0, 1, function() {
  1777. $(this).show();
  1778. });
  1779. }
  1780. }
  1781. if(action == "hide") {
  1782. if ($.fn.postitall.globals.resizable && options.features.resizable) {
  1783. if ($.ui) $($.fn.postitall.globals.prefix + index).resizable("disable");
  1784. }
  1785. if ($.fn.postitall.globals.draggable && options.features.draggable) {
  1786. //draggable
  1787. if ($.ui) $($.fn.postitall.globals.prefix + index).draggable("disable");
  1788. $('#pia_toolbar_'+index).css('cursor', 'inherit');
  1789. }
  1790. } else {
  1791. if ($.fn.postitall.globals.resizable && options.features.resizable) {
  1792. if ($.ui) $($.fn.postitall.globals.prefix + index).resizable("enable");
  1793. }
  1794. if ($.fn.postitall.globals.draggable && options.features.draggable) {
  1795. if ($.ui) $($.fn.postitall.globals.prefix + index).draggable("enable");
  1796. $('#pia_toolbar_'+index).css('cursor', 'move');
  1797. }
  1798. t.hoverState = true;
  1799. //t.hoverOptions(index, true);
  1800. }
  1801. },
  1802. //Expand note
  1803. expandNote : function() {
  1804. var t = this;
  1805. var index = t.options.id;
  1806. var options = t.options;
  1807. $('#the_lights_close').hide();
  1808. $('#pia_expand_' + index).removeClass('icofont icofont-plus').addClass('icofont icofont-minus');
  1809. t.hoverOptions(index, false);
  1810. t.saveOldPosition();
  1811. t.toogleToolbar('hide', ['idPIAIconBottom_', 'idInfo_', 'pia_config_', 'pia_fixed_', 'pia_delete_', 'pia_blocked_', 'pia_minimize_', 'pia_new_', 'pia_hidden_']);
  1812. t.hideArrow();
  1813. t.switchTrasparentNoteOn();
  1814. t.switchOffLights();
  1815. // Expand note
  1816. $($.fn.postitall.globals.prefix + index).css({
  1817. 'top':'10px',
  1818. 'position': 'fixed',
  1819. }).animate({
  1820. 'height': $(window).height() - 30,
  1821. 'width': $(window).width() - 30,
  1822. 'top': '10px',
  1823. 'left': '10px',
  1824. }, 500, function() {
  1825. $('.PIApostit').css('z-index', 999995);
  1826. $(this).css('z-index', '999999');
  1827. $( "#pia_editable_" + index ).css('height',($(window).height() - 120));
  1828. $( "#pia_editable_" + index ).focus();
  1829. });
  1830. options.flags.expand = true;
  1831. t.enableKeyboardNav();
  1832. t.saveOptions(options);
  1833. },
  1834. //Collapse note (restoreOldPosition)
  1835. collapseNote : function() {
  1836. var t = this;
  1837. var index = t.options.id;
  1838. var options = t.options;
  1839. $('#the_lights_close').show();
  1840. $('#pia_expand_' + index).removeClass('icofont icofont-minus').addClass('icofont icofont-plus');
  1841. $($.fn.postitall.globals.prefix + index).css('position', options.position);
  1842. // show toolbar
  1843. t.toogleToolbar('show', ['idPIAIconBottom_', 'idInfo_', 'pia_config_', 'pia_fixed_', 'pia_delete_', 'pia_blocked_', 'pia_minimize_', 'pia_new_', 'pia_hidden_']);
  1844. //restore oldposition
  1845. t.restoreOldPosition();
  1846. //newstate
  1847. options.flags.expand = false;
  1848. t.saveOptions(options);
  1849. },
  1850. //Minimize/Maximize note
  1851. minimizeNote : function() {
  1852. var t = this;
  1853. var index = t.options.id;
  1854. var options = t.options;
  1855. var obj = $($.fn.postitall.globals.prefix + index);
  1856. if(!$.fn.postitall.globals.minimized || !options.features.minimized)
  1857. return;
  1858. //minimize action
  1859. var minimize = function() {
  1860. t.hoverOptions(index, false);
  1861. $('#pia_editable_'+index).hide();
  1862. $('#pia_minimize_'+index).removeClass('icofont icofont-minus').addClass('icofont icofont-minus');
  1863. options.flags.minimized = true;
  1864. //Add some start text to minimized note
  1865. var txtContent = " " + $('#pia_editable_'+index).text();
  1866. if(txtContent.length > 18)
  1867. txtContent = txtContent.substring(0,15) + "...";
  1868. var smallText = $('<div id="pia_minimized_text_'+index+'" class="PIAminimizedText" />').text(txtContent);
  1869. $('#pia_toolbar_'+index).append(smallText);
  1870. //hide toolbar
  1871. t.toogleToolbar('hide', ['idPIAIconBottom_', 'idInfo_', 'pia_config_', 'pia_fixed_', 'pia_delete_', 'pia_blocked_', 'pia_expand_', 'pia_new_', 'pia_hidden_']);
  1872. //Enable draggable x axis
  1873. if ($.fn.postitall.globals.draggable && options.features.draggable) {
  1874. //draggable
  1875. if ($.ui) {
  1876. obj.draggable("enable");
  1877. obj.draggable({ axis: "x" });
  1878. }
  1879. }
  1880. t.saveOldPosition();
  1881. //Minimize
  1882. var hideNote = function() {
  1883. $($.fn.postitall.globals.prefix + index).animate({
  1884. 'width': (options.minWidth + 20),
  1885. 'height': '20px',
  1886. 'bottom': '0',
  1887. 'left': options.oldPosition.leftMinimized,
  1888. }, 500, function() {
  1889. t.hideArrow();
  1890. t.switchTrasparentNoteOn();
  1891. $($.fn.postitall.globals.prefix + index).css({position:'fixed'})
  1892. }).css({
  1893. 'top': 'auto',
  1894. });
  1895. };
  1896. $($.fn.postitall.globals.prefix + index).css({
  1897. 'position': 'fixed',
  1898. });
  1899. if(options.position == "absolute") {
  1900. $($.fn.postitall.globals.prefix + index).css({
  1901. 'top': parseInt(options.posY, 10) - $(document).scrollTop(),
  1902. });
  1903. }
  1904. setTimeout(function(){
  1905. hideNote();
  1906. }, 100);
  1907. };
  1908. //maximize action (restoreOldPosition)
  1909. var maximize = function() {
  1910. t.restoreOldPosition(true, function() {
  1911. $('#pia_editable_'+index).show();
  1912. $('#pia_minimize_'+index).removeClass('icofont icofont-minus').addClass('icofont icofont-minus');
  1913. options.flags.minimized = false;
  1914. // show toolbar
  1915. t.toogleToolbar('show', ['idPIAIconBottom_', 'idInfo_', 'pia_config_', 'pia_fixed_', 'pia_delete_', 'pia_blocked_', 'pia_expand_', 'pia_new_', 'pia_hidden_']);
  1916. $('#pia_minimized_text_'+index).remove();
  1917. //Remove draggable axis
  1918. if ($.fn.postitall.globals.draggable && options.features.draggable) {
  1919. if ($.ui) obj.draggable({ axis: "none" });
  1920. }
  1921. t.switchTrasparentNoteOff();
  1922. });
  1923. };
  1924. //Action
  1925. if(!options.flags.minimized) {
  1926. minimize();
  1927. } else {
  1928. maximize();
  1929. }
  1930. //Save feature
  1931. t.save(obj);
  1932. },
  1933. //Block note
  1934. blockNote : function() {
  1935. var t = this;
  1936. var index = t.options.id;
  1937. var options = t.options;
  1938. var obj = $($.fn.postitall.globals.prefix + index);
  1939. if(!$.fn.postitall.globals.blocked || !options.features.blocked)
  1940. return;
  1941. var ides = ['pia_config_', 'pia_fixed_', 'pia_delete_', 'idPIAIconBottom_', '.selectedArrow_'];
  1942. if(!options.flags.expand)
  1943. ides.push('pia_expand_');
  1944. if(!options.flags.minimized)
  1945. ides.push('pia_minimize_');
  1946. if(options.flags.blocked) {
  1947. $('#pia_blocked_'+index.toString()).removeClass('PIAblocked2').addClass('PIAblocked');
  1948. $('#pia_editable_'+index.toString()).attr('contenteditable', true).css("cursor", "");
  1949. //toolbar
  1950. t.toogleToolbar('show', ides);
  1951. //onhover actions
  1952. t.hoverOptions(index, true);
  1953. //new state
  1954. options.flags.blocked = false;
  1955. } else {
  1956. $('#pia_blocked_'+index.toString()).removeClass('PIAblocked').addClass('PIAblocked2');
  1957. $('#pia_editable_'+index.toString()).attr('contenteditable', false).css("cursor", "auto");
  1958. //disabel onhover actios
  1959. t.hoverOptions(index, false);
  1960. //toolbar
  1961. t.toogleToolbar('hide', ides);
  1962. //new state
  1963. options.flags.blocked = true;
  1964. }
  1965. //Save feature
  1966. t.save(obj);
  1967. },
  1968. //Fix note position
  1969. fixNote : function() {
  1970. var t = this;
  1971. var index = t.options.id;
  1972. var options = t.options;
  1973. var obj = $($.fn.postitall.globals.prefix + index);
  1974. if(options.flags.fixed) {
  1975. $('#pia_fixed_'+index).removeClass('PIAfixed2 PIAiconFixed').addClass('icofont icofont-tack-pin PIAicon');
  1976. options.position = "absolute";
  1977. options.posY = obj.offset().top;
  1978. obj.removeClass("fixed");
  1979. options.attachedTo.element = "";
  1980. options.flags.fixed = false;
  1981. } else {
  1982. $('#pia_fixed_'+index).removeClass('icofont icofont-tack-pin PIAicon').addClass('PIAfixed2 PIAiconFixed');
  1983. options.position = "fixed";
  1984. options.posY = parseInt(options.posY, 10) - $(document).scrollTop();
  1985. obj.addClass("fixed");
  1986. obj.css('z-index', 999996);
  1987. options.flags.fixed = true;
  1988. }
  1989. obj.css('position', options.position);
  1990. //obj.css('left', parseInt(options.posX, 10) + "px");
  1991. obj.css('top', parseInt(options.posY, 10) + "px");
  1992. //Save features
  1993. t.setOptions(options);
  1994. t.save(obj);
  1995. },
  1996. //Enable/Disable auto-hide toolbar icons when hover over the note
  1997. hoverOptions : function(index, enabled) {
  1998. //Hide toolbar
  1999. if(!$.fn.postitall.globals.toolbar || !this.options.features.toolbar)
  2000. return;
  2001. if(!$.fn.postitall.globals.autoHideToolBar || !this.options.features.autoHideToolBar)
  2002. enabled = false;
  2003. var fadeInTime = 200;
  2004. var fadeOuTime = 600;
  2005. var t = this;
  2006. if(enabled) {
  2007. setTimeout(function(){
  2008. //Options
  2009. $( $.fn.postitall.globals.prefix + index ).hover(function() {
  2010. $($.fn.postitall.globals.prefix + index).find('.PIAfront').find(".PIAicon, .PIAiconFixed").fadeTo(fadeInTime, 1);
  2011. if(t.options.style.arrow === undefined || t.options.style.arrow == "none")
  2012. $( $.fn.postitall.globals.prefix + index).find(".icon_box").fadeTo(fadeOuTime, 1);
  2013. $($.fn.postitall.globals.prefix + index + ' > .ui-resizable-handle').fadeTo(fadeInTime, 1);
  2014. t.hoverState = true;
  2015. }, function() {
  2016. setTimeout(function() {
  2017. if(!t.hoverState) {
  2018. $($.fn.postitall.globals.prefix + index).find('.PIAfront').find(".PIAicon, .PIAiconFixed").fadeTo(fadeOuTime, 0);
  2019. $($.fn.postitall.globals.prefix + index).find(".icon_box").fadeTo(fadeOuTime, 0);
  2020. $($.fn.postitall.globals.prefix + index + ' > .ui-resizable-handle').fadeTo(fadeOuTime, 0);
  2021. }
  2022. },(fadeOuTime - fadeInTime));
  2023. t.hoverState = false;
  2024. });
  2025. if(!t.hoverState) {
  2026. $($.fn.postitall.globals.prefix + index).find('.PIAfront').find(".PIAicon, .PIAiconFixed").fadeTo(fadeOuTime, 0);
  2027. $($.fn.postitall.globals.prefix + index).find(".icon_box").fadeTo(fadeOuTime, 0);
  2028. $($.fn.postitall.globals.prefix + index + ' > .ui-resizable-handle').fadeTo(fadeOuTime, 0);
  2029. }
  2030. },100);
  2031. return;
  2032. }
  2033. t.hoverState = false;
  2034. //Options
  2035. $( $.fn.postitall.globals.prefix + index ).unbind('mouseenter mouseleave');
  2036. //console.log('t.options.style.arrow',t.options.style.arrow);
  2037. if(t.options.style.arrow === undefined || t.options.style.arrow == "none")
  2038. $( $.fn.postitall.globals.prefix + index).find(".icon_box").fadeTo(fadeOuTime, 1);
  2039. $( $.fn.postitall.globals.prefix + index).find('.PIAfront').find(".PIAicon, .PIAiconFixed").fadeTo(fadeInTime, 1);
  2040. },
  2041. //Get a random color (if the feature is enabled, otherwhise will return default background color)
  2042. getRandomColor : function() {
  2043. var randomColor = "";
  2044. if($.fn.postitall.globals.randomColor && $.fn.postitall.defaults.features.randomColor) {
  2045. //Random color
  2046. //var colors = ["red", "blue", "yellow", "black", "green"];
  2047. //return colors[Math.floor(Math.random() * colors.length)];
  2048. randomColor = "#"+(Math.random()*0xFFFFFF<<0).toString(16);
  2049. } else {
  2050. //Default postit color
  2051. randomColor = $.fn.postitall.defaults.style.backgroundcolor;
  2052. }
  2053. if(randomColor.length < 7) {
  2054. var num = 7 - randomColor.length;
  2055. var ret = new Array( num + 1 ).join("0");
  2056. randomColor = randomColor + ret.toString();
  2057. }
  2058. return randomColor;
  2059. },
  2060. //Get text color relative tot hexcolor (if the featured is enabled, otherwise will return default text color)
  2061. getTextColor : function(hexcolor) {
  2062. if($.fn.postitall.globals.randomColor && $.fn.postitall.defaults.features.randomColor) {
  2063. //Inverse of background (hexcolor)
  2064. var nThreshold = 105;
  2065. var components = this.getRGBComponents(hexcolor);
  2066. var bgDelta = (components.R * 0.299) + (components.G * 0.587) + (components.B * 0.114);
  2067. return ((255 - bgDelta) < nThreshold) ? "#111111" : "#eeeeee";
  2068. } else {
  2069. //Default postit text color
  2070. return $.fn.postitall.defaults.style.textcolor;
  2071. }
  2072. },
  2073. //Get css text-shadow style
  2074. getTextShadowStyle : function(hexcolor) {
  2075. //console.log(hexcolor);
  2076. var nThreshold = 105;
  2077. var components = this.getRGBComponents(hexcolor);
  2078. var bgDelta = (components.R * 0.299) + (components.G * 0.587) + (components.B * 0.114);
  2079. return ((255 - bgDelta) < nThreshold) ? "tresdblack" : "tresd";
  2080. },
  2081. //Get rgb from an hex color
  2082. getRGBComponents : function(color) {
  2083. var r = color.substring(1, 3);
  2084. var g = color.substring(3, 5);
  2085. var b = color.substring(5, 7);
  2086. return {
  2087. R: parseInt(r, 16),
  2088. G: parseInt(g, 16),
  2089. B: parseInt(b, 16)
  2090. };
  2091. },
  2092. //Retrive unique random id (non savable notes)
  2093. guid : function() {
  2094. function s4() {
  2095. return Math.floor((1 + Math.random()) * 0x10000)
  2096. .toString(16)
  2097. .substring(1);
  2098. }
  2099. return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
  2100. s4() + '-' + s4() + s4() + s4();
  2101. },
  2102. //Check if the element or her parents have a fixed position
  2103. elementOrParentIsFixed : function(element) {
  2104. var $element = $(element);
  2105. var $checkElements = $element.add($element.parents());
  2106. var isFixed = false;
  2107. $checkElements.each(function(){
  2108. if ($(this).css("position") === "fixed") {
  2109. isFixed = true;
  2110. return false;
  2111. }
  2112. });
  2113. return isFixed;
  2114. },
  2115. //Create note from an object
  2116. create : function(obj) {
  2117. var t = this;
  2118. var options = t.options;
  2119. var index = options.id.toString();
  2120. //Add data to object
  2121. obj.data('PIA-id', index)
  2122. .data('PIA-initialized', true)
  2123. .data('PIA-options', options);
  2124. //Postit editable content
  2125. if (options.content === "") {
  2126. if (obj.html() !== "") {
  2127. options.content = obj.html();
  2128. }
  2129. }
  2130. //Front page: toolbar
  2131. var barCursor = "cursor: inherit;";
  2132. if ($.fn.postitall.globals.draggable && options.features.draggable) {
  2133. barCursor = "cursor: move;";
  2134. }
  2135. var toolbar = $('<div />', {
  2136. 'id': 'pia_toolbar_' + index,
  2137. 'class': 'PIAtoolbar',
  2138. 'style': barCursor
  2139. });
  2140. //Drag support without jQuery UI - don't work so much well...
  2141. if (!$.ui) {
  2142. if ($.fn.postitall.globals.draggable && options.features.draggable) {
  2143. toolbar.drags();
  2144. }
  2145. }
  2146. //Delete icon
  2147. if($.fn.postitall.globals.removable) {
  2148. if (options.features.removable) {
  2149. toolbar.append($('<div />', {
  2150. 'id': 'pia_delete_' + index,
  2151. 'class': 'PIAdelete icofont icofont-close PIAicon'
  2152. }).click(function (e) {
  2153. if (obj.hasClass('PIAdragged')) {
  2154. obj.removeClass('PIAdragged');
  2155. } else {
  2156. if($.fn.postitall.globals.askOnDelete && options.features.askOnDelete) {
  2157. if ($(this).parent().find('.ui-widget2').length <= 0) {
  2158. $('.backContent_' + index).hide();
  2159. $('#idBackDelete_' + index + ' > .PIABox').css({
  2160. //'width': options.width - 10,
  2161. 'height': options.height - 40
  2162. });
  2163. $('#idBackDelete_' + index).show();
  2164. t.switchBackNoteOn('PIAflip2');
  2165. t.switchOffLights();
  2166. }
  2167. } else {
  2168. t.destroy();
  2169. }
  2170. }
  2171. e.preventDefault();
  2172. }));
  2173. }
  2174. }
  2175. //Config icon
  2176. if($.fn.postitall.globals.changeoptions) {
  2177. if (options.features.changeoptions) {
  2178. toolbar.append(
  2179. $('<div />', {
  2180. 'id': 'pia_config_' + index,
  2181. 'class': 'PIAconfig PIAicon'
  2182. }).click(function (e) {
  2183. if (obj.hasClass('PIAdragged')) {
  2184. obj.removeClass('PIAdragged');
  2185. } else {
  2186. $('.backContent_'+index).hide();
  2187. $('#idBackConfig_'+index+' > .PIABox').css({
  2188. //'width': options.width - 10,
  2189. 'height': options.height - 40
  2190. });
  2191. $('#idBackConfig_'+index).show();
  2192. t.switchBackNoteOn('PIAflip2');
  2193. }
  2194. e.preventDefault();
  2195. })
  2196. );
  2197. }
  2198. }
  2199. //Fixed
  2200. if($.fn.postitall.globals.fixed) {
  2201. if(options.features.fixed) {
  2202. toolbar.append(
  2203. $('<div />', {
  2204. 'id': 'pia_fixed_' + index,
  2205. 'class': 'icofont icofont-tack-pin' + (options.flags.fixed ? '2 PIAiconFixed' : ' PIAicon') + ' '
  2206. }).click(function (e) {
  2207. if (obj.hasClass('PIAdragged')) {
  2208. obj.removeClass('PIAdragged');
  2209. } else {
  2210. t.fixNote();
  2211. }
  2212. e.preventDefault();
  2213. })
  2214. );
  2215. }
  2216. }
  2217. //Hide
  2218. if($.fn.postitall.globals.hidden && options.features.hidden) {
  2219. toolbar.append(
  2220. $('<div />', {
  2221. 'id': 'pia_hidden_' + index,
  2222. 'class': 'PIAhide PIAicon'
  2223. }).click(function(e) {
  2224. if(obj.hasClass('PIAdragged')) {
  2225. obj.removeClass('PIAdragged');
  2226. } else {
  2227. if($.fn.postitall.globals.askOnHide && options.features.askOnHide) {
  2228. $('.backContent_'+index).hide();
  2229. $('#idHideUntil_'+index+' > .PIABox').css({
  2230. //'width': options.width - 10,
  2231. 'height': options.height - 40
  2232. });
  2233. $('#idHideUntil_'+index).show();
  2234. t.switchBackNoteOn('PIAflip2');
  2235. } else {
  2236. t.hide(index);
  2237. }
  2238. }
  2239. e.preventDefault();
  2240. })
  2241. );
  2242. } else {
  2243. options.flags.hidden = false;
  2244. }
  2245. //MINIMIZE
  2246. if($.fn.postitall.globals.minimized && options.features.minimized) {
  2247. toolbar.append(
  2248. $('<div />', {
  2249. 'id': 'pia_minimize_' + index,
  2250. 'class': (options.flags.minimized ? 'icofont icofont-minus' : 'icofont icofont-minus') + ' PIAicon'
  2251. }).click(function (e) {
  2252. if (obj.hasClass('PIAdragged')) {
  2253. obj.removeClass('PIAdragged');
  2254. } else {
  2255. t.minimizeNote();
  2256. }
  2257. e.preventDefault();
  2258. })
  2259. );
  2260. } else {
  2261. options.flags.minimized = false;
  2262. }
  2263. //Expand note / Maximize
  2264. if($.fn.postitall.globals.expand && options.features.expand) {
  2265. toolbar.append(
  2266. $('<div />', {
  2267. 'id': 'pia_expand_' + index,
  2268. 'class': (options.flags.expand ? 'PIAmaximize' : 'icofont icofont-plus') + ' PIAicon'
  2269. }).click(function (e) {
  2270. if (obj.hasClass('PIAdragged')) {
  2271. obj.removeClass('PIAdragged');
  2272. } else {
  2273. if(!options.flags.expand) {
  2274. t.expandNote();
  2275. } else {
  2276. t.collapseNote();
  2277. }
  2278. }
  2279. e.preventDefault();
  2280. })
  2281. );
  2282. } else {
  2283. options.flags.expand = false;
  2284. }
  2285. //Blocked
  2286. if($.fn.postitall.globals.blocked && options.features.blocked) {
  2287. toolbar.append(
  2288. $('<div />', {
  2289. 'id': 'pia_blocked_' + index,
  2290. 'class': 'PIAblocked' + (options.flags.blocked == true ? '2' : '') + ' PIAicon',
  2291. }).click(function (e) {
  2292. if (obj.hasClass('PIAdragged')) {
  2293. obj.removeClass('PIAdragged');
  2294. } else {
  2295. t.blockNote();
  2296. }
  2297. e.preventDefault();
  2298. })
  2299. );
  2300. }
  2301. //Front page: content
  2302. var content = $('<div />', {
  2303. 'id': 'pia_editable_' + index,
  2304. 'class': 'PIAeditable PIAcontent',
  2305. //Reset herisated contenteditable styles
  2306. //color:'+options.style.textcolor+';min-width:99%;
  2307. 'style': 'width: auto;height: auto;padding: 10px;border-color: transparent;min-width:' + (options.minWidth) + 'px;box-shadow:none;min-height:' + (options.minHeight - 100) + 'px;'
  2308. }).change(function (e) {
  2309. if(!$.fn.postitall.globals.editable || !options.features.editable) {
  2310. return;
  2311. }
  2312. var oldContent = options.content;
  2313. //Html format
  2314. var text = $(this).text();
  2315. if ($.fn.postitall.globals.pasteHtml || options.features.pasteHtml) {
  2316. text = $(this).html();
  2317. //Default sanitize
  2318. text = text.replace(/<script[^>]*?>.*?<\/script>/gi, '').
  2319. //replace(/<[\/\!]*?[^<>]*?>/gi, '').
  2320. replace(/<style[^>]*?>.*?<\/style>/gi, '').
  2321. replace(/<![\s\S]*?--[ \t\n\r]*>/gi, '');
  2322. //htmlClean sanitize plugin
  2323. if($.htmlClean !== undefined) {
  2324. //htmlClean plugin
  2325. text = $.htmlClean(text, { format: true });
  2326. }
  2327. }
  2328. t.options.content = text;
  2329. t.autoresize();
  2330. t.save(obj, function(error) {
  2331. if(error !== undefined && error !== "") {
  2332. alert('Error saving content! \n\n'+error+'\n\nReverting to last known content.');
  2333. t.options.content = oldContent;
  2334. $('#pia_editable_' + t.options.id).html(oldContent);
  2335. $('#pia_editable_' + t.options.id).trigger('change');
  2336. t.autoresize();
  2337. }
  2338. });
  2339. }).html(t.options.content);
  2340. if($.fn.postitall.globals.editable && options.features.editable) {
  2341. content.attr('contenteditable', true);
  2342. } else {
  2343. content.attr('contenteditable', false).css("cursor", "auto");
  2344. }
  2345. //Buttom icon toolbar
  2346. if(($.fn.postitall.globals.showInfo && options.features.showInfo)
  2347. || ($.fn.postitall.globals.addNew && options.features.addNew)
  2348. || ($.fn.postitall.globals.showMeta && options.features.showMeta
  2349. && typeof options.meta !== 'undefined' && typeof options.meta === 'object')
  2350. ) {
  2351. var bottomToolbar = $('<div />', {
  2352. 'id': 'idPIAIconBottom_'+ index,
  2353. 'class': 'PIAIconBottom'
  2354. });
  2355. //Info icon (show whit showInfo or with meta)
  2356. if (($.fn.postitall.globals.showInfo && options.features.showInfo)
  2357. || ($.fn.postitall.globals.showMeta && options.features.showMeta
  2358. && typeof options.meta !== 'undefined' && typeof options.meta === 'object')
  2359. ) {
  2360. var info = $('<a />', {
  2361. 'href': '#',
  2362. 'id': 'idInfo_'+index,
  2363. 'class': ' PIAicon PIAinfoIcon',
  2364. }).click(function(e) {
  2365. if (obj.hasClass('PIAdragged')) {
  2366. obj.removeClass('PIAdragged');
  2367. } else {
  2368. $('.backContent_'+index).hide();
  2369. $('#idBackInfo_'+index+' > .PIABox').css({
  2370. //'width': options.width - 10,
  2371. 'height': options.height - 40
  2372. });
  2373. $('#idBackInfo_'+index+' .PIAtab span:nth-of-type(2)').css({
  2374. 'width': options.width - 30,
  2375. 'height': options.height - 60
  2376. });
  2377. $('#idBackInfo_'+index).show();
  2378. t.switchBackNoteOn('PIAflip2');
  2379. //Open first tab
  2380. if (($.fn.postitall.globals.showInfo && options.features.showInfo)
  2381. && ($.fn.postitall.globals.showMeta && options.features.showMeta
  2382. && typeof options.meta !== 'undefined' && typeof options.meta === 'object')) {
  2383. $('#idPIAtab-1'+index).click();
  2384. $('#idDateBackToolbar_' + index).hide();
  2385. }
  2386. }
  2387. e.preventDefault();
  2388. });
  2389. bottomToolbar.append(info);
  2390. }
  2391. //Copy note icon
  2392. if($.fn.postitall.globals.addNew && options.features.addNew) {
  2393. var newNote = $('<a />', {
  2394. 'href': '#',
  2395. 'id': 'pia_new_' + index,
  2396. 'class': 'icofont icofont-copy-alt PIAicon'
  2397. }).click(function (e) {
  2398. if (obj.hasClass('PIAdragged')) {
  2399. obj.removeClass('PIAdragged');
  2400. } else {
  2401. $.PostItAll.new({
  2402. content: options.content,
  2403. posX: parseInt(options.posX, 10) + 10,
  2404. posY: parseInt(options.posY, 10) + 10,
  2405. width: options.width,
  2406. height: options.height,
  2407. features: options.features,
  2408. attachedTo: options.attachedTo,
  2409. style: options.style,
  2410. }, function(id, options, obj) {
  2411. setTimeout(function() {
  2412. $('.PIApostit').css('z-index', 999995);
  2413. $(id).css('z-index', 999999);
  2414. }, 100);
  2415. });
  2416. }
  2417. e.preventDefault();
  2418. });
  2419. bottomToolbar.append(newNote);
  2420. }
  2421. //Export note
  2422. if(options.features.export) {
  2423. var exportNote = $('<a />', {
  2424. 'href': '#',
  2425. 'id': 'pia_export_' + index,
  2426. 'class': 'PIAexport PIAicon'
  2427. }).click(function (e) {
  2428. if (obj.hasClass('PIAdragged')) {
  2429. obj.removeClass('PIAdragged');
  2430. } else {
  2431. $.PostItAll.export(index);
  2432. }
  2433. e.preventDefault();
  2434. });
  2435. bottomToolbar.append(exportNote);
  2436. }
  2437. toolbar.prepend(bottomToolbar);
  2438. }
  2439. //Front page
  2440. var front = $('<div />', {
  2441. 'class': 'PIAfront',
  2442. 'dir': 'ltr',
  2443. }).append(toolbar).append(content);
  2444. //// BACK PAGES ////
  2445. //Back: Toolbar
  2446. var toolbar = this.__getBPToolbar(index);
  2447. //Back 1: config
  2448. var configInfo = this.__getBPConfig(index);
  2449. //Back 2: delete
  2450. var deleteInfo = this.__getBPDelete(index, obj);
  2451. //Back 3: hideUntil
  2452. var hideUntilPanel = this.__getBPHideUntil(index);
  2453. //Back 4: Info & meta data
  2454. var metaDataPanel = this.__getBPMetaData(index);
  2455. //Back page content
  2456. var back = $('<div />', {
  2457. 'class': 'PIAback PIAback1 PIAback2',
  2458. 'style': 'visibility: hidden;'
  2459. })
  2460. .append(toolbar)
  2461. .append(configInfo)
  2462. .append(deleteInfo)
  2463. .append(hideUntilPanel)
  2464. .append(metaDataPanel);
  2465. //Create postit
  2466. var postit = $('<div />', {
  2467. 'id': 'idPostIt_' + index,
  2468. 'data-id': index
  2469. });
  2470. //Add front
  2471. postit.append(front);
  2472. //Add back
  2473. postit.append(back);
  2474. //Convert relative position to prevent height and width in html layout
  2475. if (options.position === "relative") {
  2476. options.position = "absolute";
  2477. options.posY = obj.offset().top + parseInt(options.posY, 10);
  2478. options.posY += "px";
  2479. options.posX = obj.offset().left + parseInt(options.posX, 10);
  2480. options.posX += "px";
  2481. }
  2482. //Arrow
  2483. var arrowClases = " ";
  2484. var arrowPaso = false;
  2485. if($.fn.postitall.globals.addArrow != "none" && options.features.addArrow != "none") {
  2486. arrowClases += "arrow_box";
  2487. switch(options.style.arrow) {
  2488. case 'top':
  2489. arrowClases += ' arrow_box_top';
  2490. arrowPaso = true;
  2491. break;
  2492. case 'right':
  2493. arrowClases += ' arrow_box_right';
  2494. arrowPaso = true;
  2495. break;
  2496. case 'bottom':
  2497. arrowClases += ' arrow_box_bottom';
  2498. arrowPaso = true;
  2499. break;
  2500. case 'left':
  2501. arrowClases += ' arrow_box_left';
  2502. arrowPaso = true;
  2503. break;
  2504. }
  2505. }
  2506. //Modify final Postit Object
  2507. obj.removeClass()
  2508. .addClass('PIApostit ' + (options.style.tresd ? ' PIApanel ' : ' PIAplainpanel ')
  2509. + (options.position == "fixed" ? ' fixed ' : '') + arrowClases)
  2510. .css('position', options.position)
  2511. .css('top', options.posY)
  2512. .css('width', options.width + 'px')
  2513. .css('height', options.height + 'px')
  2514. .css('background-color', options.style.backgroundcolor)
  2515. .css('color', options.style.textcolor)
  2516. .css('font-family', options.style.fontfamily)
  2517. .css('font-size', options.style.fontsize)
  2518. .css('border-bottom-color', options.style.backgroundcolor)
  2519. .css('border-left-color', options.style.backgroundcolor)
  2520. .css('border-top-color', options.style.backgroundcolor)
  2521. .css('border-right-color', options.style.backgroundcolor);
  2522. if (options.right !== "") {
  2523. obj.css('right', options.right);
  2524. //options.right = "";
  2525. } else {
  2526. obj.css('left', options.posX)
  2527. }
  2528. if (options.style.textshadow) {
  2529. obj.addClass(t.getTextShadowStyle(options.style.textcolor));
  2530. } else {
  2531. obj.addClass('dosd');
  2532. }
  2533. obj.html(postit)
  2534. .on('focus', '#pia_editable_' + index, function () {
  2535. options.onSelect($.fn.postitall.globals.prefix + index);
  2536. if(options.flags.blocked)
  2537. return;
  2538. if($.fn.postitall.globals.htmlEditor && options.features.htmlEditor && $.trumbowyg) {
  2539. var paso = false;
  2540. //t.hoverOptions(index, false);
  2541. t.toogleToolbar('hide', ['idPIAIconBottom_', 'pia_toolbar_', '.selectedArrow_']);
  2542. $($.fn.postitall.globals.prefix + index + ' > .ui-resizable-handle').css('visibility', 'hidden');
  2543. $('#pia_editable_' + index).trumbowyg({
  2544. //prefix: 'trumbowyg-black trumbowyg-',
  2545. //btns: ['bold', 'italic', 'underline', 'strikethrough', '|', 'justifyLeft', 'justifyCenter', 'justifyRight', 'justifyFull', '|', 'link'],
  2546. btns: ['formatting',
  2547. '|', 'btnGrp-design',
  2548. '|', 'link',
  2549. '|', 'insertImage',
  2550. '|', 'btnGrp-justify',
  2551. '|', 'btnGrp-lists',
  2552. '|', 'horizontalRule',
  2553. '|', 'viewHTML'],
  2554. closable: true,
  2555. fullscreenable: false,
  2556. autogrow: true,
  2557. semantic: true,
  2558. })
  2559. .on('tbwfocus', function(){
  2560. //console.log('tbwfocus!');
  2561. //$( "#pia_editable_" + index ).dblclick();
  2562. //delay(function(){$( "#pia_editable_" + index ).focus();},500);
  2563. })
  2564. .on('tbwblur', function(){
  2565. //console.log('tbwblur!');
  2566. //delay(function() {
  2567. // $('#pia_editable_' + index).trumbowyg('destroy');
  2568. //},1000);
  2569. //$('.trumbowyg-close-button').click();
  2570. })
  2571. .on('tbwresize', function(){
  2572. //console.log('tbwresize!');
  2573. //delay(function() {
  2574. // this.autoresize($($.fn.postitall.globals.prefix + index));
  2575. //},1000);
  2576. });
  2577. $('#pia_editable_' + index).on('tbwclose', function(){
  2578. //console.log('tbwclose!');
  2579. $('#pia_editable_' + index).attr('contenteditable', true);
  2580. $('#pia_editable_' + index).css('height', 'auto');
  2581. t.toogleToolbar('show', ['idPIAIconBottom_', 'pia_toolbar_', '.selectedArrow_']);
  2582. $($.fn.postitall.globals.prefix + index + ' > .ui-resizable-handle').css('visibility', '');
  2583. t.autoresize();
  2584. var highlightedId = $("#the_lights").data('highlightedId');
  2585. if(highlightedId) {
  2586. t.collapseNote();
  2587. }
  2588. if(!paso) {
  2589. paso = true;
  2590. delay(function() {
  2591. options.onRelease($.fn.postitall.globals.prefix + index);
  2592. }, 100);
  2593. }
  2594. });
  2595. t.enableKeyboardNav(function() {
  2596. $('.trumbowyg-close-button').click();
  2597. t.autoresize();
  2598. });
  2599. t.autoresize();
  2600. }
  2601. var objeto = $(this);
  2602. objeto.data('before', objeto.html());
  2603. return objeto;
  2604. })
  2605. /*.on('focusout', '[contenteditable]', function () {
  2606. $('#pia_editable_' + index).trumbowyg('destroy');
  2607. $('#pia_editable_' + index).attr('contenteditable', true);
  2608. console.log('focusout!!', '#pia_editable_' + index);
  2609. var objeto = $(this);
  2610. return objeto;
  2611. })*/
  2612. .on('blur keyup paste input', '[contenteditable]', function (e) {
  2613. var objeto = $(this);
  2614. if (objeto.data('before') !== objeto.html()) {
  2615. delay(function() {
  2616. //console.log('change!');
  2617. var content = objeto.text();
  2618. if ($.fn.postitall.globals.pasteHtml || options.features.pasteHtml)
  2619. content = objeto.html();
  2620. objeto.data('before', content);
  2621. objeto.trigger('change');
  2622. options.onChange($.fn.postitall.globals.prefix + index);
  2623. //TODO : Validar que no es propaguen les tecles a l'aplicacio nativa
  2624. e.preventDefault();
  2625. }, 100);
  2626. }
  2627. return objeto;
  2628. })
  2629. .click(function (e) {
  2630. $('.PIApostit').css('z-index', 999995);
  2631. $(this).css('z-index', 999999);
  2632. });
  2633. if ($.ui) {
  2634. if ($.fn.postitall.globals.draggable && options.features.draggable) {
  2635. obj.draggable({
  2636. //handle: ".PIApostit",
  2637. scroll: false,
  2638. start: function (e) {
  2639. //Remove draggable postit option
  2640. $('.PIApostit').css('z-index', 999995);
  2641. $(this).css('z-index', 999999);
  2642. $(this).draggable('disable');
  2643. if(!options.flags.minimized) {
  2644. t.switchTrasparentNoteOn();
  2645. }
  2646. if(options.attachedTo.element !== "") {
  2647. options.attachedTo.element = '';
  2648. options.attachedTo.fixed = false;
  2649. }
  2650. obj.addClass('PIAdragged');
  2651. //onSelect event
  2652. options.onSelect($.fn.postitall.globals.prefix + options.id);
  2653. },
  2654. stop: function () {
  2655. //Enable draggable postit option
  2656. $(this).draggable('enable');
  2657. t.autoresize();
  2658. options.right = '';
  2659. //if ($.fn.postitall.globals.savable && options.features.savable) {
  2660. if(!options.flags.minimized) {
  2661. options.posY = obj.css('top');
  2662. options.posX = obj.css('left');
  2663. options.oldPosition.leftMinimized = undefined;
  2664. } else {
  2665. options.oldPosition.leftMinimized = obj.css('left');
  2666. }
  2667. t.saveOptions(options);
  2668. //}
  2669. if(!options.flags.minimized) {
  2670. t.switchTrasparentNoteOff();
  2671. }
  2672. delay(function() {
  2673. if (obj.hasClass('PIAdragged')) {
  2674. obj.removeClass('PIAdragged');
  2675. //onRelease event
  2676. options.onRelease($.fn.postitall.globals.prefix + options.id);
  2677. }
  2678. }, 200);
  2679. }
  2680. });
  2681. }
  2682. if ($.fn.postitall.globals.resizable && options.features.resizable) {
  2683. var pos = false;
  2684. //console.log('options.minHeight',options.minHeight);
  2685. obj.resizable({
  2686. animate: true,
  2687. helper: 'ui-resizable-helper',
  2688. minHeight: options.minHeight,
  2689. minWidth: options.minWidth,
  2690. start: function() {
  2691. t.switchTrasparentNoteOn();
  2692. //Change minheight on resizable
  2693. var tmpHeigth = $('#pia_editable_'+index).height();
  2694. if(tmpHeigth <= options.minHeight)
  2695. tmpHeigth = options.minHeight;
  2696. //$(this).resizable({minHeight: tmpHeigth });
  2697. //onSelect event
  2698. options.onSelect($.fn.postitall.globals.prefix + options.id);
  2699. },
  2700. stop: function () {
  2701. delay(function(){
  2702. //console.log('stop autoresize');
  2703. t.autoresize();
  2704. options.right = '';
  2705. //if ($.fn.postitall.globals.savable && options.features.savable) {
  2706. options.posY = obj.css('top');
  2707. options.posX = obj.css('left');
  2708. t.autoresize();
  2709. t.saveOptions(options);
  2710. //}
  2711. //onRelease event
  2712. options.onRelease($.fn.postitall.globals.prefix + options.id);
  2713. }, 1000);
  2714. t.switchTrasparentNoteOff();
  2715. }
  2716. });
  2717. }
  2718. } else {
  2719. //TODO : Resizable without jquer-ui
  2720. if ($.fn.postitall.globals.resizable && options.features.resizable) {
  2721. obj.css('overflow', 'hidden');
  2722. obj.css('resize', 'both');
  2723. obj.find('.PIAfront').css('height', '92%');
  2724. obj.find('.PIAIconBottom').css('bottom', '0%');
  2725. }
  2726. }
  2727. //hide back
  2728. $('.backContent_' + options.id).hide();
  2729. if(!options.style.tresd) {
  2730. $('#generalstyle_' + options.id).click();
  2731. }
  2732. if(!options.style.textshadow) {
  2733. $('#textshadow_' + options.id).click();
  2734. }
  2735. //Postit fixed?
  2736. if($.fn.postitall.globals.fixed && options.features.fixed && (options.flags.fixed || options.position == "fixed")) {
  2737. options.flags.fixed = false;
  2738. $('#pia_fixed_' + options.id).click();
  2739. }
  2740. //Postit expanded?
  2741. if($.fn.postitall.globals.expand && options.features.expand && options.flags.expand) {
  2742. options.flags.expand = false;
  2743. $('#pia_expand_' + options.id).click();
  2744. delay(function() {
  2745. $('#pia_editable_' + options.id).focus();
  2746. }, 500);
  2747. }
  2748. //Postit bloqued?
  2749. if(($.fn.postitall.globals.blocked || options.features.blocked) && options.flags.blocked) {
  2750. options.flags.blocked = false;
  2751. $('#pia_blocked_' + options.id).click();
  2752. }
  2753. //Postit minimized?
  2754. if($.fn.postitall.globals.minimized && options.features.minimized && options.flags.minimized) {
  2755. options.flags.minimized = false;
  2756. $('#pia_minimize_' + options.id).click();
  2757. }
  2758. //Select arrow in front
  2759. if( ($.fn.postitall.globals.addArrow == "front" || $.fn.postitall.globals.addArrow == "all")
  2760. || (options.features.addArrow == "all" || options.features.addArrow == "front") ) {
  2761. var checks = "<div class='PIAicon icon_box icon_box_top selectedArrow_"+index+"' data-index='"+index+"' data-value='top'><span class='ui-icon ui-icon-triangle-1-n'></span></div>";
  2762. checks += "<div class='PIAicon icon_box icon_box_right selectedArrow_"+index+"' data-index='"+index+"' data-value='right'><span class='ui-icon ui-icon-triangle-1-e'></span></div>";
  2763. checks += "<div class='PIAicon icon_box icon_box_bottom selectedArrow_"+index+"' data-index='"+index+"' data-value='bottom'><span class='ui-icon ui-icon-triangle-1-s'></span></div>";
  2764. checks += "<div class='PIAicon icon_box icon_box_left selectedArrow_"+index+"' data-index='"+index+"' data-value='left'><span class='ui-icon ui-icon-triangle-1-w'></span></div>";
  2765. obj.append(checks);
  2766. $('.selectedArrow_'+index).click(function(e) {
  2767. if (obj.hasClass('PIAdragged')) {
  2768. obj.removeClass('PIAdragged');
  2769. } else {
  2770. options = t.arrowChangeOption($(this).attr('data-value'));
  2771. }
  2772. e.preventDefault();
  2773. });
  2774. }
  2775. if(arrowPaso) {
  2776. var icon = $($.fn.postitall.globals.prefix+index).find('div[data-value="'+options.style.arrow+'"]');
  2777. icon.show();
  2778. icon.find('span').hide();
  2779. //console.log('arrowPaso', arrowPaso, index, options.style.arrow);
  2780. }
  2781. //Change hidden state if note is higlighted
  2782. if(options.flags.highlight)
  2783. options.flags.hidden = false;
  2784. //Show postit
  2785. if(!options.flags.hidden) {
  2786. obj.slideDown(function () {
  2787. //Rest of actions
  2788. //Config: text shadow
  2789. $('#textshadow_' + index).click(function () {
  2790. if ($(this).is(':checked')) {
  2791. $(this).closest('.PIApostit').find('.PIAcontent').addClass(t.getTextShadowStyle($('#minicolors_text_' + index).val())).removeClass('dosd');
  2792. options.style.textshadow = true;
  2793. } else {
  2794. $(this).closest('.PIApostit').find('.PIAcontent').addClass('dosd').removeClass('tresd').removeClass('tresdblack');
  2795. options.style.textshadow = false;
  2796. }
  2797. t.setOptions(options, true);
  2798. });
  2799. //3d or plain
  2800. $('#generalstyle_' + index).click(function () {
  2801. if ($(this).is(':checked')) {
  2802. $($.fn.postitall.globals.prefix + index).removeClass('PIAplainpanel').addClass('PIApanel');
  2803. options.style.tresd = true;
  2804. } else {
  2805. $($.fn.postitall.globals.prefix + index).removeClass('PIApanel').addClass('PIAplainpanel');
  2806. options.style.tresd = false;
  2807. }
  2808. t.setOptions(options, true);
  2809. });
  2810. //Background and text color
  2811. if ($.minicolors) {
  2812. //Config: change background-color
  2813. $('#minicolors_bg_' + index).minicolors({
  2814. //opacity: true,
  2815. change: function (hex, rgb) {
  2816. console.log(hex, rgb);
  2817. $($.fn.postitall.globals.prefix + index).css('background-color', hex);
  2818. //$($.fn.postitall.globals.prefix + index).css('opacity', rgb);
  2819. options.style.backgroundcolor = hex;
  2820. t.setOptions(options, true);
  2821. }
  2822. });
  2823. //Config: text color
  2824. $('#minicolors_text_' + index).minicolors({
  2825. change: function (hex) {
  2826. $($.fn.postitall.globals.prefix + index).css('color', hex);
  2827. options.style.textcolor = hex;
  2828. t.setOptions(options, true);
  2829. }
  2830. });
  2831. } else {
  2832. $('#minicolors_bg_' + index).change(function () {
  2833. $(this).closest('.PIApostit').css('background-color', $(this).val());
  2834. options.style.backgroundcolor = $(this).val();
  2835. t.setOptions(options, true);
  2836. });
  2837. $('#minicolors_text_' + index).change(function () {
  2838. $(this).closest('.PIApostit').css('color', $(this).val());
  2839. options.style.textcolor = $(this).val();
  2840. t.setOptions(options, true);
  2841. });
  2842. }
  2843. //Autoresize to fit content when content load is done
  2844. t.autoresize();
  2845. });
  2846. } else {
  2847. if(options.features.hideUntil !== null)
  2848. t.showAgain(index, options.features.hideUntil);
  2849. else
  2850. console.log('estic amagat sense data de fi', options.features);
  2851. }
  2852. //Hover options
  2853. if(!options.flags.minimized && !options.flags.expand && !options.flags.blocked) {
  2854. t.hoverOptions(index, true);
  2855. }
  2856. //disable draggable on mouseenter in contenteditable div
  2857. if ($.fn.postitall.globals.draggable && options.features.draggable) {
  2858. $("#pia_editable_" + index).mouseenter(function (e) {
  2859. if($.ui) obj.draggable({disabled: true});
  2860. }).mouseleave(function(e) {
  2861. if($.ui && !options.flags.blocked && !options.flags.expand) {
  2862. obj.draggable({disabled: false});
  2863. }
  2864. });
  2865. }
  2866. //Bind paste event
  2867. $("#pia_editable_" + index).bind('paste', function (e){
  2868. var element = this;
  2869. $("#pia_editable_" + index).css("opacity", "0");
  2870. setTimeout(function(){
  2871. var text = "";
  2872. if (!$.fn.postitall.globals.pasteHtml || !options.features.pasteHtml) {
  2873. //Text format
  2874. text = $(element).text();
  2875. } else {
  2876. //Html format
  2877. text = $(element).html();
  2878. //Default sanitize
  2879. text = text.replace(/<script[^>]*?>.*?<\/script>/gi, '').
  2880. //replace(/<[\/\!]*?[^<>]*?>/gi, '').
  2881. replace(/<style[^>]*?>.*?<\/style>/gi, '').
  2882. replace(/<![\s\S]*?--[ \t\n\r]*>/gi, '');
  2883. //htmlClean sanitize plugin
  2884. if($.htmlClean !== undefined) {
  2885. //htmlClean plugin
  2886. text = $.htmlClean(text, { format: true });
  2887. }
  2888. }
  2889. $("#pia_editable_" + index).html(text);
  2890. t.autoresize();
  2891. $("#pia_editable_" + index).css("opacity", "1");
  2892. }, 100);
  2893. });
  2894. //Bind click event
  2895. //$(obj).click(function() {
  2896. // options.onSelect($.fn.postitall.globals.prefix + index);
  2897. //});
  2898. //Bind dblclick event
  2899. $(obj).dblclick(function() {
  2900. options.onDblClick($.fn.postitall.globals.prefix + index);
  2901. });
  2902. //Stop key propagation on contenteditable
  2903. $("#pia_editable_" + index).keydown(function (e) {
  2904. e.stopPropagation();
  2905. });
  2906. var t = this;
  2907. setTimeout(function() {
  2908. //Save in storage
  2909. t.saveOptions(options);
  2910. //OnCreated event (id, options, obj)
  2911. options.onCreated($.fn.postitall.globals.prefix + index, options, obj);
  2912. //Highlight note
  2913. if(options.flags.highlight) {
  2914. t.scrollToNotePosition(options.posY, options.height, options.posX, options.width, function() {
  2915. t.switchOffLights();
  2916. t.enableKeyboardNav();
  2917. });
  2918. }
  2919. },200);
  2920. //chaining
  2921. return obj;
  2922. },
  2923. //// Back panels
  2924. //Toolbar
  2925. __getBPToolbar : function(index) {
  2926. var t = this;
  2927. var options = t.options;
  2928. //Creation date
  2929. var d = new Date(options.created);
  2930. //Back page: toolbar
  2931. var toolbar = $('<div />', { 'class': 'PIAtoolbarBack' }) //, 'style': barCursor })
  2932. //Close config icon
  2933. .append($('<div />', {
  2934. 'id': 'pia_close_' + index,
  2935. 'class': 'PIAclose PIAicon',
  2936. 'style': 'display:block;'
  2937. })
  2938. .click(function (e) {
  2939. t.switchBackNoteOff('PIAflip2');
  2940. t.switchOnLights();
  2941. e.preventDefault();
  2942. })
  2943. )
  2944. .append($('<span />', {
  2945. 'id': 'idDateBackToolbar_' + index,
  2946. 'class': 'PIAdateBackToolbar'
  2947. }).html(d.toLocaleDateString() + " (" + d.toLocaleTimeString() + ")")
  2948. );
  2949. return toolbar;
  2950. },
  2951. //Config back panel
  2952. __getBPConfig : function(index) {
  2953. var t = this;
  2954. var options = t.options;
  2955. //Back page: config content
  2956. var content = "";
  2957. if($.fn.postitall.globals.changeoptions) {
  2958. //Background color
  2959. var bgLabel = $('<label />', {
  2960. 'class': 'minicolors_label',
  2961. 'for': 'minicolors_bg_' + index,
  2962. }).html('Background-color:');
  2963. var bgString = $('<input />', {
  2964. 'class': 'minicolors',
  2965. 'id': 'minicolors_bg_' + index,
  2966. 'type': 'text',
  2967. 'height': '14px',
  2968. 'style': 'font-size:smaller;',
  2969. 'value': options.style.backgroundcolor,
  2970. 'data-default-value': options.style.backgroundcolor
  2971. });
  2972. //Text color
  2973. var tcLabel = $('<label />', {
  2974. 'class': 'minicolors_label',
  2975. 'for': 'minicolors_text_' + index,
  2976. 'style': 'margin-top: 5px;'
  2977. }).html('Text color:');
  2978. var tcString = $('<input />', {
  2979. 'class': 'minicolors',
  2980. 'id': 'minicolors_text_' + index,
  2981. 'type': 'text',
  2982. 'height': '14px',
  2983. 'style': 'font-size:smaller;',
  2984. 'value': options.style.textcolor,
  2985. 'data-default-value': options.style.textcolor
  2986. });
  2987. //Text shadow
  2988. var checked = '';
  2989. if (options.style.textshadow) {
  2990. checked = 'checked';
  2991. }
  2992. var tsString = $('<input />', {
  2993. 'id': 'textshadow_' + index,
  2994. 'type': 'checkbox',
  2995. 'checked': checked
  2996. });
  2997. var tsLabel = $('<label />', {
  2998. 'class': 'minicolors_label',
  2999. 'for': 'textshadow_' + index
  3000. }).append(tsString).append(' Text shadow');
  3001. //3d style
  3002. var checked2 = '';
  3003. if (options.style.tresd) {
  3004. checked2 = 'checked';
  3005. }
  3006. var gsString = $('<input />', {
  3007. 'id': 'generalstyle_' + index,
  3008. 'type': 'checkbox',
  3009. 'checked': checked2
  3010. });
  3011. var gsLabel = $('<label />', {
  3012. 'class': 'minicolors_label',
  3013. 'for': 'generalstyle_' + index,
  3014. }).append(gsString).append(' 3D style');
  3015. //Add arrow selection in options
  3016. var aaString = "";
  3017. if($.fn.postitall.globals.addArrow == "back" || $.fn.postitall.globals.addArrow == "all"
  3018. || options.features.addArrow == "back" || options.features.addArrow == "all") {
  3019. aaString = $('<select />', {
  3020. 'id': 'idAddArrow_' + index,
  3021. 'style': 'margin-top: 5px;',
  3022. });
  3023. aaString.append('<option value="none" '+(options.style.arrow == "none" ? 'selected' : '')+'>Arrow in:</option>');
  3024. aaString.append('<option value="top" '+(options.style.arrow == "top" ? 'selected' : '')+'>Top</option>');
  3025. aaString.append('<option value="right" '+(options.style.arrow == "right" ? 'selected' : '')+'>Right</option>');
  3026. aaString.append('<option value="bottom" '+(options.style.arrow == "bottom" ? 'selected' : '')+'>Bottom</option>');
  3027. aaString.append('<option value="left" '+(options.style.arrow == "left" ? 'selected' : '')+'>Left</option>');
  3028. aaString.change(function(e) {
  3029. options = t.arrowChangeOption($(this).val());
  3030. e.preventDefault();
  3031. });
  3032. }
  3033. content = $('<div />', {
  3034. 'id': 'idBackConfig_'+index,
  3035. 'class': 'PIAcontent backContent_'+index,
  3036. }).append($('<div />', {
  3037. 'class': 'PIABox PIAconfigBox',
  3038. //'style': 'margin-left: -5px;',
  3039. //'width': options.width - 10,
  3040. //'width': 'auto',
  3041. 'height': options.height - 40
  3042. }).append("<div class='PIAtitle'>Note config</div>")
  3043. .append(bgLabel).append(bgString) // Bg color
  3044. .append(gsLabel) // 3d or plain style
  3045. .append(tcLabel).append(tcString) // Text color
  3046. .append(tsLabel) // Text shadow
  3047. .append(aaString) //Arrow selection
  3048. );
  3049. }
  3050. return content;
  3051. },
  3052. //Delete back panel
  3053. __getBPDelete : function(index, obj) {
  3054. var t = this;
  3055. var options = t.options;
  3056. var deleteInfo = "";
  3057. if($.fn.postitall.globals.askOnDelete && options.features.askOnDelete) {
  3058. deleteInfo = $('<div />', {
  3059. 'id': 'idBackDelete_' + index,
  3060. 'class': 'PIAcontent backContent_'+index
  3061. }).append($('<div />', {
  3062. 'id': 'pia_confirmdel_' + index,
  3063. 'class': 'PIABox PIAwarningBox',
  3064. //'style': 'margin-left: -5px;',
  3065. //'width': options.width - 10,
  3066. //'width': 'auto',
  3067. 'height': options.height - 40
  3068. }).append("<div class='PIAtitle'>Delete note!</div>")
  3069. .append($('<span />', {
  3070. 'style': 'line-height:10px;font-size:10px;',
  3071. 'class': 'PIAdelwar float-left'
  3072. }))
  3073. .append($('<div />', { 'class': 'PIAconfirmOpt' }).append(
  3074. $('<a />', { 'id': 'sure_delete_' + index, 'href': '#' })
  3075. .click(function(e) {
  3076. t.switchOnLights();
  3077. var id = obj.data('PIA-id');
  3078. t.destroy();
  3079. e.preventDefault();
  3080. }).append($('<span />', { 'class': 'PIAdelyes' }).append("Delete this"))))
  3081. .append($('<div />', { 'class': 'PIAconfirmOpt' }).append(
  3082. $('<a />', { 'id': 'all_' + index, 'href': '#' })
  3083. .click(function(e) {
  3084. t.switchOnLights();
  3085. $.PostItAll.destroy();
  3086. e.preventDefault();
  3087. }).append($('<span />', { 'class': 'PIAdelyes' }).append("Delete all"))))
  3088. .append($('<div />', { 'class': 'PIAconfirmOpt' }).append(
  3089. $('<a />', { 'id': 'cancel_' + index, 'href': '#' })
  3090. .click(function(e) {
  3091. t.switchOnLights();
  3092. $('#pia_editable_' + index).show();
  3093. t.switchBackNoteOff('PIAflip2');
  3094. e.preventDefault();
  3095. }).append($('<span />', { 'class': 'PIAdelno' }).append("Cancel"))))
  3096. .append($('<div />', { 'class': 'clear', 'style': 'line-height:10px;font-size:10px;font-weight: bold;' }).append("*This action cannot be undone"))
  3097. );
  3098. }
  3099. return deleteInfo;
  3100. },
  3101. //Hide Until
  3102. __getBPHideUntil : function(index) {
  3103. var t = this;
  3104. var options = t.options;
  3105. var panel = "";
  3106. if($.fn.postitall.globals.askOnHide && options.features.askOnHide) {
  3107. var textInfo = "<div class='PIAtitle'>Hide note</div>";
  3108. var normalForm = "<div id='PIAhideUntilNormal_"+index+"'>";
  3109. normalForm += "<div>For how long do you want to hide this note?</div>";
  3110. normalForm += "<input type='text' id='numericIncrement_" + index + "' size=3 maxlength=2 value='30'>"
  3111. normalForm += "&nbsp;<select id='selectedIncrement_"+index+"'><option>Seconds</option><option>Minutes</option><option>Hours</option><option>Days</option></select>";
  3112. normalForm += "</div>";
  3113. var formContent = $('<div />');
  3114. var datePickerForm = "";
  3115. formContent.append(normalForm);
  3116. if($.ui) {
  3117. datePickerForm += "<div id='PIAhideUntilDatePicker_"+index+"' style='display:none;'>";
  3118. datePickerForm += "<div>The note will be displayed again on the following datetime : </div>";
  3119. if($.ui.timepicker) {
  3120. datePickerForm += "<input type='text' id='datepicker_"+index+"' placeholder='dd/mm/yyyy hh:mm'>";
  3121. } else {
  3122. datePickerForm += "<input type='text' id='datepicker_"+index+"' size=11 placeholder='dd/mm/yyyy'>";
  3123. datePickerForm += "&nbsp;<input type='textbox' placeholder='hh:mm' id='timepicker_" + index + "' size=6>";
  3124. }
  3125. datePickerForm +="</div>";
  3126. formContent.append(datePickerForm)
  3127. .append($('<label />').append($('<input />', { 'id': 'PIAhideUntilCB_' + index, 'type': 'checkbox' }).click(function(e) {
  3128. if ($(this).is(':checked')) {
  3129. if($.ui.timepicker) {
  3130. $('#datepicker_'+index).datetimepicker({ minDate: 0, dateFormat: "yy-mm-dd", timeFormat: "HH:mm" });
  3131. } else {
  3132. $('#datepicker_'+index).datepicker({ minDate: 0, dateFormat: "yy-mm-dd" });
  3133. }
  3134. $('#PIAhideUntilDatePicker_'+index).show();
  3135. $('#PIAhideUntilNormal_'+index).hide();
  3136. } else {
  3137. $('#PIAhideUntilDatePicker_'+index).hide();
  3138. $('#PIAhideUntilNormal_'+index).show();
  3139. }
  3140. })
  3141. ).append(' Datepicker'));
  3142. }
  3143. panel = $('<div />', {
  3144. 'id': 'idHideUntil_'+index,
  3145. 'class': 'PIAcontent backContent_'+index
  3146. }).append(
  3147. $('<div />', {
  3148. 'class': 'PIAinfoBox PIABox',
  3149. 'height': options.height - 40
  3150. }).append(textInfo)
  3151. .append(formContent)
  3152. .append($('<span />', { 'id': 'PIAhideUntilError_' + index}))
  3153. .append($('<br />'))
  3154. .append($('<button />', { 'text': 'Hide' })
  3155. .click(function(e) {
  3156. var dat = new Date();
  3157. var paso = false;
  3158. if($.ui) {
  3159. if($('#PIAhideUntilCB_' + index).is(':checked')) {
  3160. paso = true;
  3161. var gmt = new Date().toString().match(/([-\+][0-9]+)\s/)[1];
  3162. var datestr = "";
  3163. if($.ui.timepicker) {
  3164. //With timepicker
  3165. datestr = $('#datepicker_'+index).val();
  3166. } else {
  3167. //With datepicker
  3168. datestr = $('#datepicker_'+index).val() + " " + $('#timepicker_'+index).val();
  3169. }
  3170. datestr += " " + gmt;
  3171. dat = new Date(datestr);
  3172. }
  3173. }
  3174. if(!paso) {
  3175. //Normal
  3176. var inc = parseInt($('#numericIncrement_' + index).val(), 10);
  3177. if(inc < 0) inc = 1;
  3178. if(inc > 99) inc = 99;
  3179. switch ($('#selectedIncrement_' + index).val()) {
  3180. case 'Days':
  3181. dat = dat.addDays(inc);
  3182. break;
  3183. case 'Hours':
  3184. dat = dat.addHours(inc);
  3185. break;
  3186. case 'Minutes':
  3187. dat = dat.addMinutes(inc);
  3188. break;
  3189. case 'Seconds':
  3190. default:
  3191. dat = dat.addSeconds(inc);
  3192. break;
  3193. }
  3194. }
  3195. if(!isNaN(dat)) {
  3196. options.features.hideUntil = dat;
  3197. t.switchOnLights();
  3198. t.hide(index);
  3199. t.saveOptions(options);
  3200. t.showAgain(index, dat);
  3201. } else {
  3202. $('#PIAhideUntilError_' + index).css('color', 'red').html('Invalid date!').slideDown();
  3203. setTimeout(function() { $('#PIAhideUntilError_' + index).slideUp(); }, 1000);
  3204. }
  3205. e.preventDefault();
  3206. })
  3207. )
  3208. .append(
  3209. $('<button />', { 'text': 'Cancel' })
  3210. .click(function(e) {
  3211. t.switchOnLights();
  3212. $('#pia_editable_' + index).show();
  3213. t.switchBackNoteOff('PIAflip2');
  3214. e.preventDefault();
  3215. })
  3216. )
  3217. );
  3218. }
  3219. return panel;
  3220. },
  3221. //Meta data panel
  3222. __getBPMetaData : function(index) {
  3223. var t = this;
  3224. var options = t.options;
  3225. //Show info tab?
  3226. var addInfo = false;
  3227. if($.fn.postitall.globals.showInfo && options.features.showInfo) {
  3228. addInfo = true;
  3229. }
  3230. //Show meta data tab?
  3231. var addMeta = false;
  3232. if($.fn.postitall.globals.showMeta && options.features.showMeta
  3233. && typeof options.meta !== 'undefined' && typeof options.meta === 'object') {
  3234. addMeta = true;
  3235. }
  3236. //Nothing to show
  3237. if(!addInfo && !addMeta)
  3238. return "";
  3239. //Info note
  3240. if(addInfo) {
  3241. var d = new Date(options.created);
  3242. var textDate = d.toLocaleDateString() + " (" + d.toLocaleTimeString() + ")";
  3243. var textInfo = "";
  3244. if(!addMeta)
  3245. textInfo += "<div class='PIAtitle'>Note info</div>";
  3246. textInfo += "<strong>Id:</strong> "+$.fn.postitall.globals.prefix+index+"<br>";
  3247. textInfo += "<strong>Created on:</strong> "+textDate+"<br>";
  3248. if(typeof options.domain === 'object' && options.domain.indexOf("http") >= 0)
  3249. textInfo += "<strong>Domain:</strong> "+options.domain+"<br>";
  3250. textInfo += "<strong>Page:</strong> "+options.page+"<br>";
  3251. textInfo += "<strong>Op.System:</strong> " + t.getOSName() + " - "+options.osname+"<br>";
  3252. }
  3253. //Meta data
  3254. if(addMeta) {
  3255. //Get form input
  3256. var _FormElement = function(index, key, element) {
  3257. var ret = "";
  3258. if(typeof element === 'object' && element.type !== undefined) {
  3259. ret = $("<div />");
  3260. //On-change event
  3261. var _onChange = function(e) {
  3262. var tmpObj = $(this)
  3263. delay(function() {
  3264. //Save meta-data
  3265. options.meta[tmpObj.attr("data-title")].value = tmpObj.val();
  3266. t.setOptions(options, true);
  3267. },500);
  3268. };
  3269. var objElement = null;
  3270. var val = element.value;
  3271. //Input properties
  3272. var tmpObj = { 'name': 'input_' + key + '_' + index, 'data-title': key };
  3273. if(element.maxlength !== undefined) tmpObj.maxlength = parseInt(element.maxlength, 10);
  3274. if(element.size !== undefined) tmpObj.size = parseInt(element.size, 10);
  3275. if(element.placeholder !== undefined) tmpObj.placeholder = element.placeholder;
  3276. //type treatment
  3277. switch(element.type) {
  3278. //Combo type
  3279. case 'combo':
  3280. objElement = $('<select />', tmpObj).keypress(_onChange).change(_onChange);
  3281. $(element.values).each(function(i,e) {
  3282. $.each(this, function (tmpName, tmpValue) {
  3283. var tmpSelOpt = { 'value' : tmpName };
  3284. if(val !== undefined && val === tmpName) tmpSelOpt.selected = 'selected';
  3285. objElement.append($('<option />', tmpSelOpt).append(tmpValue));
  3286. });
  3287. });
  3288. break;
  3289. //Textarea type
  3290. case 'textarea':
  3291. objElement = $('<textarea />', tmpObj).keypress(_onChange).change(_onChange).append(val);
  3292. break;
  3293. //Default & input type
  3294. default:
  3295. case 'input':
  3296. tmpObj.type = 'text';
  3297. if(val !== undefined) tmpObj.value = val;
  3298. objElement = $('<input />', tmpObj).keypress(_onChange).change(_onChange);
  3299. break;
  3300. }
  3301. //Add form element
  3302. ret.append($("<label />").append(key).append($('<br />')).append(objElement));
  3303. }
  3304. return ret;
  3305. };
  3306. var formInfo = $('<div />', { 'class' : 'meta-form' });
  3307. $(options.meta).each(function() {
  3308. $.each(this, function (key, element) {
  3309. formInfo.append(_FormElement(index, key, element));
  3310. });
  3311. });
  3312. }
  3313. var finalPanel = "";
  3314. var panelClass = "";
  3315. if(addInfo && addMeta) {
  3316. //Tabs : info & meta
  3317. panelClass = "PIApanelBox";
  3318. finalPanel = $('<div />', { 'class' : 'PIAtabs' }).append(
  3319. $('<div />', { 'id' : 'PIAtab-1'+index, 'class': 'PIAtab' }).append(
  3320. $('<span />').append(
  3321. $('<a />', { 'id' : 'idPIAtab-1'+index }).click(function() {
  3322. $('#PIAtab-1'+index).addClass('PIAtab_selected');
  3323. $('#PIAtab-2'+index).removeClass('PIAtab_selected');
  3324. $('#idPIApanel1'+index).show();
  3325. $('#idPIApanel2'+index).hide();
  3326. }).append("Note info")
  3327. )
  3328. ).append(
  3329. $('<span />', { 'id' : 'idPIApanel1'+index, 'class' : 'PIAinfoBox' }).append(textInfo)
  3330. )
  3331. ).append(
  3332. $('<div />', { 'id' : 'PIAtab-2'+index, 'class': 'PIAtab' }).append(
  3333. $('<span />').append(
  3334. $('<a />', { 'id' : 'idPIAtab-2'+index }).click(function() {
  3335. $('#PIAtab-1'+index).removeClass('PIAtab_selected');
  3336. $('#PIAtab-2'+index).addClass('PIAtab_selected');
  3337. $('#idPIApanel1'+index).hide();
  3338. $('#idPIApanel2'+index).show();
  3339. }).append("Meta data")
  3340. )
  3341. ).append(
  3342. $('<span />', { 'id' : 'idPIApanel2'+index, 'class' : 'PIAinfoBox' }).append(formInfo)
  3343. )
  3344. );
  3345. } else if(addInfo) {
  3346. //Only info
  3347. panelClass = "PIAinfoBox";
  3348. finalPanel = textInfo
  3349. } else {
  3350. //Only meta
  3351. panelClass = "PIAinfoBox";
  3352. var formTitle = $('<div />', { 'class' : 'PIAtitle' }).append("Meta data");
  3353. finalPanel = formInfo.prepend(formTitle);
  3354. }
  3355. //Final obj
  3356. var panel = $('<div />', {
  3357. 'id': 'idBackInfo_'+index,
  3358. 'class': 'PIAcontent backContent_'+index
  3359. }).append(
  3360. $('<div />', {
  3361. 'class': 'PIABox ' + panelClass,
  3362. 'height': options.height - 40
  3363. }).append(finalPanel)
  3364. );
  3365. return panel;
  3366. }
  3367. };
  3368. //Drag postits
  3369. //used if jQuery UI is not loaded
  3370. $.fn.drags = function (opt) {
  3371. opt = $.extend({handle: "", cursor: "move"}, opt);
  3372. var onMouseDown = function(e) {
  3373. var $drag;
  3374. if (opt.handle === "") {
  3375. $drag = $(this).parent().parent().parent().addClass('draggable');
  3376. } else {
  3377. $drag = $(this).parent().parent().parent().addClass('active-handle').parent().addClass('draggable');
  3378. }
  3379. var options = $drag.data('PIA-options');
  3380. if(options.flags.minimized || options.flags.expand)
  3381. return;
  3382. var z_idx = $drag.css('z-index'),
  3383. drg_h = $drag.outerHeight(),
  3384. drg_w = $drag.outerWidth(),
  3385. pos_y = $drag.offset().top + drg_h - e.pageY,
  3386. pos_x = $drag.offset().left + drg_w - e.pageX;
  3387. var onMouseMove = function(e) {
  3388. $drag.addClass('PIAdragged');
  3389. $('.draggable').offset({
  3390. top: e.pageY + pos_y - drg_h,
  3391. left: e.pageX + pos_x - drg_w
  3392. }).on("mouseup", function () {
  3393. $(this).removeClass('draggable').css('z-index', z_idx);
  3394. });
  3395. };
  3396. $drag.css('z-index', 10000).parents()
  3397. .on("mousemove", onMouseMove)
  3398. .on("mouseup",function() {
  3399. $(this).off("mousemove", onMouseMove);
  3400. delay(function() {
  3401. $drag.removeClass('PIAdragged');
  3402. },200);
  3403. });
  3404. e.preventDefault(); // disable selection
  3405. };
  3406. var onMouseUp = function(e) {
  3407. var $drag;
  3408. if (opt.handle === "") {
  3409. $(this).removeClass('draggable');
  3410. $drag = $(this).parent().parent().parent();
  3411. } else {
  3412. $(this).removeClass('active-handle').parent().removeClass('draggable');
  3413. $drag = $(this).parent().parent().parent().parent();
  3414. }
  3415. var options = $drag.data('PIA-options');
  3416. options.right = '';
  3417. if(!options.flags.minimized) {
  3418. options.posY = $drag.css('top');
  3419. options.posX = $drag.css('left');
  3420. options.oldPosition.leftMinimized = undefined;
  3421. } else {
  3422. options.oldPosition.leftMinimized = $drag.css('left');
  3423. }
  3424. $($.fn.postitall.globals.prefix + options.id).postitall('save', options);
  3425. };
  3426. var $el = this;
  3427. if (opt.handle !== "") {
  3428. $el = this.find(opt.handle);
  3429. }
  3430. return $el.css('cursor', opt.cursor).on("mousedown", onMouseDown).on("mouseup", onMouseUp);
  3431. };
  3432. /********* STORAGE ************/
  3433. // Global storage var
  3434. var $storage = null;
  3435. // Storage Manager
  3436. var storageManager = {
  3437. add: function (obj, callback) {
  3438. this.loadManager(function() {
  3439. $storage.add(obj, function(error) {
  3440. if(callback != null) callback(error);
  3441. });
  3442. });
  3443. },
  3444. get: function (id, callback) {
  3445. this.loadManager(function() {
  3446. if(id !== "") {
  3447. $storage.get(id, function(varvalue) {
  3448. if(callback != null) callback(varvalue);
  3449. });
  3450. } else {
  3451. if(callback != null) callback(null);
  3452. }
  3453. });
  3454. },
  3455. getAll: function (callback) {
  3456. this.loadManager(function() {
  3457. $storage.getAll(function(varvalue) {
  3458. if(callback != null) callback(varvalue);
  3459. });
  3460. });
  3461. },
  3462. getByKey: function (key, callback) {
  3463. this.loadManager(function() {
  3464. if (key != null && key.slice(0,7) === "PostIt_") {
  3465. key = key.slice(7,key.length);
  3466. if(key !== "") {
  3467. storageManager.get(key, callback);
  3468. } else {
  3469. if(callback != null) callback(null);
  3470. }
  3471. } else {
  3472. if(callback != null) callback(null);
  3473. }
  3474. });
  3475. },
  3476. remove: function (id, callback) {
  3477. this.loadManager(function() {
  3478. $storage.remove(id, function(varvalue) {
  3479. if(callback != null) callback();
  3480. });
  3481. });
  3482. },
  3483. removeDom: function (options, callback) {
  3484. this.loadManager(function() {
  3485. $storage.removeDom(options, function() {
  3486. if(callback != null) callback();
  3487. });
  3488. });
  3489. },
  3490. clear: function (options, callback) {
  3491. this.loadManager(function() {
  3492. $storage.clear(function() {
  3493. if(callback != null) callback();
  3494. });
  3495. });
  3496. },
  3497. getlength: function (callback) {
  3498. this.loadManager(function() {
  3499. $storage.getlength(function(length) {
  3500. if(callback != null) callback(length);
  3501. });
  3502. });
  3503. },
  3504. key: function (i, callback) {
  3505. this.loadManager(function() {
  3506. $storage.key(i, function(name) {
  3507. if(callback != null) callback(name);
  3508. });
  3509. });
  3510. },
  3511. view: function (callback) {
  3512. this.loadManager(function() {
  3513. $storage.view();
  3514. });
  3515. },
  3516. //Load storage manager
  3517. loadManager: function(callback) {
  3518. if($storage === null) {
  3519. this.getStorageManager(function($tmpStorage) {
  3520. $storage = $tmpStorage;
  3521. callback($storage)
  3522. });
  3523. } else {
  3524. callback($storage);
  3525. }
  3526. },
  3527. // Get storage manager
  3528. getStorageManager: function(callback) {
  3529. if (typeof externalManager !== 'undefined') callback(externalManager);
  3530. else callback(localManager);
  3531. }
  3532. };
  3533. // local storage manager
  3534. var localManager = {
  3535. add: function (obj, callback) {
  3536. var varname = 'PostIt_' + obj.id.toString();
  3537. var testPrefs = JSON.stringify(obj);
  3538. $localStorage.setItem(varname, testPrefs);
  3539. //console.log('Saved', varname, testPrefs);
  3540. if(callback != null) callback("");
  3541. },
  3542. get: function (id, callback) {
  3543. var varname = 'PostIt_' + id.toString();
  3544. var varvalue = $localStorage.getItem(varname);
  3545. if(varvalue != null)
  3546. varvalue = JSON.parse(varvalue);
  3547. else
  3548. varvalue = "";
  3549. //console.log('Loaded', varname, varvalue);
  3550. if(callback != null) callback(varvalue);
  3551. },
  3552. remove: function (id, callback) {
  3553. $localStorage.removeItem('PostIt_' + id);
  3554. if(callback != null) callback();
  3555. },
  3556. removeDom: function (options, callback) {
  3557. this.clear(callback);
  3558. },
  3559. clear: function (callback) {
  3560. $localStorage.clear();
  3561. if(callback != null) callback();
  3562. },
  3563. getlength: function (callback) {
  3564. callback($localStorage.length);
  3565. },
  3566. key: function (i, callback) {
  3567. i--;
  3568. var name = $localStorage.key(i);
  3569. callback(name);
  3570. },
  3571. view: function () {
  3572. console.log('view local');
  3573. console.log($localStorage);
  3574. },
  3575. getAll: function (callback) {
  3576. console.log('TODO getAll on localStorage');
  3577. }
  3578. };
  3579. //Initialize environement
  3580. $(document).ready(function() {
  3581. $.PostItAll.__initialize();
  3582. });
  3583. }(jQuery, window.localStorage));