functions.js 134 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167
  1. /* vim: set expandtab sw=4 ts=4 sts=4: */
  2. /**
  3. * general function, usually for data manipulation pages
  4. *
  5. */
  6. /**
  7. * @var $table_clone reference to the action links on the tbl_structure page
  8. */
  9. var $table_clone = false;
  10. /**
  11. * @var sql_box_locked lock for the sqlbox textarea in the querybox/querywindow
  12. */
  13. var sql_box_locked = false;
  14. /**
  15. * @var array holds elements which content should only selected once
  16. */
  17. var only_once_elements = [];
  18. /**
  19. * @var int ajax_message_count Number of AJAX messages shown since page load
  20. */
  21. var ajax_message_count = 0;
  22. /**
  23. * @var codemirror_editor object containing CodeMirror editor of the query editor in SQL tab
  24. */
  25. var codemirror_editor = false;
  26. /**
  27. * @var codemirror_editor object containing CodeMirror editor of the inline query editor
  28. */
  29. var codemirror_inline_editor = false;
  30. /**
  31. * @var chart_activeTimeouts object active timeouts that refresh the charts. When disabling a realtime chart, this can be used to stop the continuous ajax requests
  32. */
  33. var chart_activeTimeouts = {};
  34. /**
  35. * Make sure that ajax requests will not be cached
  36. * by appending a random variable to their parameters
  37. */
  38. $.ajaxPrefilter(function (options, originalOptions, jqXHR) {
  39. var nocache = new Date().getTime() + "" + Math.floor(Math.random() * 1000000);
  40. if (typeof options.data == "string") {
  41. options.data += "&_nocache=" + nocache;
  42. } else if (typeof options.data == "object") {
  43. options.data = $.extend(originalOptions.data, {'_nocache' : nocache});
  44. }
  45. });
  46. /**
  47. * Add a hidden field to the form to indicate that this will be an
  48. * Ajax request (only if this hidden field does not exist)
  49. *
  50. * @param object the form
  51. */
  52. function PMA_prepareForAjaxRequest($form)
  53. {
  54. if (! $form.find('input:hidden').is('#ajax_request_hidden')) {
  55. $form.append('<input type="hidden" id="ajax_request_hidden" name="ajax_request" value="true" />');
  56. }
  57. }
  58. /**
  59. * Generate a new password and copy it to the password input areas
  60. *
  61. * @param object the form that holds the password fields
  62. *
  63. * @return boolean always true
  64. */
  65. function suggestPassword(passwd_form)
  66. {
  67. // restrict the password to just letters and numbers to avoid problems:
  68. // "editors and viewers regard the password as multiple words and
  69. // things like double click no longer work"
  70. var pwchars = "abcdefhjmnpqrstuvwxyz23456789ABCDEFGHJKLMNPQRSTUVWYXZ";
  71. var passwordlength = 16; // do we want that to be dynamic? no, keep it simple :)
  72. var passwd = passwd_form.generated_pw;
  73. passwd.value = '';
  74. for (var i = 0; i < passwordlength; i++) {
  75. passwd.value += pwchars.charAt(Math.floor(Math.random() * pwchars.length));
  76. }
  77. passwd_form.text_pma_pw.value = passwd.value;
  78. passwd_form.text_pma_pw2.value = passwd.value;
  79. return true;
  80. }
  81. /**
  82. * Version string to integer conversion.
  83. */
  84. function parseVersionString(str)
  85. {
  86. if (typeof(str) != 'string') { return false; }
  87. var add = 0;
  88. // Parse possible alpha/beta/rc/
  89. var state = str.split('-');
  90. if (state.length >= 2) {
  91. if (state[1].substr(0, 2) == 'rc') {
  92. add = - 20 - parseInt(state[1].substr(2), 10);
  93. } else if (state[1].substr(0, 4) == 'beta') {
  94. add = - 40 - parseInt(state[1].substr(4), 10);
  95. } else if (state[1].substr(0, 5) == 'alpha') {
  96. add = - 60 - parseInt(state[1].substr(5), 10);
  97. } else if (state[1].substr(0, 3) == 'dev') {
  98. /* We don't handle dev, it's git snapshot */
  99. add = 0;
  100. }
  101. }
  102. // Parse version
  103. var x = str.split('.');
  104. // Use 0 for non existing parts
  105. var maj = parseInt(x[0], 10) || 0;
  106. var min = parseInt(x[1], 10) || 0;
  107. var pat = parseInt(x[2], 10) || 0;
  108. var hotfix = parseInt(x[3], 10) || 0;
  109. return maj * 100000000 + min * 1000000 + pat * 10000 + hotfix * 100 + add;
  110. }
  111. /**
  112. * Indicates current available version on main page.
  113. */
  114. function PMA_current_version(data)
  115. {
  116. if (data && data.version && data.date) {
  117. var current = parseVersionString(pmaversion);
  118. var latest = parseVersionString(data.version);
  119. var version_information_message = '<span>'
  120. + PMA_messages.strLatestAvailable
  121. + ' ' + escapeHtml(data.version)
  122. + '</span>';
  123. if (latest > current) {
  124. var message = $.sprintf(
  125. PMA_messages.strNewerVersion,
  126. escapeHtml(data.version),
  127. escapeHtml(data.date)
  128. );
  129. var htmlClass = 'notice';
  130. if (Math.floor(latest / 10000) === Math.floor(current / 10000)) {
  131. /* Security update */
  132. htmlClass = 'error';
  133. }
  134. $('#maincontainer').after('<div class="' + htmlClass + '">' + message + '</div>');
  135. }
  136. if (latest === current) {
  137. version_information_message = ' (' + PMA_messages.strUpToDate + ')';
  138. }
  139. $('#li_pma_version span').remove();
  140. $('#li_pma_version').append(version_information_message);
  141. }
  142. }
  143. /**
  144. * Loads Git revision data from ajax for index.php
  145. */
  146. function PMA_display_git_revision()
  147. {
  148. $('#is_git_revision').remove();
  149. $('#li_pma_version_git').remove();
  150. $.get(
  151. "index.php",
  152. {
  153. "server": PMA_commonParams.get('server'),
  154. "token": PMA_commonParams.get('token'),
  155. "git_revision": true,
  156. "ajax_request": true
  157. },
  158. function (data) {
  159. if (data.success === true) {
  160. $(data.message).insertAfter('#li_pma_version');
  161. }
  162. }
  163. );
  164. }
  165. /**
  166. * for libraries/display_change_password.lib.php
  167. * libraries/user_password.php
  168. *
  169. */
  170. function displayPasswordGenerateButton()
  171. {
  172. $('#tr_element_before_generate_password').parent().append('<tr class="vmiddle"><td>' + PMA_messages.strGeneratePassword + '</td><td><input type="button" class="button" id="button_generate_password" value="' + PMA_messages.strGenerate + '" onclick="suggestPassword(this.form)" /><input type="text" name="generated_pw" id="generated_pw" /></td></tr>');
  173. $('#div_element_before_generate_password').parent().append('<div class="item"><label for="button_generate_password">' + PMA_messages.strGeneratePassword + ':</label><span class="options"><input type="button" class="button" id="button_generate_password" value="' + PMA_messages.strGenerate + '" onclick="suggestPassword(this.form)" /></span><input type="text" name="generated_pw" id="generated_pw" /></div>');
  174. }
  175. /*
  176. * Adds a date/time picker to an element
  177. *
  178. * @param object $this_element a jQuery object pointing to the element
  179. */
  180. function PMA_addDatepicker($this_element, type, options)
  181. {
  182. var showTimepicker = true;
  183. if (type=="date") {
  184. showTimepicker = false;
  185. }
  186. var defaultOptions = {
  187. showOn: 'button',
  188. buttonImage: themeCalendarImage, // defined in js/messages.php
  189. buttonImageOnly: true,
  190. stepMinutes: 1,
  191. stepHours: 1,
  192. showSecond: true,
  193. showMillisec: true,
  194. showMicrosec: true,
  195. showTimepicker: showTimepicker,
  196. showButtonPanel: false,
  197. dateFormat: 'yy-mm-dd', // yy means year with four digits
  198. timeFormat: 'HH:mm:ss.lc',
  199. constrainInput: false,
  200. altFieldTimeOnly: false,
  201. showAnim: '',
  202. beforeShow: function (input, inst) {
  203. // Remember that we came from the datepicker; this is used
  204. // in tbl_change.js by verificationsAfterFieldChange()
  205. $this_element.data('comes_from', 'datepicker');
  206. // Fix wrong timepicker z-index, doesn't work without timeout
  207. setTimeout(function () {
  208. $('#ui-timepicker-div').css('z-index', $('#ui-datepicker-div').css('z-index'));
  209. }, 0);
  210. },
  211. onClose: function (dateText, dp_inst) {
  212. // The value is no more from the date picker
  213. $this_element.data('comes_from', '');
  214. }
  215. };
  216. if (type == "datetime" || type == "timestamp") {
  217. $this_element.datetimepicker($.extend(defaultOptions, options));
  218. }
  219. else if (type == "date") {
  220. $this_element.datetimepicker($.extend(defaultOptions, options));
  221. }
  222. else if (type == "time") {
  223. $this_element.timepicker($.extend(defaultOptions, options));
  224. }
  225. }
  226. /**
  227. * selects the content of a given object, f.e. a textarea
  228. *
  229. * @param object element element of which the content will be selected
  230. * @param var lock variable which holds the lock for this element
  231. * or true, if no lock exists
  232. * @param boolean only_once if true this is only done once
  233. * f.e. only on first focus
  234. */
  235. function selectContent(element, lock, only_once)
  236. {
  237. if (only_once && only_once_elements[element.name]) {
  238. return;
  239. }
  240. only_once_elements[element.name] = true;
  241. if (lock) {
  242. return;
  243. }
  244. element.select();
  245. }
  246. /**
  247. * Displays a confirmation box before submitting a "DROP/DELETE/ALTER" query.
  248. * This function is called while clicking links
  249. *
  250. * @param object the link
  251. * @param object the sql query to submit
  252. *
  253. * @return boolean whether to run the query or not
  254. */
  255. function confirmLink(theLink, theSqlQuery)
  256. {
  257. // Confirmation is not required in the configuration file
  258. // or browser is Opera (crappy js implementation)
  259. if (PMA_messages.strDoYouReally === '' || typeof(window.opera) != 'undefined') {
  260. return true;
  261. }
  262. var is_confirmed = confirm($.sprintf(PMA_messages.strDoYouReally, theSqlQuery));
  263. if (is_confirmed) {
  264. if ($(theLink).hasClass('formLinkSubmit')) {
  265. var name = 'is_js_confirmed';
  266. if ($(theLink).attr('href').indexOf('usesubform') != -1) {
  267. name = 'subform[' + $(theLink).attr('href').substr('#').match(/usesubform\[(\d+)\]/i)[1] + '][is_js_confirmed]';
  268. }
  269. $(theLink).parents('form').append('<input type="hidden" name="' + name + '" value="1" />');
  270. } else if (typeof(theLink.href) != 'undefined') {
  271. theLink.href += '&is_js_confirmed=1';
  272. } else if (typeof(theLink.form) != 'undefined') {
  273. theLink.form.action += '?is_js_confirmed=1';
  274. }
  275. }
  276. return is_confirmed;
  277. } // end of the 'confirmLink()' function
  278. /**
  279. * Displays an error message if a "DROP DATABASE" statement is submitted
  280. * while it isn't allowed, else confirms a "DROP/DELETE/ALTER" query before
  281. * sumitting it if required.
  282. * This function is called by the 'checkSqlQuery()' js function.
  283. *
  284. * @param object the form
  285. * @param object the sql query textarea
  286. *
  287. * @return boolean whether to run the query or not
  288. *
  289. * @see checkSqlQuery()
  290. */
  291. function confirmQuery(theForm1, sqlQuery1)
  292. {
  293. // Confirmation is not required in the configuration file
  294. if (PMA_messages.strDoYouReally === '') {
  295. return true;
  296. }
  297. // "DROP DATABASE" statement isn't allowed
  298. if (PMA_messages.strNoDropDatabases !== '') {
  299. var drop_re = new RegExp('(^|;)\\s*DROP\\s+(IF EXISTS\\s+)?DATABASE\\s', 'i');
  300. if (drop_re.test(sqlQuery1.value)) {
  301. alert(PMA_messages.strNoDropDatabases);
  302. theForm1.reset();
  303. sqlQuery1.focus();
  304. return false;
  305. } // end if
  306. } // end if
  307. // Confirms a "DROP/DELETE/ALTER/TRUNCATE" statement
  308. //
  309. // TODO: find a way (if possible) to use the parser-analyser
  310. // for this kind of verification
  311. // For now, I just added a ^ to check for the statement at
  312. // beginning of expression
  313. var do_confirm_re_0 = new RegExp('^\\s*DROP\\s+(IF EXISTS\\s+)?(TABLE|DATABASE|PROCEDURE)\\s', 'i');
  314. var do_confirm_re_1 = new RegExp('^\\s*ALTER\\s+TABLE\\s+((`[^`]+`)|([A-Za-z0-9_$]+))\\s+DROP\\s', 'i');
  315. var do_confirm_re_2 = new RegExp('^\\s*DELETE\\s+FROM\\s', 'i');
  316. var do_confirm_re_3 = new RegExp('^\\s*TRUNCATE\\s', 'i');
  317. if (do_confirm_re_0.test(sqlQuery1.value) ||
  318. do_confirm_re_1.test(sqlQuery1.value) ||
  319. do_confirm_re_2.test(sqlQuery1.value) ||
  320. do_confirm_re_3.test(sqlQuery1.value)) {
  321. var message;
  322. if (sqlQuery1.value.length > 100) {
  323. message = sqlQuery1.value.substr(0, 100) + '\n ...';
  324. } else {
  325. message = sqlQuery1.value;
  326. }
  327. var is_confirmed = confirm($.sprintf(PMA_messages.strDoYouReally, message));
  328. // statement is confirmed -> update the
  329. // "is_js_confirmed" form field so the confirm test won't be
  330. // run on the server side and allows to submit the form
  331. if (is_confirmed) {
  332. theForm1.elements['is_js_confirmed'].value = 1;
  333. return true;
  334. }
  335. // statement is rejected -> do not submit the form
  336. else {
  337. window.focus();
  338. sqlQuery1.focus();
  339. return false;
  340. } // end if (handle confirm box result)
  341. } // end if (display confirm box)
  342. return true;
  343. } // end of the 'confirmQuery()' function
  344. /**
  345. * Displays an error message if the user submitted the sql query form with no
  346. * sql query, else checks for "DROP/DELETE/ALTER" statements
  347. *
  348. * @param object the form
  349. *
  350. * @return boolean always false
  351. *
  352. * @see confirmQuery()
  353. */
  354. function checkSqlQuery(theForm)
  355. {
  356. var sqlQuery;
  357. // get the textarea element containing the query
  358. if (codemirror_editor) {
  359. codemirror_editor.save();
  360. sqlQuery = codemirror_editor.getValue();
  361. } else {
  362. sqlQuery = theForm.elements.sql_query.value;
  363. }
  364. var isEmpty = 1;
  365. var space_re = new RegExp('\\s+');
  366. if (typeof(theForm.elements['sql_file']) != 'undefined' &&
  367. theForm.elements['sql_file'].value.replace(space_re, '') !== '') {
  368. return true;
  369. }
  370. if (typeof(theForm.elements['sql_localfile']) != 'undefined' &&
  371. theForm.elements['sql_localfile'].value.replace(space_re, '') !== '') {
  372. return true;
  373. }
  374. if (isEmpty && typeof(theForm.elements['id_bookmark']) != 'undefined' &&
  375. (theForm.elements['id_bookmark'].value !== null || theForm.elements['id_bookmark'].value !== '') &&
  376. theForm.elements['id_bookmark'].selectedIndex !== 0) {
  377. return true;
  378. }
  379. // Checks for "DROP/DELETE/ALTER" statements
  380. if (sqlQuery.replace(space_re, '') !== '') {
  381. if (confirmQuery(theForm, sqlQuery)) {
  382. return true;
  383. } else {
  384. return false;
  385. }
  386. }
  387. theForm.reset();
  388. isEmpty = 1;
  389. if (isEmpty) {
  390. alert(PMA_messages.strFormEmpty);
  391. codemirror_editor.focus();
  392. return false;
  393. }
  394. return true;
  395. } // end of the 'checkSqlQuery()' function
  396. /**
  397. * Check if a form's element is empty.
  398. * An element containing only spaces is also considered empty
  399. *
  400. * @param object the form
  401. * @param string the name of the form field to put the focus on
  402. *
  403. * @return boolean whether the form field is empty or not
  404. */
  405. function emptyCheckTheField(theForm, theFieldName)
  406. {
  407. var theField = theForm.elements[theFieldName];
  408. var space_re = new RegExp('\\s+');
  409. return (theField.value.replace(space_re, '') === '') ? 1 : 0;
  410. } // end of the 'emptyCheckTheField()' function
  411. /**
  412. * Check whether a form field is empty or not
  413. *
  414. * @param object the form
  415. * @param string the name of the form field to put the focus on
  416. *
  417. * @return boolean whether the form field is empty or not
  418. */
  419. function emptyFormElements(theForm, theFieldName)
  420. {
  421. var theField = theForm.elements[theFieldName];
  422. var isEmpty = emptyCheckTheField(theForm, theFieldName);
  423. return isEmpty;
  424. } // end of the 'emptyFormElements()' function
  425. /**
  426. * Ensures a value submitted in a form is numeric and is in a range
  427. *
  428. * @param object the form
  429. * @param string the name of the form field to check
  430. * @param integer the minimum authorized value
  431. * @param integer the maximum authorized value
  432. *
  433. * @return boolean whether a valid number has been submitted or not
  434. */
  435. function checkFormElementInRange(theForm, theFieldName, message, min, max)
  436. {
  437. var theField = theForm.elements[theFieldName];
  438. var val = parseInt(theField.value, 10);
  439. if (typeof(min) == 'undefined') {
  440. min = 0;
  441. }
  442. if (typeof(max) == 'undefined') {
  443. max = Number.MAX_VALUE;
  444. }
  445. // It's not a number
  446. if (isNaN(val)) {
  447. theField.select();
  448. alert(PMA_messages.strEnterValidNumber);
  449. theField.focus();
  450. return false;
  451. }
  452. // It's a number but it is not between min and max
  453. else if (val < min || val > max) {
  454. theField.select();
  455. alert($.sprintf(message, val));
  456. theField.focus();
  457. return false;
  458. }
  459. // It's a valid number
  460. else {
  461. theField.value = val;
  462. }
  463. return true;
  464. } // end of the 'checkFormElementInRange()' function
  465. function checkTableEditForm(theForm, fieldsCnt)
  466. {
  467. // TODO: avoid sending a message if user just wants to add a line
  468. // on the form but has not completed at least one field name
  469. var atLeastOneField = 0;
  470. var i, elm, elm2, elm3, val, id;
  471. for (i = 0; i < fieldsCnt; i++) {
  472. id = "#field_" + i + "_2";
  473. elm = $(id);
  474. val = elm.val();
  475. if (val == 'VARCHAR' || val == 'CHAR' || val == 'BIT' || val == 'VARBINARY' || val == 'BINARY') {
  476. elm2 = $("#field_" + i + "_3");
  477. val = parseInt(elm2.val(), 10);
  478. elm3 = $("#field_" + i + "_1");
  479. if (isNaN(val) && elm3.val() !== "") {
  480. elm2.select();
  481. alert(PMA_messages.strEnterValidLength);
  482. elm2.focus();
  483. return false;
  484. }
  485. }
  486. if (atLeastOneField === 0) {
  487. id = "field_" + i + "_1";
  488. if (!emptyCheckTheField(theForm, id)) {
  489. atLeastOneField = 1;
  490. }
  491. }
  492. }
  493. if (atLeastOneField === 0) {
  494. var theField = theForm.elements["field_0_1"];
  495. alert(PMA_messages.strFormEmpty);
  496. theField.focus();
  497. return false;
  498. }
  499. // at least this section is under jQuery
  500. if ($("input.textfield[name='table']").val() === "") {
  501. alert(PMA_messages.strFormEmpty);
  502. $("input.textfield[name='table']").focus();
  503. return false;
  504. }
  505. return true;
  506. } // enf of the 'checkTableEditForm()' function
  507. /**
  508. * Unbind all event handlers before tearing down a page
  509. */
  510. AJAX.registerTeardown('functions.js', function () {
  511. $('input:checkbox.checkall').die('click');
  512. });
  513. AJAX.registerOnload('functions.js', function () {
  514. /**
  515. * Row marking in horizontal mode (use "live" so that it works also for
  516. * next pages reached via AJAX); a tr may have the class noclick to remove
  517. * this behavior.
  518. */
  519. $('input:checkbox.checkall').live('click', function (e) {
  520. var $tr = $(this).closest('tr');
  521. // make the table unselectable (to prevent default highlighting when shift+click)
  522. //$tr.parents('table').noSelect();
  523. if (!e.shiftKey || last_clicked_row == -1) {
  524. // usual click
  525. // XXX: FF fires two click events for <label> (label and checkbox), so we need to handle this differently
  526. var $checkbox = $tr.find(':checkbox');
  527. if ($checkbox.length) {
  528. // checkbox in a row, add or remove class depending on checkbox state
  529. var checked = $checkbox.prop('checked');
  530. if (!$(e.target).is(':checkbox, label')) {
  531. checked = !checked;
  532. $checkbox.prop('checked', checked).trigger('change');
  533. }
  534. if (checked) {
  535. $tr.addClass('marked');
  536. } else {
  537. $tr.removeClass('marked');
  538. }
  539. last_click_checked = checked;
  540. } else {
  541. // normal data table, just toggle class
  542. $tr.toggleClass('marked');
  543. last_click_checked = false;
  544. }
  545. // remember the last clicked row
  546. last_clicked_row = last_click_checked ? $('tr.odd:not(.noclick), tr.even:not(.noclick)').index($tr) : -1;
  547. last_shift_clicked_row = -1;
  548. } else {
  549. // handle the shift click
  550. PMA_clearSelection();
  551. var start, end;
  552. // clear last shift click result
  553. if (last_shift_clicked_row >= 0) {
  554. if (last_shift_clicked_row >= last_clicked_row) {
  555. start = last_clicked_row;
  556. end = last_shift_clicked_row;
  557. } else {
  558. start = last_shift_clicked_row;
  559. end = last_clicked_row;
  560. }
  561. $tr.parent().find('tr.odd:not(.noclick), tr.even:not(.noclick)')
  562. .slice(start, end + 1)
  563. .removeClass('marked')
  564. .find(':checkbox')
  565. .prop('checked', false)
  566. .trigger('change');
  567. }
  568. // handle new shift click
  569. var curr_row = $('tr.odd:not(.noclick), tr.even:not(.noclick)').index($tr);
  570. if (curr_row >= last_clicked_row) {
  571. start = last_clicked_row;
  572. end = curr_row;
  573. } else {
  574. start = curr_row;
  575. end = last_clicked_row;
  576. }
  577. $tr.parent().find('tr.odd:not(.noclick), tr.even:not(.noclick)')
  578. .slice(start, end + 1)
  579. .addClass('marked')
  580. .find(':checkbox')
  581. .prop('checked', true)
  582. .trigger('change');
  583. // remember the last shift clicked row
  584. last_shift_clicked_row = curr_row;
  585. }
  586. });
  587. addDateTimePicker();
  588. /**
  589. * Add attribute to text boxes for iOS devices (based on bugID: 3508912)
  590. */
  591. if (navigator.userAgent.match(/(iphone|ipod|ipad)/i)) {
  592. $('input[type=text]').attr('autocapitalize', 'off').attr('autocorrect', 'off');
  593. }
  594. });
  595. /**
  596. * True if last click is to check a row.
  597. */
  598. var last_click_checked = false;
  599. /**
  600. * Zero-based index of last clicked row.
  601. * Used to handle the shift + click event in the code above.
  602. */
  603. var last_clicked_row = -1;
  604. /**
  605. * Zero-based index of last shift clicked row.
  606. */
  607. var last_shift_clicked_row = -1;
  608. /**
  609. * Row highlighting in horizontal mode (use "live"
  610. * so that it works also for pages reached via AJAX)
  611. */
  612. /*AJAX.registerOnload('functions.js', function () {
  613. $('tr.odd, tr.even').live('hover',function (event) {
  614. var $tr = $(this);
  615. $tr.toggleClass('hover',event.type=='mouseover');
  616. $tr.children().toggleClass('hover',event.type=='mouseover');
  617. });
  618. })*/
  619. /**
  620. * This array is used to remember mark status of rows in browse mode
  621. */
  622. var marked_row = [];
  623. /**
  624. * marks all rows and selects its first checkbox inside the given element
  625. * the given element is usaly a table or a div containing the table or tables
  626. *
  627. * @param container DOM element
  628. */
  629. function markAllRows(container_id)
  630. {
  631. $("#" + container_id).find("input:checkbox:enabled").prop('checked', true)
  632. .trigger("change")
  633. .parents("tr").addClass("marked");
  634. return true;
  635. }
  636. /**
  637. * marks all rows and selects its first checkbox inside the given element
  638. * the given element is usaly a table or a div containing the table or tables
  639. *
  640. * @param container DOM element
  641. */
  642. function unMarkAllRows(container_id)
  643. {
  644. $("#" + container_id).find("input:checkbox:enabled").prop('checked', false)
  645. .trigger("change")
  646. .parents("tr").removeClass("marked");
  647. return true;
  648. }
  649. /**
  650. * Checks/unchecks all checkbox in given conainer (f.e. a form, fieldset or div)
  651. *
  652. * @param string container_id the container id
  653. * @param boolean state new value for checkbox (true or false)
  654. * @return boolean always true
  655. */
  656. function setCheckboxes(container_id, state)
  657. {
  658. $("#" + container_id).find("input:checkbox").prop('checked', state);
  659. return true;
  660. } // end of the 'setCheckboxes()' function
  661. /**
  662. * Checks/unchecks all options of a <select> element
  663. *
  664. * @param string the form name
  665. * @param string the element name
  666. * @param boolean whether to check or to uncheck options
  667. *
  668. * @return boolean always true
  669. */
  670. function setSelectOptions(the_form, the_select, do_check)
  671. {
  672. $("form[name='" + the_form + "'] select[name='" + the_select + "']").find("option").prop('selected', do_check);
  673. return true;
  674. } // end of the 'setSelectOptions()' function
  675. /**
  676. * Sets current value for query box.
  677. */
  678. function setQuery(query)
  679. {
  680. if (codemirror_editor) {
  681. codemirror_editor.setValue(query);
  682. codemirror_editor.focus();
  683. } else {
  684. document.sqlform.sql_query.value = query;
  685. document.sqlform.sql_query.focus();
  686. }
  687. }
  688. /**
  689. * Create quick sql statements.
  690. *
  691. */
  692. function insertQuery(queryType)
  693. {
  694. if (queryType == "clear") {
  695. setQuery('');
  696. return;
  697. }
  698. var query = "";
  699. var myListBox = document.sqlform.dummy;
  700. var table = document.sqlform.table.value;
  701. if (myListBox.options.length > 0) {
  702. sql_box_locked = true;
  703. var columnsList = "";
  704. var valDis = "";
  705. var editDis = "";
  706. var NbSelect = 0;
  707. for (var i = 0; i < myListBox.options.length; i++) {
  708. NbSelect++;
  709. if (NbSelect > 1) {
  710. columnsList += ", ";
  711. valDis += ",";
  712. editDis += ",";
  713. }
  714. columnsList += myListBox.options[i].value;
  715. valDis += "[value-" + NbSelect + "]";
  716. editDis += myListBox.options[i].value + "=[value-" + NbSelect + "]";
  717. }
  718. if (queryType == "selectall") {
  719. query = "SELECT * FROM `" + table + "` WHERE 1";
  720. } else if (queryType == "select") {
  721. query = "SELECT " + columnsList + " FROM `" + table + "` WHERE 1";
  722. } else if (queryType == "insert") {
  723. query = "INSERT INTO `" + table + "`(" + columnsList + ") VALUES (" + valDis + ")";
  724. } else if (queryType == "update") {
  725. query = "UPDATE `" + table + "` SET " + editDis + " WHERE 1";
  726. } else if (queryType == "delete") {
  727. query = "DELETE FROM `" + table + "` WHERE 1";
  728. }
  729. setQuery(query);
  730. sql_box_locked = false;
  731. }
  732. }
  733. /**
  734. * Inserts multiple fields.
  735. *
  736. */
  737. function insertValueQuery()
  738. {
  739. var myQuery = document.sqlform.sql_query;
  740. var myListBox = document.sqlform.dummy;
  741. if (myListBox.options.length > 0) {
  742. sql_box_locked = true;
  743. var columnsList = "";
  744. var NbSelect = 0;
  745. for (var i = 0; i < myListBox.options.length; i++) {
  746. if (myListBox.options[i].selected) {
  747. NbSelect++;
  748. if (NbSelect > 1) {
  749. columnsList += ", ";
  750. }
  751. columnsList += myListBox.options[i].value;
  752. }
  753. }
  754. /* CodeMirror support */
  755. if (codemirror_editor) {
  756. codemirror_editor.replaceSelection(columnsList);
  757. //IE support
  758. } else if (document.selection) {
  759. myQuery.focus();
  760. var sel = document.selection.createRange();
  761. sel.text = columnsList;
  762. document.sqlform.insert.focus();
  763. }
  764. //MOZILLA/NETSCAPE support
  765. else if (document.sqlform.sql_query.selectionStart || document.sqlform.sql_query.selectionStart == "0") {
  766. var startPos = document.sqlform.sql_query.selectionStart;
  767. var endPos = document.sqlform.sql_query.selectionEnd;
  768. var SqlString = document.sqlform.sql_query.value;
  769. myQuery.value = SqlString.substring(0, startPos) + columnsList + SqlString.substring(endPos, SqlString.length);
  770. } else {
  771. myQuery.value += columnsList;
  772. }
  773. sql_box_locked = false;
  774. }
  775. }
  776. /**
  777. * Add a date/time picker to each element that needs it
  778. * (only when jquery-ui-timepicker-addon.js is loaded)
  779. */
  780. function addDateTimePicker() {
  781. if ($.timepicker !== undefined) {
  782. $('input.timefield, input.datefield, input.datetimefield').each(function () {
  783. no_decimals = $(this).parent().attr('data-decimals');
  784. var showMillisec = false;
  785. var showMicrosec = false;
  786. var timeFormat = 'HH:mm:ss';
  787. // check for decimal places of seconds
  788. if (($(this).parent().attr('data-decimals') > 0) && ($(this).parent().attr('data-type').indexOf('time') != -1)){
  789. showMillisec = true;
  790. timeFormat = 'HH:mm:ss.lc';
  791. if ($(this).parent().attr('data-decimals') > 3) {
  792. showMicrosec = true;
  793. }
  794. }
  795. PMA_addDatepicker($(this), $(this).parent().attr('data-type'), {
  796. showMillisec: showMillisec,
  797. showMicrosec: showMicrosec,
  798. timeFormat: timeFormat
  799. });
  800. });
  801. }
  802. }
  803. /**
  804. * Refresh/resize the WYSIWYG scratchboard
  805. */
  806. function refreshLayout()
  807. {
  808. var $elm = $('#pdflayout');
  809. var orientation = $('#orientation_opt').val();
  810. var paper = 'A4';
  811. if ($('#paper_opt').length == 1) {
  812. paper = $('#paper_opt').val();
  813. }
  814. var posa = 'y';
  815. var posb = 'x';
  816. if (orientation == 'P') {
  817. posa = 'x';
  818. posb = 'y';
  819. }
  820. $elm.css('width', pdfPaperSize(paper, posa) + 'px');
  821. $elm.css('height', pdfPaperSize(paper, posb) + 'px');
  822. }
  823. /**
  824. * Initializes positions of elements.
  825. */
  826. function TableDragInit() {
  827. $('.pdflayout_table').each(function () {
  828. var $this = $(this);
  829. var number = $this.data('number');
  830. var x = $('#c_table_' + number + '_x').val();
  831. var y = $('#c_table_' + number + '_y').val();
  832. $this.css('left', x + 'px');
  833. $this.css('top', y + 'px');
  834. /* Make elements draggable */
  835. $this.draggable({
  836. containment: "parent",
  837. drag: function (evt, ui) {
  838. var number = $this.data('number');
  839. $('#c_table_' + number + '_x').val(parseInt(ui.position.left, 10));
  840. $('#c_table_' + number + '_y').val(parseInt(ui.position.top, 10));
  841. }
  842. });
  843. });
  844. }
  845. /**
  846. * Resets drag and drop positions.
  847. */
  848. function resetDrag() {
  849. $('.pdflayout_table').each(function () {
  850. var $this = $(this);
  851. var x = $this.data('x');
  852. var y = $this.data('y');
  853. $this.css('left', x + 'px');
  854. $this.css('top', y + 'px');
  855. });
  856. }
  857. /**
  858. * User schema handlers.
  859. */
  860. $(function () {
  861. /* Move in scratchboard on manual change */
  862. $('.position-change').live('change', function () {
  863. var $this = $(this);
  864. var $elm = $('#table_' + $this.data('number'));
  865. $elm.css($this.data('axis'), $this.val() + 'px');
  866. });
  867. /* Refresh on paper size/orientation change */
  868. $('.paper-change').live('change', function () {
  869. var $elm = $('#pdflayout');
  870. if ($elm.css('visibility') == 'visible') {
  871. refreshLayout();
  872. TableDragInit();
  873. }
  874. });
  875. /* Show/hide the WYSIWYG scratchboard */
  876. $('#toggle-dragdrop').live('click', function () {
  877. var $elm = $('#pdflayout');
  878. if ($elm.css('visibility') == 'hidden') {
  879. refreshLayout();
  880. TableDragInit();
  881. $elm.css('visibility', 'visible');
  882. $elm.css('display', 'block');
  883. $('#showwysiwyg').val('1');
  884. } else {
  885. $elm.css('visibility', 'hidden');
  886. $elm.css('display', 'none');
  887. $('#showwysiwyg').val('0');
  888. }
  889. });
  890. /* Reset scratchboard */
  891. $('#reset-dragdrop').live('click', function () {
  892. resetDrag();
  893. });
  894. });
  895. /**
  896. * Returns paper sizes for a given format
  897. */
  898. function pdfPaperSize(format, axis)
  899. {
  900. switch (format.toUpperCase()) {
  901. case '4A0':
  902. if (axis == 'x') {
  903. return 4767.87;
  904. } else {
  905. return 6740.79;
  906. }
  907. break;
  908. case '2A0':
  909. if (axis == 'x') {
  910. return 3370.39;
  911. } else {
  912. return 4767.87;
  913. }
  914. break;
  915. case 'A0':
  916. if (axis == 'x') {
  917. return 2383.94;
  918. } else {
  919. return 3370.39;
  920. }
  921. break;
  922. case 'A1':
  923. if (axis == 'x') {
  924. return 1683.78;
  925. } else {
  926. return 2383.94;
  927. }
  928. break;
  929. case 'A2':
  930. if (axis == 'x') {
  931. return 1190.55;
  932. } else {
  933. return 1683.78;
  934. }
  935. break;
  936. case 'A3':
  937. if (axis == 'x') {
  938. return 841.89;
  939. } else {
  940. return 1190.55;
  941. }
  942. break;
  943. case 'A4':
  944. if (axis == 'x') {
  945. return 595.28;
  946. } else {
  947. return 841.89;
  948. }
  949. break;
  950. case 'A5':
  951. if (axis == 'x') {
  952. return 419.53;
  953. } else {
  954. return 595.28;
  955. }
  956. break;
  957. case 'A6':
  958. if (axis == 'x') {
  959. return 297.64;
  960. } else {
  961. return 419.53;
  962. }
  963. break;
  964. case 'A7':
  965. if (axis == 'x') {
  966. return 209.76;
  967. } else {
  968. return 297.64;
  969. }
  970. break;
  971. case 'A8':
  972. if (axis == 'x') {
  973. return 147.40;
  974. } else {
  975. return 209.76;
  976. }
  977. break;
  978. case 'A9':
  979. if (axis == 'x') {
  980. return 104.88;
  981. } else {
  982. return 147.40;
  983. }
  984. break;
  985. case 'A10':
  986. if (axis == 'x') {
  987. return 73.70;
  988. } else {
  989. return 104.88;
  990. }
  991. break;
  992. case 'B0':
  993. if (axis == 'x') {
  994. return 2834.65;
  995. } else {
  996. return 4008.19;
  997. }
  998. break;
  999. case 'B1':
  1000. if (axis == 'x') {
  1001. return 2004.09;
  1002. } else {
  1003. return 2834.65;
  1004. }
  1005. break;
  1006. case 'B2':
  1007. if (axis == 'x') {
  1008. return 1417.32;
  1009. } else {
  1010. return 2004.09;
  1011. }
  1012. break;
  1013. case 'B3':
  1014. if (axis == 'x') {
  1015. return 1000.63;
  1016. } else {
  1017. return 1417.32;
  1018. }
  1019. break;
  1020. case 'B4':
  1021. if (axis == 'x') {
  1022. return 708.66;
  1023. } else {
  1024. return 1000.63;
  1025. }
  1026. break;
  1027. case 'B5':
  1028. if (axis == 'x') {
  1029. return 498.90;
  1030. } else {
  1031. return 708.66;
  1032. }
  1033. break;
  1034. case 'B6':
  1035. if (axis == 'x') {
  1036. return 354.33;
  1037. } else {
  1038. return 498.90;
  1039. }
  1040. break;
  1041. case 'B7':
  1042. if (axis == 'x') {
  1043. return 249.45;
  1044. } else {
  1045. return 354.33;
  1046. }
  1047. break;
  1048. case 'B8':
  1049. if (axis == 'x') {
  1050. return 175.75;
  1051. } else {
  1052. return 249.45;
  1053. }
  1054. break;
  1055. case 'B9':
  1056. if (axis == 'x') {
  1057. return 124.72;
  1058. } else {
  1059. return 175.75;
  1060. }
  1061. break;
  1062. case 'B10':
  1063. if (axis == 'x') {
  1064. return 87.87;
  1065. } else {
  1066. return 124.72;
  1067. }
  1068. break;
  1069. case 'C0':
  1070. if (axis == 'x') {
  1071. return 2599.37;
  1072. } else {
  1073. return 3676.54;
  1074. }
  1075. break;
  1076. case 'C1':
  1077. if (axis == 'x') {
  1078. return 1836.85;
  1079. } else {
  1080. return 2599.37;
  1081. }
  1082. break;
  1083. case 'C2':
  1084. if (axis == 'x') {
  1085. return 1298.27;
  1086. } else {
  1087. return 1836.85;
  1088. }
  1089. break;
  1090. case 'C3':
  1091. if (axis == 'x') {
  1092. return 918.43;
  1093. } else {
  1094. return 1298.27;
  1095. }
  1096. break;
  1097. case 'C4':
  1098. if (axis == 'x') {
  1099. return 649.13;
  1100. } else {
  1101. return 918.43;
  1102. }
  1103. break;
  1104. case 'C5':
  1105. if (axis == 'x') {
  1106. return 459.21;
  1107. } else {
  1108. return 649.13;
  1109. }
  1110. break;
  1111. case 'C6':
  1112. if (axis == 'x') {
  1113. return 323.15;
  1114. } else {
  1115. return 459.21;
  1116. }
  1117. break;
  1118. case 'C7':
  1119. if (axis == 'x') {
  1120. return 229.61;
  1121. } else {
  1122. return 323.15;
  1123. }
  1124. break;
  1125. case 'C8':
  1126. if (axis == 'x') {
  1127. return 161.57;
  1128. } else {
  1129. return 229.61;
  1130. }
  1131. break;
  1132. case 'C9':
  1133. if (axis == 'x') {
  1134. return 113.39;
  1135. } else {
  1136. return 161.57;
  1137. }
  1138. break;
  1139. case 'C10':
  1140. if (axis == 'x') {
  1141. return 79.37;
  1142. } else {
  1143. return 113.39;
  1144. }
  1145. break;
  1146. case 'RA0':
  1147. if (axis == 'x') {
  1148. return 2437.80;
  1149. } else {
  1150. return 3458.27;
  1151. }
  1152. break;
  1153. case 'RA1':
  1154. if (axis == 'x') {
  1155. return 1729.13;
  1156. } else {
  1157. return 2437.80;
  1158. }
  1159. break;
  1160. case 'RA2':
  1161. if (axis == 'x') {
  1162. return 1218.90;
  1163. } else {
  1164. return 1729.13;
  1165. }
  1166. break;
  1167. case 'RA3':
  1168. if (axis == 'x') {
  1169. return 864.57;
  1170. } else {
  1171. return 1218.90;
  1172. }
  1173. break;
  1174. case 'RA4':
  1175. if (axis == 'x') {
  1176. return 609.45;
  1177. } else {
  1178. return 864.57;
  1179. }
  1180. break;
  1181. case 'SRA0':
  1182. if (axis == 'x') {
  1183. return 2551.18;
  1184. } else {
  1185. return 3628.35;
  1186. }
  1187. break;
  1188. case 'SRA1':
  1189. if (axis == 'x') {
  1190. return 1814.17;
  1191. } else {
  1192. return 2551.18;
  1193. }
  1194. break;
  1195. case 'SRA2':
  1196. if (axis == 'x') {
  1197. return 1275.59;
  1198. } else {
  1199. return 1814.17;
  1200. }
  1201. break;
  1202. case 'SRA3':
  1203. if (axis == 'x') {
  1204. return 907.09;
  1205. } else {
  1206. return 1275.59;
  1207. }
  1208. break;
  1209. case 'SRA4':
  1210. if (axis == 'x') {
  1211. return 637.80;
  1212. } else {
  1213. return 907.09;
  1214. }
  1215. break;
  1216. case 'LETTER':
  1217. if (axis == 'x') {
  1218. return 612.00;
  1219. } else {
  1220. return 792.00;
  1221. }
  1222. break;
  1223. case 'LEGAL':
  1224. if (axis == 'x') {
  1225. return 612.00;
  1226. } else {
  1227. return 1008.00;
  1228. }
  1229. break;
  1230. case 'EXECUTIVE':
  1231. if (axis == 'x') {
  1232. return 521.86;
  1233. } else {
  1234. return 756.00;
  1235. }
  1236. break;
  1237. case 'FOLIO':
  1238. if (axis == 'x') {
  1239. return 612.00;
  1240. } else {
  1241. return 936.00;
  1242. }
  1243. break;
  1244. } // end switch
  1245. return 0;
  1246. }
  1247. /**
  1248. * Unbind all event handlers before tearing down a page
  1249. */
  1250. AJAX.registerTeardown('functions.js', function () {
  1251. $("a.inline_edit_sql").die('click');
  1252. $("input#sql_query_edit_save").die('click');
  1253. $("input#sql_query_edit_discard").die('click');
  1254. $('input.sqlbutton').unbind('click');
  1255. $("#export_type").unbind('change');
  1256. $('#sqlquery').unbind('keydown');
  1257. $('#sql_query_edit').unbind('keydown');
  1258. if (codemirror_inline_editor) {
  1259. // Copy the sql query to the text area to preserve it.
  1260. $('#sql_query_edit').text(codemirror_inline_editor.getValue());
  1261. $(codemirror_inline_editor.getWrapperElement()).unbind('keydown');
  1262. codemirror_inline_editor.toTextArea();
  1263. codemirror_inline_editor = false;
  1264. }
  1265. if (codemirror_editor) {
  1266. $(codemirror_editor.getWrapperElement()).unbind('keydown');
  1267. }
  1268. });
  1269. /**
  1270. * Jquery Coding for inline editing SQL_QUERY
  1271. */
  1272. AJAX.registerOnload('functions.js', function () {
  1273. // If we are coming back to the page by clicking forward button
  1274. // of the browser, bind the code mirror to inline query editor.
  1275. bindCodeMirrorToInlineEditor();
  1276. $("a.inline_edit_sql").live('click', function () {
  1277. if ($('#sql_query_edit').length) {
  1278. // An inline query editor is already open,
  1279. // we don't want another copy of it
  1280. return false;
  1281. }
  1282. var $form = $(this).prev('form');
  1283. var sql_query = $form.find("input[name='sql_query']").val().trim();
  1284. var $inner_sql = $(this).parent().prev().find('code.sql');
  1285. var old_text = $inner_sql.html();
  1286. var new_content = "<textarea name=\"sql_query_edit\" id=\"sql_query_edit\">" + sql_query + "</textarea>\n";
  1287. new_content += "<input type=\"submit\" id=\"sql_query_edit_save\" class=\"button btnSave\" value=\"" + PMA_messages.strGo + "\"/>\n";
  1288. new_content += "<input type=\"button\" id=\"sql_query_edit_discard\" class=\"button btnDiscard\" value=\"" + PMA_messages.strCancel + "\"/>\n";
  1289. var $editor_area = $('div#inline_editor');
  1290. if ($editor_area.length === 0) {
  1291. $editor_area = $('<div id="inline_editor_outer"></div>');
  1292. $editor_area.insertBefore($inner_sql);
  1293. }
  1294. $editor_area.html(new_content);
  1295. $inner_sql.hide();
  1296. bindCodeMirrorToInlineEditor();
  1297. return false;
  1298. });
  1299. $("input#sql_query_edit_save").live('click', function () {
  1300. $(".success").hide();
  1301. //hide already existing success message
  1302. var sql_query;
  1303. if (codemirror_inline_editor) {
  1304. codemirror_inline_editor.save();
  1305. sql_query = codemirror_inline_editor.getValue();
  1306. } else {
  1307. sql_query = $(this).prev().val();
  1308. }
  1309. var $form = $("a.inline_edit_sql").prev('form');
  1310. var $fake_form = $('<form>', {action: 'import.php', method: 'post'})
  1311. .append($form.find("input[name=server], input[name=db], input[name=table], input[name=token]").clone())
  1312. .append($('<input/>', {type: 'hidden', name: 'show_query', value: 1}))
  1313. .append($('<input/>', {type: 'hidden', name: 'is_js_confirmed', value: 0}))
  1314. .append($('<input/>', {type: 'hidden', name: 'sql_query', value: sql_query}));
  1315. if (! checkSqlQuery($fake_form[0])) {
  1316. return false;
  1317. }
  1318. $fake_form.appendTo($('body')).submit();
  1319. });
  1320. $("input#sql_query_edit_discard").live('click', function () {
  1321. $('div#inline_editor_outer').siblings('code.sql').show();
  1322. $('div#inline_editor_outer').remove();
  1323. });
  1324. $('input.sqlbutton').click(function (evt) {
  1325. insertQuery(evt.target.id);
  1326. return false;
  1327. });
  1328. $("#export_type").change(function () {
  1329. if ($("#export_type").val() == 'svg') {
  1330. $("#show_grid_opt").prop("disabled", true);
  1331. $("#orientation_opt").prop("disabled", true);
  1332. $("#with_doc").prop("disabled", true);
  1333. $("#show_table_dim_opt").removeProp("disabled");
  1334. $("#all_tables_same_width").removeProp("disabled");
  1335. $("#paper_opt").removeProp("disabled");
  1336. $("#show_color_opt").removeProp("disabled");
  1337. //$(this).css("background-color","yellow");
  1338. } else if ($("#export_type").val() == 'dia') {
  1339. $("#show_grid_opt").prop("disabled", true);
  1340. $("#with_doc").prop("disabled", true);
  1341. $("#show_table_dim_opt").prop("disabled", true);
  1342. $("#all_tables_same_width").prop("disabled", true);
  1343. $("#paper_opt").removeProp("disabled");
  1344. $("#show_color_opt").removeProp("disabled");
  1345. $("#orientation_opt").removeProp("disabled");
  1346. } else if ($("#export_type").val() == 'eps') {
  1347. $("#show_grid_opt").prop("disabled", true);
  1348. $("#orientation_opt").removeProp("disabled");
  1349. $("#with_doc").prop("disabled", true);
  1350. $("#show_table_dim_opt").prop("disabled", true);
  1351. $("#all_tables_same_width").prop("disabled", true);
  1352. $("#paper_opt").prop("disabled", true);
  1353. $("#show_color_opt").prop("disabled", true);
  1354. } else if ($("#export_type").val() == 'pdf') {
  1355. $("#show_grid_opt").removeProp("disabled");
  1356. $("#orientation_opt").removeProp("disabled");
  1357. $("#with_doc").removeProp("disabled");
  1358. $("#show_table_dim_opt").removeProp("disabled");
  1359. $("#all_tables_same_width").removeProp("disabled");
  1360. $("#paper_opt").removeProp("disabled");
  1361. $("#show_color_opt").removeProp("disabled");
  1362. } else {
  1363. // nothing
  1364. }
  1365. });
  1366. if ($('#input_username')) {
  1367. if ($('#input_username').val() === '') {
  1368. $('#input_username').focus();
  1369. } else {
  1370. $('#input_password').focus();
  1371. }
  1372. }
  1373. });
  1374. /**
  1375. * Binds the CodeMirror to the text area used to inline edit a query.
  1376. */
  1377. function bindCodeMirrorToInlineEditor() {
  1378. var $inline_editor = $('#sql_query_edit');
  1379. if ($inline_editor.length > 0) {
  1380. if (typeof CodeMirror !== 'undefined') {
  1381. var height = $('#sql_query_edit').css('height');
  1382. codemirror_inline_editor = CodeMirror.fromTextArea($inline_editor[0], {
  1383. lineNumbers: true,
  1384. matchBrackets: true,
  1385. indentUnit: 4,
  1386. mode: "text/x-mysql",
  1387. lineWrapping: true
  1388. });
  1389. codemirror_inline_editor.getScrollerElement().style.height = height;
  1390. codemirror_inline_editor.refresh();
  1391. codemirror_inline_editor.focus();
  1392. $(codemirror_inline_editor.getWrapperElement()).bind(
  1393. 'keydown',
  1394. catchKeypressesFromSqlTextboxes
  1395. );
  1396. } else {
  1397. $inline_editor.focus().bind(
  1398. 'keydown',
  1399. catchKeypressesFromSqlTextboxes
  1400. );
  1401. }
  1402. }
  1403. }
  1404. function catchKeypressesFromSqlTextboxes(event) {
  1405. // ctrl-enter is 10 in chrome and ie, but 13 in ff
  1406. if (event.ctrlKey && (event.keyCode == 13 || event.keyCode == 10)) {
  1407. if ($('#sql_query_edit').length > 0) {
  1408. $("#sql_query_edit_save").trigger('click');
  1409. } else if ($('#sqlquery').length > 0) {
  1410. $("#button_submit_query").trigger('click');
  1411. }
  1412. }
  1413. }
  1414. /**
  1415. * Adds doc link to single highlighted SQL element
  1416. */
  1417. function PMA_doc_add($elm, params)
  1418. {
  1419. if (typeof mysql_doc_template == 'undefined') {
  1420. return;
  1421. }
  1422. var url = $.sprintf(
  1423. mysql_doc_template,
  1424. params[0]
  1425. );
  1426. if (params.length > 1) {
  1427. url += '#' + params[1];
  1428. }
  1429. var content = $elm.text();
  1430. $elm.text('');
  1431. $elm.append('<a target="mysql_doc" class="cm-sql-doc" href="' + url + '">' + content + '</a>');
  1432. }
  1433. /**
  1434. * Generates doc links for keywords inside highlighted SQL
  1435. */
  1436. function PMA_doc_keyword(idx, elm)
  1437. {
  1438. var $elm = $(elm);
  1439. /* Skip already processed ones */
  1440. if ($elm.find('a').length > 0) {
  1441. return;
  1442. }
  1443. var keyword = $elm.text().toUpperCase();
  1444. var $next = $elm.next('.cm-keyword');
  1445. if ($next) {
  1446. var next_keyword = $next.text().toUpperCase();
  1447. var full = keyword + ' ' + next_keyword;
  1448. var $next2 = $next.next('.cm-keyword');
  1449. if ($next2) {
  1450. var next2_keyword = $next2.text().toUpperCase();
  1451. var full2 = full + ' ' + next2_keyword;
  1452. if (full2 in mysql_doc_keyword) {
  1453. PMA_doc_add($elm, mysql_doc_keyword[full2]);
  1454. PMA_doc_add($next, mysql_doc_keyword[full2]);
  1455. PMA_doc_add($next2, mysql_doc_keyword[full2]);
  1456. return;
  1457. }
  1458. }
  1459. if (full in mysql_doc_keyword) {
  1460. PMA_doc_add($elm, mysql_doc_keyword[full]);
  1461. PMA_doc_add($next, mysql_doc_keyword[full]);
  1462. return;
  1463. }
  1464. }
  1465. if (keyword in mysql_doc_keyword) {
  1466. PMA_doc_add($elm, mysql_doc_keyword[keyword]);
  1467. }
  1468. }
  1469. /**
  1470. * Generates doc links for builtins inside highlighted SQL
  1471. */
  1472. function PMA_doc_builtin(idx, elm)
  1473. {
  1474. var $elm = $(elm);
  1475. var builtin = $elm.text().toUpperCase();
  1476. if (builtin in mysql_doc_builtin) {
  1477. PMA_doc_add($elm, mysql_doc_builtin[builtin]);
  1478. }
  1479. }
  1480. /**
  1481. * Higlights SQL using CodeMirror.
  1482. */
  1483. function PMA_highlightSQL(base)
  1484. {
  1485. var $elm = base.find('code.sql');
  1486. $elm.each(function () {
  1487. var $sql = $(this);
  1488. var $pre = $sql.find('pre');
  1489. /* We only care about visible elements to avoid double processing */
  1490. if ($pre.is(":visible")) {
  1491. var $highlight = $('<div class="sql-highlight cm-s-default"></div>');
  1492. $sql.append($highlight);
  1493. if (typeof CodeMirror != 'undefined') {
  1494. CodeMirror.runMode($sql.text(), 'text/x-mysql', $highlight[0]);
  1495. $pre.hide();
  1496. $highlight.find('.cm-keyword').each(PMA_doc_keyword);
  1497. $highlight.find('.cm-builtin').each(PMA_doc_builtin);
  1498. }
  1499. }
  1500. });
  1501. }
  1502. /**
  1503. * Show a message on the top of the page for an Ajax request
  1504. *
  1505. * Sample usage:
  1506. *
  1507. * 1) var $msg = PMA_ajaxShowMessage();
  1508. * This will show a message that reads "Loading...". Such a message will not
  1509. * disappear automatically and cannot be dismissed by the user. To remove this
  1510. * message either the PMA_ajaxRemoveMessage($msg) function must be called or
  1511. * another message must be show with PMA_ajaxShowMessage() function.
  1512. *
  1513. * 2) var $msg = PMA_ajaxShowMessage(PMA_messages.strProcessingRequest);
  1514. * This is a special case. The behaviour is same as above,
  1515. * just with a different message
  1516. *
  1517. * 3) var $msg = PMA_ajaxShowMessage('The operation was successful');
  1518. * This will show a message that will disappear automatically and it can also
  1519. * be dismissed by the user.
  1520. *
  1521. * 4) var $msg = PMA_ajaxShowMessage('Some error', false);
  1522. * This will show a message that will not disappear automatically, but it
  1523. * can be dismissed by the user after he has finished reading it.
  1524. *
  1525. * @param string message string containing the message to be shown.
  1526. * optional, defaults to 'Loading...'
  1527. * @param mixed timeout number of milliseconds for the message to be visible
  1528. * optional, defaults to 5000. If set to 'false', the
  1529. * notification will never disappear
  1530. * @return jQuery object jQuery Element that holds the message div
  1531. * this object can be passed to PMA_ajaxRemoveMessage()
  1532. * to remove the notification
  1533. */
  1534. function PMA_ajaxShowMessage(message, timeout)
  1535. {
  1536. /**
  1537. * @var self_closing Whether the notification will automatically disappear
  1538. */
  1539. var self_closing = true;
  1540. /**
  1541. * @var dismissable Whether the user will be able to remove
  1542. * the notification by clicking on it
  1543. */
  1544. var dismissable = true;
  1545. // Handle the case when a empty data.message is passed.
  1546. // We don't want the empty message
  1547. if (message === '') {
  1548. return true;
  1549. } else if (! message) {
  1550. // If the message is undefined, show the default
  1551. message = PMA_messages.strLoading;
  1552. dismissable = false;
  1553. self_closing = false;
  1554. } else if (message == PMA_messages.strProcessingRequest) {
  1555. // This is another case where the message should not disappear
  1556. dismissable = false;
  1557. self_closing = false;
  1558. }
  1559. // Figure out whether (or after how long) to remove the notification
  1560. if (timeout === undefined) {
  1561. timeout = 5000;
  1562. } else if (timeout === false) {
  1563. self_closing = false;
  1564. }
  1565. // Create a parent element for the AJAX messages, if necessary
  1566. if ($('#loading_parent').length === 0) {
  1567. $('<div id="loading_parent"></div>')
  1568. .prependTo("body");
  1569. }
  1570. // Update message count to create distinct message elements every time
  1571. ajax_message_count++;
  1572. // Remove all old messages, if any
  1573. $("span.ajax_notification[id^=ajax_message_num]").remove();
  1574. /**
  1575. * @var $retval a jQuery object containing the reference
  1576. * to the created AJAX message
  1577. */
  1578. var $retval = $(
  1579. '<span class="ajax_notification" id="ajax_message_num_' +
  1580. ajax_message_count +
  1581. '"></span>'
  1582. )
  1583. .hide()
  1584. .appendTo("#loading_parent")
  1585. .html(message)
  1586. .show();
  1587. // If the notification is self-closing we should create a callback to remove it
  1588. if (self_closing) {
  1589. $retval
  1590. .delay(timeout)
  1591. .fadeOut('medium', function () {
  1592. if ($(this).is(':data(tooltip)')) {
  1593. $(this).tooltip('destroy');
  1594. }
  1595. // Remove the notification
  1596. $(this).remove();
  1597. });
  1598. }
  1599. // If the notification is dismissable we need to add the relevant class to it
  1600. // and add a tooltip so that the users know that it can be removed
  1601. if (dismissable) {
  1602. $retval.addClass('dismissable').css('cursor', 'pointer');
  1603. /**
  1604. * Add a tooltip to the notification to let the user know that (s)he
  1605. * can dismiss the ajax notification by clicking on it.
  1606. */
  1607. PMA_tooltip(
  1608. $retval,
  1609. 'span',
  1610. PMA_messages.strDismiss
  1611. );
  1612. }
  1613. PMA_highlightSQL($retval);
  1614. return $retval;
  1615. }
  1616. /**
  1617. * Removes the message shown for an Ajax operation when it's completed
  1618. *
  1619. * @param jQuery object jQuery Element that holds the notification
  1620. *
  1621. * @return nothing
  1622. */
  1623. function PMA_ajaxRemoveMessage($this_msgbox)
  1624. {
  1625. if ($this_msgbox !== undefined && $this_msgbox instanceof jQuery) {
  1626. $this_msgbox
  1627. .stop(true, true)
  1628. .fadeOut('medium');
  1629. if ($this_msgbox.is(':data(tooltip)')) {
  1630. $this_msgbox.tooltip('destroy');
  1631. } else {
  1632. $this_msgbox.remove();
  1633. }
  1634. }
  1635. }
  1636. // This event only need to be fired once after the initial page load
  1637. $(function () {
  1638. /**
  1639. * Allows the user to dismiss a notification
  1640. * created with PMA_ajaxShowMessage()
  1641. */
  1642. $('span.ajax_notification.dismissable').live('click', function () {
  1643. PMA_ajaxRemoveMessage($(this));
  1644. });
  1645. /**
  1646. * The below two functions hide the "Dismiss notification" tooltip when a user
  1647. * is hovering a link or button that is inside an ajax message
  1648. */
  1649. $('span.ajax_notification a, span.ajax_notification button, span.ajax_notification input')
  1650. .live('mouseover', function () {
  1651. if ($(this).parents('span.ajax_notification').is(':data(tooltip)')) {
  1652. $(this).parents('span.ajax_notification').tooltip('disable');
  1653. }
  1654. })
  1655. .live('mouseout', function () {
  1656. if ($(this).parents('span.ajax_notification').is(':data(tooltip)')) {
  1657. $(this).parents('span.ajax_notification').tooltip('enable');
  1658. }
  1659. });
  1660. });
  1661. /**
  1662. * Hides/shows the "Open in ENUM/SET editor" message, depending on the data type of the column currently selected
  1663. */
  1664. function PMA_showNoticeForEnum(selectElement)
  1665. {
  1666. var enum_notice_id = selectElement.attr("id").split("_")[1];
  1667. enum_notice_id += "_" + (parseInt(selectElement.attr("id").split("_")[2], 10) + 1);
  1668. var selectedType = selectElement.val();
  1669. if (selectedType == "ENUM" || selectedType == "SET") {
  1670. $("p#enum_notice_" + enum_notice_id).show();
  1671. } else {
  1672. $("p#enum_notice_" + enum_notice_id).hide();
  1673. }
  1674. }
  1675. /*
  1676. * Creates a Profiling Chart with jqplot. Used in sql.js
  1677. * and in server_status_monitor.js
  1678. */
  1679. function PMA_createProfilingChartJqplot(target, data)
  1680. {
  1681. return $.jqplot(target, [data],
  1682. {
  1683. seriesDefaults: {
  1684. renderer: $.jqplot.PieRenderer,
  1685. rendererOptions: {
  1686. showDataLabels: true
  1687. }
  1688. },
  1689. highlighter: {
  1690. show: true,
  1691. tooltipLocation: 'se',
  1692. sizeAdjust: 0,
  1693. tooltipAxes: 'pieref',
  1694. useAxesFormatters: false,
  1695. formatString: '%s, %.9Ps'
  1696. },
  1697. legend: {
  1698. show: true,
  1699. location: 'e',
  1700. rendererOptions: {numberColumns: 2}
  1701. },
  1702. // from http://tango.freedesktop.org/Tango_Icon_Theme_Guidelines#Color_Palette
  1703. seriesColors: [
  1704. '#fce94f',
  1705. '#fcaf3e',
  1706. '#e9b96e',
  1707. '#8ae234',
  1708. '#729fcf',
  1709. '#ad7fa8',
  1710. '#ef2929',
  1711. '#eeeeec',
  1712. '#888a85',
  1713. '#c4a000',
  1714. '#ce5c00',
  1715. '#8f5902',
  1716. '#4e9a06',
  1717. '#204a87',
  1718. '#5c3566',
  1719. '#a40000',
  1720. '#babdb6',
  1721. '#2e3436'
  1722. ]
  1723. }
  1724. );
  1725. }
  1726. /**
  1727. * Formats a profiling duration nicely (in us and ms time).
  1728. * Used in server_status_monitor.js
  1729. *
  1730. * @param integer Number to be formatted, should be in the range of microsecond to second
  1731. * @param integer Accuracy, how many numbers right to the comma should be
  1732. * @return string The formatted number
  1733. */
  1734. function PMA_prettyProfilingNum(num, acc)
  1735. {
  1736. if (!acc) {
  1737. acc = 2;
  1738. }
  1739. acc = Math.pow(10, acc);
  1740. if (num * 1000 < 0.1) {
  1741. num = Math.round(acc * (num * 1000 * 1000)) / acc + 'µ';
  1742. } else if (num < 0.1) {
  1743. num = Math.round(acc * (num * 1000)) / acc + 'm';
  1744. } else {
  1745. num = Math.round(acc * num) / acc;
  1746. }
  1747. return num + 's';
  1748. }
  1749. /**
  1750. * Formats a SQL Query nicely with newlines and indentation. Depends on Codemirror and MySQL Mode!
  1751. *
  1752. * @param string Query to be formatted
  1753. * @return string The formatted query
  1754. */
  1755. function PMA_SQLPrettyPrint(string)
  1756. {
  1757. if (typeof CodeMirror == 'undefined') {
  1758. return string;
  1759. }
  1760. var mode = CodeMirror.getMode({}, "text/x-mysql");
  1761. var stream = new CodeMirror.StringStream(string);
  1762. var state = mode.startState();
  1763. var token, tokens = [];
  1764. var output = '';
  1765. var tabs = function (cnt) {
  1766. var ret = '';
  1767. for (var i = 0; i < 4 * cnt; i++) {
  1768. ret += " ";
  1769. }
  1770. return ret;
  1771. };
  1772. // "root-level" statements
  1773. var statements = {
  1774. 'select': ['select', 'from', 'on', 'where', 'having', 'limit', 'order by', 'group by'],
  1775. 'update': ['update', 'set', 'where'],
  1776. 'insert into': ['insert into', 'values']
  1777. };
  1778. // don't put spaces before these tokens
  1779. var spaceExceptionsBefore = {';': true, ',': true, '.': true, '(': true};
  1780. // don't put spaces after these tokens
  1781. var spaceExceptionsAfter = {'.': true};
  1782. // Populate tokens array
  1783. var str = '';
  1784. while (! stream.eol()) {
  1785. stream.start = stream.pos;
  1786. token = mode.token(stream, state);
  1787. if (token !== null) {
  1788. tokens.push([token, stream.current().toLowerCase()]);
  1789. }
  1790. }
  1791. var currentStatement = tokens[0][1];
  1792. if (! statements[currentStatement]) {
  1793. return string;
  1794. }
  1795. // Holds all currently opened code blocks (statement, function or generic)
  1796. var blockStack = [];
  1797. // Holds the type of block from last iteration (the current is in blockStack[0])
  1798. var previousBlock;
  1799. // If a new code block is found, newBlock contains its type for one iteration and vice versa for endBlock
  1800. var newBlock, endBlock;
  1801. // How much to indent in the current line
  1802. var indentLevel = 0;
  1803. // Holds the "root-level" statements
  1804. var statementPart, lastStatementPart = statements[currentStatement][0];
  1805. blockStack.unshift('statement');
  1806. // Iterate through every token and format accordingly
  1807. for (var i = 0; i < tokens.length; i++) {
  1808. previousBlock = blockStack[0];
  1809. // New block => push to stack
  1810. if (tokens[i][1] == '(') {
  1811. if (i < tokens.length - 1 && tokens[i + 1][0] == 'statement-verb') {
  1812. blockStack.unshift(newBlock = 'statement');
  1813. } else if (i > 0 && tokens[i - 1][0] == 'builtin') {
  1814. blockStack.unshift(newBlock = 'function');
  1815. } else {
  1816. blockStack.unshift(newBlock = 'generic');
  1817. }
  1818. } else {
  1819. newBlock = null;
  1820. }
  1821. // Block end => pop from stack
  1822. if (tokens[i][1] == ')') {
  1823. endBlock = blockStack[0];
  1824. blockStack.shift();
  1825. } else {
  1826. endBlock = null;
  1827. }
  1828. // A subquery is starting
  1829. if (i > 0 && newBlock == 'statement') {
  1830. indentLevel++;
  1831. output += "\n" + tabs(indentLevel) + tokens[i][1] + ' ' + tokens[i + 1][1].toUpperCase() + "\n" + tabs(indentLevel + 1);
  1832. currentStatement = tokens[i + 1][1];
  1833. i++;
  1834. continue;
  1835. }
  1836. // A subquery is ending
  1837. if (endBlock == 'statement' && indentLevel > 0) {
  1838. output += "\n" + tabs(indentLevel);
  1839. indentLevel--;
  1840. }
  1841. // One less indentation for statement parts (from, where, order by, etc.) and a newline
  1842. statementPart = statements[currentStatement].indexOf(tokens[i][1]);
  1843. if (statementPart != -1) {
  1844. if (i > 0) {
  1845. output += "\n";
  1846. }
  1847. output += tabs(indentLevel) + tokens[i][1].toUpperCase();
  1848. output += "\n" + tabs(indentLevel + 1);
  1849. lastStatementPart = tokens[i][1];
  1850. }
  1851. // Normal indentatin and spaces for everything else
  1852. else {
  1853. if (! spaceExceptionsBefore[tokens[i][1]] &&
  1854. ! (i > 0 && spaceExceptionsAfter[tokens[i - 1][1]]) &&
  1855. output.charAt(output.length - 1) != ' ') {
  1856. output += " ";
  1857. }
  1858. if (tokens[i][0] == 'keyword') {
  1859. output += tokens[i][1].toUpperCase();
  1860. } else {
  1861. output += tokens[i][1];
  1862. }
  1863. }
  1864. // split columns in select and 'update set' clauses, but only inside statements blocks
  1865. if ((lastStatementPart == 'select' || lastStatementPart == 'where' || lastStatementPart == 'set') &&
  1866. tokens[i][1] == ',' && blockStack[0] == 'statement') {
  1867. output += "\n" + tabs(indentLevel + 1);
  1868. }
  1869. // split conditions in where clauses, but only inside statements blocks
  1870. if (lastStatementPart == 'where' &&
  1871. (tokens[i][1] == 'and' || tokens[i][1] == 'or' || tokens[i][1] == 'xor')) {
  1872. if (blockStack[0] == 'statement') {
  1873. output += "\n" + tabs(indentLevel + 1);
  1874. }
  1875. // Todo: Also split and or blocks in newlines & identation++
  1876. //if (blockStack[0] == 'generic')
  1877. // output += ...
  1878. }
  1879. }
  1880. return output;
  1881. }
  1882. /**
  1883. * jQuery function that uses jQueryUI's dialogs to confirm with user. Does not
  1884. * return a jQuery object yet and hence cannot be chained
  1885. *
  1886. * @param string question
  1887. * @param string url URL to be passed to the callbackFn to make
  1888. * an Ajax call to
  1889. * @param function callbackFn callback to execute after user clicks on OK
  1890. */
  1891. jQuery.fn.PMA_confirm = function (question, url, callbackFn) {
  1892. var confirmState = PMA_commonParams.get('confirm');
  1893. // when the Confirm directive is set to false in config.inc.php
  1894. // and not changed in user prefs, confirmState is ""
  1895. // when it's unticked in user prefs, confirmState is 1
  1896. if (confirmState === "" || confirmState === "1") {
  1897. // user does not want to confirm
  1898. if ($.isFunction(callbackFn)) {
  1899. callbackFn.call(this, url);
  1900. return true;
  1901. }
  1902. }
  1903. if (PMA_messages.strDoYouReally === '') {
  1904. return true;
  1905. }
  1906. /**
  1907. * @var button_options Object that stores the options passed to jQueryUI
  1908. * dialog
  1909. */
  1910. var button_options = {};
  1911. button_options[PMA_messages.strOK] = function () {
  1912. $(this).dialog("close");
  1913. if ($.isFunction(callbackFn)) {
  1914. callbackFn.call(this, url);
  1915. }
  1916. };
  1917. button_options[PMA_messages.strCancel] = function () {
  1918. $(this).dialog("close");
  1919. };
  1920. $('<div/>', {'id': 'confirm_dialog'})
  1921. .prepend(question)
  1922. .dialog({
  1923. buttons: button_options,
  1924. close: function () {
  1925. $(this).remove();
  1926. },
  1927. modal: true
  1928. });
  1929. };
  1930. /**
  1931. * jQuery function to sort a table's body after a new row has been appended to it.
  1932. * Also fixes the even/odd classes of the table rows at the end.
  1933. *
  1934. * @param string text_selector string to select the sortKey's text
  1935. *
  1936. * @return jQuery Object for chaining purposes
  1937. */
  1938. jQuery.fn.PMA_sort_table = function (text_selector) {
  1939. return this.each(function () {
  1940. /**
  1941. * @var table_body Object referring to the table's <tbody> element
  1942. */
  1943. var table_body = $(this);
  1944. /**
  1945. * @var rows Object referring to the collection of rows in {@link table_body}
  1946. */
  1947. var rows = $(this).find('tr').get();
  1948. //get the text of the field that we will sort by
  1949. $.each(rows, function (index, row) {
  1950. row.sortKey = $.trim($(row).find(text_selector).text().toLowerCase());
  1951. });
  1952. //get the sorted order
  1953. rows.sort(function (a, b) {
  1954. if (a.sortKey < b.sortKey) {
  1955. return -1;
  1956. }
  1957. if (a.sortKey > b.sortKey) {
  1958. return 1;
  1959. }
  1960. return 0;
  1961. });
  1962. //pull out each row from the table and then append it according to it's order
  1963. $.each(rows, function (index, row) {
  1964. $(table_body).append(row);
  1965. row.sortKey = null;
  1966. });
  1967. //Re-check the classes of each row
  1968. $(this).find('tr:odd')
  1969. .removeClass('even').addClass('odd')
  1970. .end()
  1971. .find('tr:even')
  1972. .removeClass('odd').addClass('even');
  1973. });
  1974. };
  1975. /**
  1976. * Unbind all event handlers before tearing down a page
  1977. */
  1978. AJAX.registerTeardown('functions.js', function () {
  1979. $("#create_table_form_minimal.ajax").die('submit');
  1980. $("form.create_table_form.ajax").die('submit');
  1981. $("form.create_table_form.ajax input[name=submit_num_fields]").die('click');
  1982. $("form.create_table_form.ajax input").die('keyup');
  1983. });
  1984. /**
  1985. * jQuery coding for 'Create Table'. Used on db_operations.php,
  1986. * db_structure.php and db_tracking.php (i.e., wherever
  1987. * libraries/display_create_table.lib.php is used)
  1988. *
  1989. * Attach Ajax Event handlers for Create Table
  1990. */
  1991. AJAX.registerOnload('functions.js', function () {
  1992. /**
  1993. * Attach event handler for submission of create table form (save)
  1994. */
  1995. $("form.create_table_form.ajax").live('submit', function (event) {
  1996. event.preventDefault();
  1997. /**
  1998. * @var the_form object referring to the create table form
  1999. */
  2000. var $form = $(this);
  2001. /*
  2002. * First validate the form; if there is a problem, avoid submitting it
  2003. *
  2004. * checkTableEditForm() needs a pure element and not a jQuery object,
  2005. * this is why we pass $form[0] as a parameter (the jQuery object
  2006. * is actually an array of DOM elements)
  2007. */
  2008. if (checkTableEditForm($form[0], $form.find('input[name=orig_num_fields]').val())) {
  2009. PMA_ajaxShowMessage(PMA_messages.strProcessingRequest);
  2010. PMA_prepareForAjaxRequest($form);
  2011. //User wants to submit the form
  2012. $.post($form.attr('action'), $form.serialize() + "&do_save_data=1", function (data) {
  2013. if (data.success === true) {
  2014. $('#properties_message')
  2015. .removeClass('error')
  2016. .html('');
  2017. PMA_ajaxShowMessage(data.message);
  2018. // Only if the create table dialog (distinct panel) exists
  2019. if ($("#create_table_dialog").length > 0) {
  2020. $("#create_table_dialog").dialog("close").remove();
  2021. }
  2022. $('#tableslistcontainer').before(data.formatted_sql);
  2023. /**
  2024. * @var tables_table Object referring to the <tbody> element that holds the list of tables
  2025. */
  2026. var tables_table = $("#tablesForm").find("tbody").not("#tbl_summary_row");
  2027. // this is the first table created in this db
  2028. if (tables_table.length === 0) {
  2029. PMA_commonActions.refreshMain(
  2030. PMA_commonParams.get('opendb_url')
  2031. );
  2032. } else {
  2033. /**
  2034. * @var curr_last_row Object referring to the last <tr> element in {@link tables_table}
  2035. */
  2036. var curr_last_row = $(tables_table).find('tr:last');
  2037. /**
  2038. * @var curr_last_row_index_string String containing the index of {@link curr_last_row}
  2039. */
  2040. var curr_last_row_index_string = $(curr_last_row).find('input:checkbox').attr('id').match(/\d+/)[0];
  2041. /**
  2042. * @var curr_last_row_index Index of {@link curr_last_row}
  2043. */
  2044. var curr_last_row_index = parseFloat(curr_last_row_index_string);
  2045. /**
  2046. * @var new_last_row_index Index of the new row to be appended to {@link tables_table}
  2047. */
  2048. var new_last_row_index = curr_last_row_index + 1;
  2049. /**
  2050. * @var new_last_row_id String containing the id of the row to be appended to {@link tables_table}
  2051. */
  2052. var new_last_row_id = 'checkbox_tbl_' + new_last_row_index;
  2053. data.new_table_string = data.new_table_string.replace(/checkbox_tbl_/, new_last_row_id);
  2054. //append to table
  2055. $(data.new_table_string)
  2056. .appendTo(tables_table);
  2057. //Sort the table
  2058. $(tables_table).PMA_sort_table('th');
  2059. // Adjust summary row
  2060. PMA_adjustTotals();
  2061. }
  2062. //Refresh navigation as a new table has been added
  2063. PMA_reloadNavigation();
  2064. } else {
  2065. PMA_ajaxShowMessage(
  2066. '<div class="error">' + data.error + '</div>',
  2067. false
  2068. );
  2069. }
  2070. }); // end $.post()
  2071. } // end if (checkTableEditForm() )
  2072. }); // end create table form (save)
  2073. /**
  2074. * Attach event handler for create table form (add fields)
  2075. */
  2076. $("form.create_table_form.ajax input[name=submit_num_fields]").live('click', function (event) {
  2077. event.preventDefault();
  2078. /**
  2079. * @var the_form object referring to the create table form
  2080. */
  2081. var $form = $(this).closest('form');
  2082. var $msgbox = PMA_ajaxShowMessage(PMA_messages.strProcessingRequest);
  2083. PMA_prepareForAjaxRequest($form);
  2084. //User wants to add more fields to the table
  2085. $.post($form.attr('action'), $form.serialize() + "&submit_num_fields=1", function (data) {
  2086. if (data.success) {
  2087. $("#page_content").html(data.message);
  2088. PMA_highlightSQL($('#page_content'));
  2089. PMA_verifyColumnsProperties();
  2090. PMA_ajaxRemoveMessage($msgbox);
  2091. } else {
  2092. PMA_ajaxShowMessage(data.error);
  2093. }
  2094. }); //end $.post()
  2095. }); // end create table form (add fields)
  2096. $("form.create_table_form.ajax input").live('keydown', function (event) {
  2097. if (event.keyCode == 13) {
  2098. event.preventDefault();
  2099. event.stopImmediatePropagation();
  2100. $(this)
  2101. .closest('form')
  2102. .append('<input type="hidden" name="do_save_data" value="1" />')
  2103. .submit();
  2104. }
  2105. });
  2106. });
  2107. /**
  2108. * Unbind all event handlers before tearing down a page
  2109. */
  2110. AJAX.registerTeardown('functions.js', function () {
  2111. $("#copyTable.ajax").die('submit');
  2112. $("#moveTableForm").die('submit');
  2113. $("#tableOptionsForm").die('submit');
  2114. $("#tbl_maintenance li a.maintain_action.ajax").die('click');
  2115. });
  2116. /**
  2117. * jQuery coding for 'Table operations'. Used on tbl_operations.php
  2118. * Attach Ajax Event handlers for Table operations
  2119. */
  2120. AJAX.registerOnload('functions.js', function () {
  2121. /**
  2122. *Ajax action for submitting the "Copy table"
  2123. **/
  2124. $("#copyTable.ajax").live('submit', function (event) {
  2125. event.preventDefault();
  2126. var $form = $(this);
  2127. PMA_prepareForAjaxRequest($form);
  2128. $.post($form.attr('action'), $form.serialize() + "&submit_copy=Go", function (data) {
  2129. if (data.success === true) {
  2130. if ($form.find("input[name='switch_to_new']").prop('checked')) {
  2131. PMA_commonParams.set(
  2132. 'db',
  2133. data.db
  2134. );
  2135. PMA_commonParams.set(
  2136. 'table',
  2137. $form.find("input[name='new_name']").val()
  2138. );
  2139. PMA_commonActions.refreshMain(false, function () {
  2140. PMA_ajaxShowMessage(data.message);
  2141. });
  2142. } else {
  2143. PMA_ajaxShowMessage(data.message);
  2144. }
  2145. // Refresh navigation when the table is copied
  2146. PMA_reloadNavigation();
  2147. } else {
  2148. PMA_ajaxShowMessage(data.error, false);
  2149. }
  2150. }); // end $.post()
  2151. });//end of copyTable ajax submit
  2152. /**
  2153. *Ajax action for submitting the "Move table"
  2154. */
  2155. $("#moveTableForm").live('submit', function (event) {
  2156. event.preventDefault();
  2157. var $form = $(this);
  2158. var db = $form.find('select[name=target_db]').val();
  2159. var tbl = $form.find('input[name=new_name]').val();
  2160. PMA_prepareForAjaxRequest($form);
  2161. $.post($form.attr('action'), $form.serialize() + "&submit_move=1", function (data) {
  2162. if (data.success === true) {
  2163. PMA_commonParams.set('db', db);
  2164. PMA_commonParams.set('table', tbl);
  2165. PMA_commonActions.refreshMain(false, function () {
  2166. PMA_ajaxShowMessage(data.message);
  2167. });
  2168. // Refresh navigation when the table is copied
  2169. PMA_reloadNavigation();
  2170. } else {
  2171. PMA_ajaxShowMessage(data.error, false);
  2172. }
  2173. }); // end $.post()
  2174. });
  2175. /**
  2176. * Ajax action for submitting the "Table options"
  2177. */
  2178. $("#tableOptionsForm").live('submit', function (event) {
  2179. event.preventDefault();
  2180. event.stopPropagation();
  2181. var $form = $(this);
  2182. var $tblNameField = $form.find('input[name=new_name]');
  2183. if ($tblNameField.val() !== $tblNameField[0].defaultValue) {
  2184. // reload page and navigation if the table has been renamed
  2185. PMA_prepareForAjaxRequest($form);
  2186. var tbl = $tblNameField.val();
  2187. $.post($form.attr('action'), $form.serialize(), function (data) {
  2188. if (data.success === true) {
  2189. PMA_commonParams.set('table', tbl);
  2190. PMA_commonActions.refreshMain(false, function () {
  2191. $('#page_content').html(data.message);
  2192. PMA_highlightSQL($('#page_content'));
  2193. });
  2194. } else {
  2195. PMA_ajaxShowMessage(data.error, false);
  2196. }
  2197. }); // end $.post()
  2198. } else {
  2199. $form.removeClass('ajax').submit().addClass('ajax');
  2200. }
  2201. });
  2202. /**
  2203. *Ajax events for actions in the "Table maintenance"
  2204. **/
  2205. $("#tbl_maintenance li a.maintain_action.ajax").live('click', function (event) {
  2206. event.preventDefault();
  2207. if ($("#sqlqueryresults").length !== 0) {
  2208. $("#sqlqueryresults").remove();
  2209. }
  2210. if ($("#result_query").length !== 0) {
  2211. $("#result_query").remove();
  2212. }
  2213. //variables which stores the common attributes
  2214. $.post($(this).attr('href'), { ajax_request: 1 }, function (data) {
  2215. function scrollToTop() {
  2216. $('html, body').animate({ scrollTop: 0 });
  2217. }
  2218. if (data.success === true && data.sql_query !== undefined) {
  2219. PMA_ajaxShowMessage(data.message);
  2220. $("<div id='sqlqueryresults' class='ajax'></div>").prependTo("#page_content");
  2221. $("#sqlqueryresults").html(data.sql_query);
  2222. PMA_highlightSQL($('#page_content'));
  2223. scrollToTop();
  2224. } else if (data.success === true) {
  2225. var $temp_div = $("<div id='temp_div'></div>");
  2226. $temp_div.html(data.message);
  2227. var $success = $temp_div.find("#result_query .success");
  2228. PMA_ajaxShowMessage($success);
  2229. $("<div id='sqlqueryresults' class='ajax'></div>").prependTo("#page_content");
  2230. $("#sqlqueryresults").html(data.message);
  2231. PMA_highlightSQL($('#page_content'));
  2232. PMA_init_slider();
  2233. $("#sqlqueryresults").children("fieldset,br").remove();
  2234. scrollToTop();
  2235. } else {
  2236. var $temp_div = $("<div id='temp_div'></div>");
  2237. $temp_div.html(data.error);
  2238. var $error = $temp_div.find("code").addClass("error");
  2239. PMA_ajaxShowMessage($error, false);
  2240. }
  2241. }); // end $.post()
  2242. });//end of table maintanance ajax click
  2243. }); //end $(document).ready for 'Table operations'
  2244. /**
  2245. * Unbind all event handlers before tearing down a page
  2246. */
  2247. AJAX.registerTeardown('functions.js', function () {
  2248. $("#drop_db_anchor.ajax").die('click');
  2249. });
  2250. /**
  2251. * Attach Ajax event handlers for Drop Database. Moved here from db_structure.js
  2252. * as it was also required on db_create.php
  2253. */
  2254. AJAX.registerOnload('functions.js', function () {
  2255. $("#drop_db_anchor.ajax").live('click', function (event) {
  2256. event.preventDefault();
  2257. /**
  2258. * @var question String containing the question to be asked for confirmation
  2259. */
  2260. var question = PMA_messages.strDropDatabaseStrongWarning + ' ';
  2261. question += $.sprintf(
  2262. PMA_messages.strDoYouReally,
  2263. 'DROP DATABASE ' + escapeHtml(PMA_commonParams.get('db'))
  2264. );
  2265. $(this).PMA_confirm(question, $(this).attr('href'), function (url) {
  2266. PMA_ajaxShowMessage(PMA_messages.strProcessingRequest);
  2267. $.get(url, {'is_js_confirmed': '1', 'ajax_request': true}, function (data) {
  2268. if (data.success) {
  2269. //Database deleted successfully, refresh both the frames
  2270. PMA_reloadNavigation();
  2271. PMA_commonParams.set('db', '');
  2272. PMA_commonActions.refreshMain(
  2273. 'server_databases.php',
  2274. function () {
  2275. PMA_ajaxShowMessage(data.message);
  2276. }
  2277. );
  2278. } else {
  2279. PMA_ajaxShowMessage(data.error, false);
  2280. }
  2281. });
  2282. });
  2283. });
  2284. }); // end of $() for Drop Database
  2285. /**
  2286. * Validates the password field in a form
  2287. *
  2288. * @see PMA_messages.strPasswordEmpty
  2289. * @see PMA_messages.strPasswordNotSame
  2290. * @param object $the_form The form to be validated
  2291. * @return bool
  2292. */
  2293. function PMA_checkPassword($the_form)
  2294. {
  2295. // Did the user select 'no password'?
  2296. if ($the_form.find('#nopass_1').is(':checked')) {
  2297. return true;
  2298. } else {
  2299. var $pred = $the_form.find('#select_pred_password');
  2300. if ($pred.length && ($pred.val() == 'none' || $pred.val() == 'keep')) {
  2301. return true;
  2302. }
  2303. }
  2304. var $password = $the_form.find('input[name=pma_pw]');
  2305. var $password_repeat = $the_form.find('input[name=pma_pw2]');
  2306. var alert_msg = false;
  2307. if ($password.val() === '') {
  2308. alert_msg = PMA_messages.strPasswordEmpty;
  2309. } else if ($password.val() != $password_repeat.val()) {
  2310. alert_msg = PMA_messages.strPasswordNotSame;
  2311. }
  2312. if (alert_msg) {
  2313. alert(alert_msg);
  2314. $password.val('');
  2315. $password_repeat.val('');
  2316. $password.focus();
  2317. return false;
  2318. }
  2319. return true;
  2320. }
  2321. /**
  2322. * Unbind all event handlers before tearing down a page
  2323. */
  2324. AJAX.registerTeardown('functions.js', function () {
  2325. $('#change_password_anchor.ajax').die('click');
  2326. });
  2327. /**
  2328. * Attach Ajax event handlers for 'Change Password' on index.php
  2329. */
  2330. AJAX.registerOnload('functions.js', function () {
  2331. /**
  2332. * Attach Ajax event handler on the change password anchor
  2333. */
  2334. $('#change_password_anchor.ajax').live('click', function (event) {
  2335. event.preventDefault();
  2336. var $msgbox = PMA_ajaxShowMessage();
  2337. /**
  2338. * @var button_options Object containing options to be passed to jQueryUI's dialog
  2339. */
  2340. var button_options = {};
  2341. button_options[PMA_messages.strGo] = function () {
  2342. event.preventDefault();
  2343. /**
  2344. * @var $the_form Object referring to the change password form
  2345. */
  2346. var $the_form = $("#change_password_form");
  2347. if (! PMA_checkPassword($the_form)) {
  2348. return false;
  2349. }
  2350. /**
  2351. * @var this_value String containing the value of the submit button.
  2352. * Need to append this for the change password form on Server Privileges
  2353. * page to work
  2354. */
  2355. var this_value = $(this).val();
  2356. var $msgbox = PMA_ajaxShowMessage(PMA_messages.strProcessingRequest);
  2357. $the_form.append('<input type="hidden" name="ajax_request" value="true" />');
  2358. $.post($the_form.attr('action'), $the_form.serialize() + '&change_pw=' + this_value, function (data) {
  2359. if (data.success === true) {
  2360. $("#page_content").prepend(data.message);
  2361. PMA_highlightSQL($('#page_content'));
  2362. $("#change_password_dialog").hide().remove();
  2363. $("#edit_user_dialog").dialog("close").remove();
  2364. PMA_ajaxRemoveMessage($msgbox);
  2365. }
  2366. else {
  2367. PMA_ajaxShowMessage(data.error, false);
  2368. }
  2369. }); // end $.post()
  2370. };
  2371. button_options[PMA_messages.strCancel] = function () {
  2372. $(this).dialog('close');
  2373. };
  2374. $.get($(this).attr('href'), {'ajax_request': true}, function (data) {
  2375. if (data.success) {
  2376. $('<div id="change_password_dialog"></div>')
  2377. .dialog({
  2378. title: PMA_messages.strChangePassword,
  2379. width: 600,
  2380. close: function (ev, ui) {
  2381. $(this).remove();
  2382. },
  2383. buttons : button_options,
  2384. modal: true
  2385. })
  2386. .append(data.message);
  2387. // for this dialog, we remove the fieldset wrapping due to double headings
  2388. $("fieldset#fieldset_change_password")
  2389. .find("legend").remove().end()
  2390. .find("table.noclick").unwrap().addClass("some-margin")
  2391. .find("input#text_pma_pw").focus();
  2392. displayPasswordGenerateButton();
  2393. $('#fieldset_change_password_footer').hide();
  2394. PMA_ajaxRemoveMessage($msgbox);
  2395. $('#change_password_form').bind('submit', function (e) {
  2396. e.preventDefault();
  2397. $(this)
  2398. .closest('.ui-dialog')
  2399. .find('.ui-dialog-buttonpane .ui-button')
  2400. .first()
  2401. .click();
  2402. });
  2403. } else {
  2404. PMA_ajaxShowMessage(data.error, false);
  2405. }
  2406. }); // end $.get()
  2407. }); // end handler for change password anchor
  2408. }); // end $() for Change Password
  2409. /**
  2410. * Unbind all event handlers before tearing down a page
  2411. */
  2412. AJAX.registerTeardown('functions.js', function () {
  2413. $("select.column_type").die('change');
  2414. $("select.default_type").die('change');
  2415. $('input.allow_null').die('change');
  2416. });
  2417. /**
  2418. * Toggle the hiding/showing of the "Open in ENUM/SET editor" message when
  2419. * the page loads and when the selected data type changes
  2420. */
  2421. AJAX.registerOnload('functions.js', function () {
  2422. // is called here for normal page loads and also when opening
  2423. // the Create table dialog
  2424. PMA_verifyColumnsProperties();
  2425. //
  2426. // needs live() to work also in the Create Table dialog
  2427. $("select.column_type").live('change', function () {
  2428. PMA_showNoticeForEnum($(this));
  2429. });
  2430. $("select.default_type").live('change', function () {
  2431. PMA_hideShowDefaultValue($(this));
  2432. });
  2433. $('input.allow_null').live('change', function () {
  2434. PMA_validateDefaultValue($(this));
  2435. });
  2436. });
  2437. function PMA_verifyColumnsProperties()
  2438. {
  2439. $("select.column_type").each(function () {
  2440. PMA_showNoticeForEnum($(this));
  2441. });
  2442. $("select.default_type").each(function () {
  2443. PMA_hideShowDefaultValue($(this));
  2444. });
  2445. }
  2446. /**
  2447. * Hides/shows the default value input field, depending on the default type
  2448. * Ticks the NULL checkbox if NULL is chosen as default value.
  2449. */
  2450. function PMA_hideShowDefaultValue($default_type)
  2451. {
  2452. if ($default_type.val() == 'USER_DEFINED') {
  2453. $default_type.siblings('.default_value').show().focus();
  2454. } else {
  2455. $default_type.siblings('.default_value').hide();
  2456. if ($default_type.val() == 'NULL') {
  2457. var $null_checkbox = $default_type.closest('tr').find('.allow_null');
  2458. $null_checkbox.prop('checked', true);
  2459. }
  2460. }
  2461. }
  2462. /**
  2463. * If the column does not allow NULL values, makes sure that default is not NULL
  2464. */
  2465. function PMA_validateDefaultValue($null_checkbox)
  2466. {
  2467. if (! $null_checkbox.prop('checked')) {
  2468. var $default = $null_checkbox.closest('tr').find('.default_type');
  2469. if ($default.val() == 'NULL') {
  2470. $default.val('NONE');
  2471. }
  2472. }
  2473. }
  2474. /**
  2475. * Unbind all event handlers before tearing down a page
  2476. */
  2477. AJAX.registerTeardown('functions.js', function () {
  2478. $("a.open_enum_editor").die('click');
  2479. $("input.add_value").die('click');
  2480. $("#enum_editor td.drop").die('click');
  2481. });
  2482. /**
  2483. * @var $enum_editor_dialog An object that points to the jQuery
  2484. * dialog of the ENUM/SET editor
  2485. */
  2486. var $enum_editor_dialog = null;
  2487. /**
  2488. * Opens the ENUM/SET editor and controls its functions
  2489. */
  2490. AJAX.registerOnload('functions.js', function () {
  2491. $("a.open_enum_editor").live('click', function () {
  2492. // Get the name of the column that is being edited
  2493. var colname = $(this).closest('tr').find('input:first').val();
  2494. var title;
  2495. var i;
  2496. // And use it to make up a title for the page
  2497. if (colname.length < 1) {
  2498. title = PMA_messages.enum_newColumnVals;
  2499. } else {
  2500. title = PMA_messages.enum_columnVals.replace(
  2501. /%s/,
  2502. '"' + escapeHtml(decodeURIComponent(colname)) + '"'
  2503. );
  2504. }
  2505. // Get the values as a string
  2506. var inputstring = $(this)
  2507. .closest('td')
  2508. .find("input")
  2509. .val();
  2510. // Escape html entities
  2511. inputstring = $('<div/>')
  2512. .text(inputstring)
  2513. .html();
  2514. // Parse the values, escaping quotes and
  2515. // slashes on the fly, into an array
  2516. var values = [];
  2517. var in_string = false;
  2518. var curr, next, buffer = '';
  2519. for (i = 0; i < inputstring.length; i++) {
  2520. curr = inputstring.charAt(i);
  2521. next = i == inputstring.length ? '' : inputstring.charAt(i + 1);
  2522. if (! in_string && curr == "'") {
  2523. in_string = true;
  2524. } else if (in_string && curr == "\\" && next == "\\") {
  2525. buffer += "&#92;";
  2526. i++;
  2527. } else if (in_string && next == "'" && (curr == "'" || curr == "\\")) {
  2528. buffer += "&#39;";
  2529. i++;
  2530. } else if (in_string && curr == "'") {
  2531. in_string = false;
  2532. values.push(buffer);
  2533. buffer = '';
  2534. } else if (in_string) {
  2535. buffer += curr;
  2536. }
  2537. }
  2538. if (buffer.length > 0) {
  2539. // The leftovers in the buffer are the last value (if any)
  2540. values.push(buffer);
  2541. }
  2542. var fields = '';
  2543. // If there are no values, maybe the user is about to make a
  2544. // new list so we add a few for him/her to get started with.
  2545. if (values.length === 0) {
  2546. values.push('', '', '', '');
  2547. }
  2548. // Add the parsed values to the editor
  2549. var drop_icon = PMA_getImage('b_drop.png');
  2550. for (i = 0; i < values.length; i++) {
  2551. fields += "<tr><td>" +
  2552. "<input type='text' value='" + values[i] + "'/>" +
  2553. "</td><td class='drop'>" +
  2554. drop_icon +
  2555. "</td></tr>";
  2556. }
  2557. /**
  2558. * @var dialog HTML code for the ENUM/SET dialog
  2559. */
  2560. var dialog = "<div id='enum_editor'>" +
  2561. "<fieldset>" +
  2562. "<legend>" + title + "</legend>" +
  2563. "<p>" + PMA_getImage('s_notice.png') +
  2564. PMA_messages.enum_hint + "</p>" +
  2565. "<table class='values'>" + fields + "</table>" +
  2566. "</fieldset><fieldset class='tblFooters'>" +
  2567. "<table class='add'><tr><td>" +
  2568. "<div class='slider'></div>" +
  2569. "</td><td>" +
  2570. "<form><div><input type='submit' class='add_value' value='" +
  2571. $.sprintf(PMA_messages.enum_addValue, 1) +
  2572. "'/></div></form>" +
  2573. "</td></tr></table>" +
  2574. "<input type='hidden' value='" + // So we know which column's data is being edited
  2575. $(this).closest('td').find("input").attr("id") +
  2576. "' />" +
  2577. "</fieldset>" +
  2578. "</div>";
  2579. /**
  2580. * @var Defines functions to be called when the buttons in
  2581. * the buttonOptions jQuery dialog bar are pressed
  2582. */
  2583. var buttonOptions = {};
  2584. buttonOptions[PMA_messages.strGo] = function () {
  2585. // When the submit button is clicked,
  2586. // put the data back into the original form
  2587. var value_array = [];
  2588. $(this).find(".values input").each(function (index, elm) {
  2589. var val = elm.value.replace(/\\/g, '\\\\').replace(/'/g, "''");
  2590. value_array.push("'" + val + "'");
  2591. });
  2592. // get the Length/Values text field where this value belongs
  2593. var values_id = $(this).find("input[type='hidden']").val();
  2594. $("input#" + values_id).val(value_array.join(","));
  2595. $(this).dialog("close");
  2596. };
  2597. buttonOptions[PMA_messages.strClose] = function () {
  2598. $(this).dialog("close");
  2599. };
  2600. // Show the dialog
  2601. var width = parseInt(
  2602. (parseInt($('html').css('font-size'), 10) / 13) * 340,
  2603. 10
  2604. );
  2605. if (! width) {
  2606. width = 340;
  2607. }
  2608. $enum_editor_dialog = $(dialog).dialog({
  2609. minWidth: width,
  2610. modal: true,
  2611. title: PMA_messages.enum_editor,
  2612. buttons: buttonOptions,
  2613. open: function () {
  2614. // Focus the "Go" button after opening the dialog
  2615. $(this).closest('.ui-dialog').find('.ui-dialog-buttonpane button:first').focus();
  2616. },
  2617. close: function () {
  2618. $(this).remove();
  2619. }
  2620. });
  2621. // slider for choosing how many fields to add
  2622. $enum_editor_dialog.find(".slider").slider({
  2623. animate: true,
  2624. range: "min",
  2625. value: 1,
  2626. min: 1,
  2627. max: 9,
  2628. slide: function (event, ui) {
  2629. $(this).closest('table').find('input[type=submit]').val(
  2630. $.sprintf(PMA_messages.enum_addValue, ui.value)
  2631. );
  2632. }
  2633. });
  2634. // Focus the slider, otherwise it looks nearly transparent
  2635. $('a.ui-slider-handle').addClass('ui-state-focus');
  2636. return false;
  2637. });
  2638. // When "add a new value" is clicked, append an empty text field
  2639. $("input.add_value").live('click', function (e) {
  2640. e.preventDefault();
  2641. var num_new_rows = $enum_editor_dialog.find("div.slider").slider('value');
  2642. while (num_new_rows--) {
  2643. $enum_editor_dialog.find('.values')
  2644. .append(
  2645. "<tr style='display: none;'><td>" +
  2646. "<input type='text' />" +
  2647. "</td><td class='drop'>" +
  2648. PMA_getImage('b_drop.png') +
  2649. "</td></tr>"
  2650. )
  2651. .find('tr:last')
  2652. .show('fast');
  2653. }
  2654. });
  2655. // Removes the specified row from the enum editor
  2656. $("#enum_editor td.drop").live('click', function () {
  2657. $(this).closest('tr').hide('fast', function () {
  2658. $(this).remove();
  2659. });
  2660. });
  2661. });
  2662. /**
  2663. * Ensures indexes names are valid according to their type and, for a primary
  2664. * key, lock index name to 'PRIMARY'
  2665. * @param string form_id Variable which parses the form name as
  2666. * the input
  2667. * @return boolean false if there is no index form, true else
  2668. */
  2669. function checkIndexName(form_id)
  2670. {
  2671. if ($("#" + form_id).length === 0) {
  2672. return false;
  2673. }
  2674. // Gets the elements pointers
  2675. var $the_idx_name = $("#input_index_name");
  2676. var $the_idx_type = $("#select_index_type");
  2677. // Index is a primary key
  2678. if ($the_idx_type.find("option:selected").val() == 'PRIMARY') {
  2679. $the_idx_name.val('PRIMARY');
  2680. $the_idx_name.prop("disabled", true);
  2681. }
  2682. // Other cases
  2683. else {
  2684. if ($the_idx_name.val() == 'PRIMARY') {
  2685. $the_idx_name.val("");
  2686. }
  2687. $the_idx_name.prop("disabled", false);
  2688. }
  2689. return true;
  2690. } // end of the 'checkIndexName()' function
  2691. AJAX.registerTeardown('functions.js', function () {
  2692. $('#index_frm input[type=submit]').die('click');
  2693. });
  2694. AJAX.registerOnload('functions.js', function () {
  2695. /**
  2696. * Handler for adding more columns to an index in the editor
  2697. */
  2698. $('#index_frm input[type=submit]').live('click', function (event) {
  2699. event.preventDefault();
  2700. var rows_to_add = $(this)
  2701. .closest('fieldset')
  2702. .find('.slider')
  2703. .slider('value');
  2704. while (rows_to_add--) {
  2705. var $newrow = $('#index_columns')
  2706. .find('tbody > tr:first')
  2707. .clone()
  2708. .appendTo(
  2709. $('#index_columns').find('tbody')
  2710. );
  2711. $newrow.find(':input').each(function () {
  2712. $(this).val('');
  2713. });
  2714. // focus index size input on column picked
  2715. $newrow.find('select').change(function () {
  2716. if ($(this).find("option:selected").val() === '') {
  2717. return true;
  2718. }
  2719. $(this).closest("tr").find("input").focus();
  2720. });
  2721. }
  2722. });
  2723. });
  2724. function indexEditorDialog(url, title, callback_success, callback_failure)
  2725. {
  2726. /*Remove the hidden dialogs if there are*/
  2727. if ($('#edit_index_dialog').length !== 0) {
  2728. $('#edit_index_dialog').remove();
  2729. }
  2730. var $div = $('<div id="edit_index_dialog"></div>');
  2731. /**
  2732. * @var button_options Object that stores the options
  2733. * passed to jQueryUI dialog
  2734. */
  2735. var button_options = {};
  2736. button_options[PMA_messages.strGo] = function () {
  2737. /**
  2738. * @var the_form object referring to the export form
  2739. */
  2740. var $form = $("#index_frm");
  2741. var $msgbox = PMA_ajaxShowMessage(PMA_messages['strProcessingRequest']);
  2742. PMA_prepareForAjaxRequest($form);
  2743. //User wants to submit the form
  2744. $.post($form.attr('action'), $form.serialize() + "&do_save_data=1", function (data) {
  2745. if ($("#sqlqueryresults").length !== 0) {
  2746. $("#sqlqueryresults").remove();
  2747. }
  2748. if (data.success === true) {
  2749. PMA_ajaxShowMessage(data.message);
  2750. if ($('#result_query').length) {
  2751. $('#result_query').remove();
  2752. }
  2753. if (data.sql_query) {
  2754. $('<div id="result_query"></div>')
  2755. .html(data.sql_query)
  2756. .prependTo('#page_content');
  2757. PMA_highlightSQL($('#page_content'));
  2758. }
  2759. $("#result_query .notice").remove();
  2760. $("#result_query").prepend(data.message);
  2761. /*Reload the field form*/
  2762. $("#table_index").remove();
  2763. var $temp_div = $("<div id='temp_div'><div>").append(data.index_table);
  2764. $temp_div.find("#table_index").insertAfter("#index_header");
  2765. if ($("#edit_index_dialog").length > 0) {
  2766. $("#edit_index_dialog").dialog("close");
  2767. }
  2768. $('div.no_indexes_defined').hide();
  2769. if (callback_success) {
  2770. callback_success();
  2771. }
  2772. PMA_reloadNavigation();
  2773. } else {
  2774. var $temp_div = $("<div id='temp_div'><div>").append(data.error);
  2775. var $error;
  2776. if ($temp_div.find(".error code").length !== 0) {
  2777. $error = $temp_div.find(".error code").addClass("error");
  2778. } else {
  2779. $error = $temp_div;
  2780. }
  2781. if (callback_failure) {
  2782. callback_failure();
  2783. }
  2784. PMA_ajaxShowMessage($error, false);
  2785. }
  2786. }); // end $.post()
  2787. };
  2788. button_options[PMA_messages.strCancel] = function () {
  2789. $(this).dialog('close');
  2790. };
  2791. var $msgbox = PMA_ajaxShowMessage();
  2792. $.get("tbl_indexes.php", url, function (data) {
  2793. if (data.success === false) {
  2794. //in the case of an error, show the error message returned.
  2795. PMA_ajaxShowMessage(data.error, false);
  2796. } else {
  2797. PMA_ajaxRemoveMessage($msgbox);
  2798. // Show dialog if the request was successful
  2799. $div
  2800. .append(data.message)
  2801. .dialog({
  2802. title: title,
  2803. width: 450,
  2804. // increase the chance that the footer will be visible:
  2805. height: 450,
  2806. open: PMA_verifyColumnsProperties,
  2807. modal: true,
  2808. buttons: button_options,
  2809. close: function () {
  2810. $(this).remove();
  2811. }
  2812. });
  2813. checkIndexType();
  2814. checkIndexName("index_frm");
  2815. PMA_showHints($div);
  2816. // Add a slider for selecting how many columns to add to the index
  2817. $div.find('.slider').slider({
  2818. animate: true,
  2819. value: 1,
  2820. min: 1,
  2821. max: 16,
  2822. slide: function (event, ui) {
  2823. $(this).closest('fieldset').find('input[type=submit]').val(
  2824. $.sprintf(PMA_messages.strAddToIndex, ui.value)
  2825. );
  2826. }
  2827. });
  2828. // focus index size input on column picked
  2829. $div.find('table#index_columns select').change(function () {
  2830. if ($(this).find("option:selected").val() === '') {
  2831. return true;
  2832. }
  2833. $(this).closest("tr").find("input").focus();
  2834. });
  2835. // Focus the slider, otherwise it looks nearly transparent
  2836. $('a.ui-slider-handle').addClass('ui-state-focus');
  2837. // set focus on index name input, if empty
  2838. var input = $div.find('input#input_index_name');
  2839. input.val() || input.focus();
  2840. }
  2841. }); // end $.get()
  2842. }
  2843. /**
  2844. * Function to display tooltips that were
  2845. * generated on the PHP side by PMA_Util::showHint()
  2846. *
  2847. * @param object $div a div jquery object which specifies the
  2848. * domain for searching for tooltips. If we
  2849. * omit this parameter the function searches
  2850. * in the whole body
  2851. **/
  2852. function PMA_showHints($div)
  2853. {
  2854. if ($div === undefined || ! $div instanceof jQuery || $div.length === 0) {
  2855. $div = $("body");
  2856. }
  2857. $div.find('.pma_hint').each(function () {
  2858. PMA_tooltip(
  2859. $(this).children('img'),
  2860. 'img',
  2861. $(this).children('span').html()
  2862. );
  2863. });
  2864. }
  2865. AJAX.registerOnload('functions.js', function () {
  2866. PMA_showHints();
  2867. });
  2868. function PMA_mainMenuResizerCallback() {
  2869. // 5 px margin for jumping menu in Chrome
  2870. return $(document.body).width() - 5;
  2871. }
  2872. // This must be fired only once after the inital page load
  2873. $(function () {
  2874. // Initialise the menu resize plugin
  2875. $('#topmenu').menuResizer(PMA_mainMenuResizerCallback);
  2876. // register resize event
  2877. $(window).resize(function () {
  2878. $('#topmenu').menuResizer('resize');
  2879. });
  2880. });
  2881. /**
  2882. * Get the row number from the classlist (for example, row_1)
  2883. */
  2884. function PMA_getRowNumber(classlist)
  2885. {
  2886. return parseInt(classlist.split(/\s+row_/)[1], 10);
  2887. }
  2888. /**
  2889. * Changes status of slider
  2890. */
  2891. function PMA_set_status_label($element)
  2892. {
  2893. var text;
  2894. if ($element.css('display') == 'none') {
  2895. text = '+ ';
  2896. } else {
  2897. text = '- ';
  2898. }
  2899. $element.closest('.slide-wrapper').prev().find('span').text(text);
  2900. }
  2901. /**
  2902. * var toggleButton This is a function that creates a toggle
  2903. * sliding button given a jQuery reference
  2904. * to the correct DOM element
  2905. */
  2906. var toggleButton = function ($obj) {
  2907. // In rtl mode the toggle switch is flipped horizontally
  2908. // so we need to take that into account
  2909. var right;
  2910. if ($('span.text_direction', $obj).text() == 'ltr') {
  2911. right = 'right';
  2912. } else {
  2913. right = 'left';
  2914. }
  2915. /**
  2916. * var h Height of the button, used to scale the
  2917. * background image and position the layers
  2918. */
  2919. var h = $obj.height();
  2920. $('img', $obj).height(h);
  2921. $('table', $obj).css('bottom', h - 1);
  2922. /**
  2923. * var on Width of the "ON" part of the toggle switch
  2924. * var off Width of the "OFF" part of the toggle switch
  2925. */
  2926. var on = $('td.toggleOn', $obj).width();
  2927. var off = $('td.toggleOff', $obj).width();
  2928. // Make the "ON" and "OFF" parts of the switch the same size
  2929. // + 2 pixels to avoid overflowed
  2930. $('td.toggleOn > div', $obj).width(Math.max(on, off) + 2);
  2931. $('td.toggleOff > div', $obj).width(Math.max(on, off) + 2);
  2932. /**
  2933. * var w Width of the central part of the switch
  2934. */
  2935. var w = parseInt(($('img', $obj).height() / 16) * 22, 10);
  2936. // Resize the central part of the switch on the top
  2937. // layer to match the background
  2938. $('table td:nth-child(2) > div', $obj).width(w);
  2939. /**
  2940. * var imgw Width of the background image
  2941. * var tblw Width of the foreground layer
  2942. * var offset By how many pixels to move the background
  2943. * image, so that it matches the top layer
  2944. */
  2945. var imgw = $('img', $obj).width();
  2946. var tblw = $('table', $obj).width();
  2947. var offset = parseInt(((imgw - tblw) / 2), 10);
  2948. // Move the background to match the layout of the top layer
  2949. $obj.find('img').css(right, offset);
  2950. /**
  2951. * var offw Outer width of the "ON" part of the toggle switch
  2952. * var btnw Outer width of the central part of the switch
  2953. */
  2954. var offw = $('td.toggleOff', $obj).outerWidth();
  2955. var btnw = $('table td:nth-child(2)', $obj).outerWidth();
  2956. // Resize the main div so that exactly one side of
  2957. // the switch plus the central part fit into it.
  2958. $obj.width(offw + btnw + 2);
  2959. /**
  2960. * var move How many pixels to move the
  2961. * switch by when toggling
  2962. */
  2963. var move = $('td.toggleOff', $obj).outerWidth();
  2964. // If the switch is initialized to the
  2965. // OFF state we need to move it now.
  2966. if ($('div.container', $obj).hasClass('off')) {
  2967. if (right == 'right') {
  2968. $('div.container', $obj).animate({'left': '-=' + move + 'px'}, 0);
  2969. } else {
  2970. $('div.container', $obj).animate({'left': '+=' + move + 'px'}, 0);
  2971. }
  2972. }
  2973. // Attach an 'onclick' event to the switch
  2974. $('div.container', $obj).click(function () {
  2975. if ($(this).hasClass('isActive')) {
  2976. return false;
  2977. } else {
  2978. $(this).addClass('isActive');
  2979. }
  2980. var $msg = PMA_ajaxShowMessage();
  2981. var $container = $(this);
  2982. var callback = $('span.callback', this).text();
  2983. var operator, url, removeClass, addClass;
  2984. // Perform the actual toggle
  2985. if ($(this).hasClass('on')) {
  2986. if (right == 'right') {
  2987. operator = '-=';
  2988. } else {
  2989. operator = '+=';
  2990. }
  2991. url = $(this).find('td.toggleOff > span').text();
  2992. removeClass = 'on';
  2993. addClass = 'off';
  2994. } else {
  2995. if (right == 'right') {
  2996. operator = '+=';
  2997. } else {
  2998. operator = '-=';
  2999. }
  3000. url = $(this).find('td.toggleOn > span').text();
  3001. removeClass = 'off';
  3002. addClass = 'on';
  3003. }
  3004. $.post(url, {'ajax_request': true}, function (data) {
  3005. if (data.success === true) {
  3006. PMA_ajaxRemoveMessage($msg);
  3007. $container
  3008. .removeClass(removeClass)
  3009. .addClass(addClass)
  3010. .animate({'left': operator + move + 'px'}, function () {
  3011. $container.removeClass('isActive');
  3012. });
  3013. eval(callback);
  3014. } else {
  3015. PMA_ajaxShowMessage(data.error, false);
  3016. $container.removeClass('isActive');
  3017. }
  3018. });
  3019. });
  3020. };
  3021. /**
  3022. * Unbind all event handlers before tearing down a page
  3023. */
  3024. AJAX.registerTeardown('functions.js', function () {
  3025. $('div.container').unbind('click');
  3026. });
  3027. /**
  3028. * Initialise all toggle buttons
  3029. */
  3030. AJAX.registerOnload('functions.js', function () {
  3031. $('div.toggleAjax').each(function () {
  3032. var $button = $(this).show();
  3033. $button.find('img').each(function () {
  3034. if (this.complete) {
  3035. toggleButton($button);
  3036. } else {
  3037. $(this).load(function () {
  3038. toggleButton($button);
  3039. });
  3040. }
  3041. });
  3042. });
  3043. });
  3044. /**
  3045. * Unbind all event handlers before tearing down a page
  3046. */
  3047. AJAX.registerTeardown('functions.js', function () {
  3048. $('.vpointer').die('hover');
  3049. $('.vmarker').die('click');
  3050. $('#pageselector').die('change');
  3051. $('a.formLinkSubmit').die('click');
  3052. $('#update_recent_tables').unbind('ready');
  3053. $('#sync_favorite_tables').unbind('ready');
  3054. });
  3055. /**
  3056. * Vertical pointer
  3057. */
  3058. AJAX.registerOnload('functions.js', function () {
  3059. $('.vpointer').live('hover',
  3060. //handlerInOut
  3061. function (e) {
  3062. var $this_td = $(this);
  3063. var row_num = PMA_getRowNumber($this_td.attr('class'));
  3064. // for all td of the same vertical row, toggle hover
  3065. $('.vpointer').filter('.row_' + row_num).toggleClass('hover');
  3066. }
  3067. );
  3068. /**
  3069. * Vertical marker
  3070. */
  3071. $('.vmarker').live('click', function (e) {
  3072. // do not trigger when clicked on anchor
  3073. if ($(e.target).is('a, img, a *')) {
  3074. return;
  3075. }
  3076. var $this_td = $(this);
  3077. var row_num = PMA_getRowNumber($this_td.attr('class'));
  3078. // XXX: FF fires two click events for <label> (label and checkbox), so we need to handle this differently
  3079. var $checkbox = $('.vmarker').filter('.row_' + row_num + ':first').find(':checkbox');
  3080. if ($checkbox.length) {
  3081. // checkbox in a row, add or remove class depending on checkbox state
  3082. var checked = $checkbox.prop('checked');
  3083. if (!$(e.target).is(':checkbox, label')) {
  3084. checked = !checked;
  3085. $checkbox.prop('checked', checked);
  3086. }
  3087. // for all td of the same vertical row, toggle the marked class
  3088. if (checked) {
  3089. $('.vmarker').filter('.row_' + row_num).addClass('marked');
  3090. } else {
  3091. $('.vmarker').filter('.row_' + row_num).removeClass('marked');
  3092. }
  3093. } else {
  3094. // normaln data table, just toggle class
  3095. $('.vmarker').filter('.row_' + row_num).toggleClass('marked');
  3096. }
  3097. });
  3098. /**
  3099. * Autosubmit page selector
  3100. */
  3101. $('select.pageselector').live('change', function (event) {
  3102. event.stopPropagation();
  3103. // Check where to load the new content
  3104. if ($(this).closest("#pma_navigation").length === 0) {
  3105. // For the main page we don't need to do anything,
  3106. $(this).closest("form").submit();
  3107. } else {
  3108. // but for the navigation we need to manually replace the content
  3109. PMA_navigationTreePagination($(this));
  3110. }
  3111. });
  3112. /**
  3113. * Load version information asynchronously.
  3114. */
  3115. if ($('li.jsversioncheck').length > 0) {
  3116. $.getJSON('version_check.php', {}, PMA_current_version);
  3117. }
  3118. if ($('#is_git_revision').length > 0) {
  3119. setTimeout(PMA_display_git_revision, 10);
  3120. }
  3121. /**
  3122. * Slider effect.
  3123. */
  3124. PMA_init_slider();
  3125. /**
  3126. * Enables the text generated by PMA_Util::linkOrButton() to be clickable
  3127. */
  3128. $('a.formLinkSubmit').live('click', function (e) {
  3129. if ($(this).attr('href').indexOf('=') != -1) {
  3130. var data = $(this).attr('href').substr($(this).attr('href').indexOf('#') + 1).split('=', 2);
  3131. $(this).parents('form').append('<input type="hidden" name="' + data[0] + '" value="' + data[1] + '"/>');
  3132. }
  3133. $(this).parents('form').submit();
  3134. return false;
  3135. });
  3136. if ($('#update_recent_tables').length) {
  3137. $.get(
  3138. $('#update_recent_tables').attr('href'),
  3139. function (data) {
  3140. if (data.success === true) {
  3141. $('#pma_recent_list').html(data.list);
  3142. }
  3143. }
  3144. );
  3145. }
  3146. // Sync favorite tables from localStorage to pmadb.
  3147. if ($('#sync_favorite_tables').length) {
  3148. $.ajax({
  3149. url: $('#sync_favorite_tables').attr("href"),
  3150. cache: false,
  3151. type: 'POST',
  3152. data: {
  3153. favorite_tables: (window.localStorage['favorite_tables']
  3154. !== undefined)
  3155. ? window.localStorage['favorite_tables']
  3156. : ''
  3157. },
  3158. success: function (data) {
  3159. // Update localStorage.
  3160. if (window.localStorage !== undefined) {
  3161. window.localStorage['favorite_tables']
  3162. = data.favorite_tables;
  3163. }
  3164. $('#pma_favorite_list').html(data.list);
  3165. }
  3166. });
  3167. }
  3168. }); // end of $()
  3169. /**
  3170. * Initializes slider effect.
  3171. */
  3172. function PMA_init_slider()
  3173. {
  3174. $('div.pma_auto_slider').each(function () {
  3175. var $this = $(this);
  3176. if ($this.data('slider_init_done')) {
  3177. return;
  3178. }
  3179. var $wrapper = $('<div>', {'class': 'slide-wrapper'});
  3180. $wrapper.toggle($this.is(':visible'));
  3181. $('<a>', {href: '#' + this.id, "class": 'ajax'})
  3182. .text(this.title)
  3183. .prepend($('<span>'))
  3184. .insertBefore($this)
  3185. .click(function () {
  3186. var $wrapper = $this.closest('.slide-wrapper');
  3187. var visible = $this.is(':visible');
  3188. if (!visible) {
  3189. $wrapper.show();
  3190. }
  3191. $this[visible ? 'hide' : 'show']('blind', function () {
  3192. $wrapper.toggle(!visible);
  3193. PMA_set_status_label($this);
  3194. });
  3195. return false;
  3196. });
  3197. $this.wrap($wrapper);
  3198. PMA_set_status_label($this);
  3199. $this.data('slider_init_done', 1);
  3200. });
  3201. }
  3202. /**
  3203. * Initializes slider effect.
  3204. */
  3205. AJAX.registerOnload('functions.js', function () {
  3206. PMA_init_slider();
  3207. });
  3208. /**
  3209. * Restores sliders to the state they were in before initialisation.
  3210. */
  3211. AJAX.registerTeardown('functions.js', function () {
  3212. $('div.pma_auto_slider').each(function () {
  3213. var $this = $(this);
  3214. $this.removeData();
  3215. $this.parent().replaceWith($this);
  3216. $this.parent().children('a').remove();
  3217. });
  3218. });
  3219. /**
  3220. * Creates a message inside an object with a sliding effect
  3221. *
  3222. * @param msg A string containing the text to display
  3223. * @param $obj a jQuery object containing the reference
  3224. * to the element where to put the message
  3225. * This is optional, if no element is
  3226. * provided, one will be created below the
  3227. * navigation links at the top of the page
  3228. *
  3229. * @return bool True on success, false on failure
  3230. */
  3231. function PMA_slidingMessage(msg, $obj)
  3232. {
  3233. if (msg === undefined || msg.length === 0) {
  3234. // Don't show an empty message
  3235. return false;
  3236. }
  3237. if ($obj === undefined || ! $obj instanceof jQuery || $obj.length === 0) {
  3238. // If the second argument was not supplied,
  3239. // we might have to create a new DOM node.
  3240. if ($('#PMA_slidingMessage').length === 0) {
  3241. $('#page_content').prepend(
  3242. '<span id="PMA_slidingMessage" ' +
  3243. 'style="display: inline-block;"></span>'
  3244. );
  3245. }
  3246. $obj = $('#PMA_slidingMessage');
  3247. }
  3248. if ($obj.has('div').length > 0) {
  3249. // If there already is a message inside the
  3250. // target object, we must get rid of it
  3251. $obj
  3252. .find('div')
  3253. .first()
  3254. .fadeOut(function () {
  3255. $obj
  3256. .children()
  3257. .remove();
  3258. $obj
  3259. .append('<div>' + msg + '</div>');
  3260. // highlight any sql before taking height;
  3261. PMA_highlightSQL($obj);
  3262. $obj.find('div')
  3263. .first()
  3264. .hide();
  3265. $obj
  3266. .animate({
  3267. height: $obj.find('div').first().height()
  3268. })
  3269. .find('div')
  3270. .first()
  3271. .fadeIn();
  3272. });
  3273. } else {
  3274. // Object does not already have a message
  3275. // inside it, so we simply slide it down
  3276. $obj.width('100%')
  3277. .html('<div>' + msg + '</div>');
  3278. // highlight any sql before taking height;
  3279. PMA_highlightSQL($obj);
  3280. var h = $obj
  3281. .find('div')
  3282. .first()
  3283. .hide()
  3284. .height();
  3285. $obj
  3286. .find('div')
  3287. .first()
  3288. .css('height', 0)
  3289. .show()
  3290. .animate({
  3291. height: h
  3292. }, function () {
  3293. // Set the height of the parent
  3294. // to the height of the child
  3295. $obj
  3296. .height(
  3297. $obj
  3298. .find('div')
  3299. .first()
  3300. .height()
  3301. );
  3302. });
  3303. }
  3304. return true;
  3305. } // end PMA_slidingMessage()
  3306. /**
  3307. * Unbind all event handlers before tearing down a page
  3308. */
  3309. AJAX.registerTeardown('functions.js', function () {
  3310. $("#drop_tbl_anchor.ajax").die('click');
  3311. $("#drop_view_anchor.ajax").die('click');
  3312. $("#truncate_tbl_anchor.ajax").die('click');
  3313. });
  3314. /**
  3315. * Attach Ajax event handlers for Drop Table.
  3316. */
  3317. AJAX.registerOnload('functions.js', function () {
  3318. $("#drop_tbl_anchor.ajax").live('click', function (event) {
  3319. event.preventDefault();
  3320. /**
  3321. * @var question String containing the question to be asked for confirmation
  3322. */
  3323. var question = PMA_messages.strDropTableStrongWarning + ' ';
  3324. question += $.sprintf(
  3325. PMA_messages.strDoYouReally,
  3326. 'DROP TABLE ' + escapeHtml(PMA_commonParams.get('table'))
  3327. );
  3328. $(this).PMA_confirm(question, $(this).attr('href'), function (url) {
  3329. var $msgbox = PMA_ajaxShowMessage(PMA_messages.strProcessingRequest);
  3330. $.get(url, {'is_js_confirmed': '1', 'ajax_request': true}, function (data) {
  3331. if (data.success === true) {
  3332. PMA_ajaxRemoveMessage($msgbox);
  3333. // Table deleted successfully, refresh both the frames
  3334. PMA_reloadNavigation();
  3335. PMA_commonParams.set('table', '');
  3336. PMA_commonActions.refreshMain(
  3337. PMA_commonParams.get('opendb_url'),
  3338. function () {
  3339. PMA_ajaxShowMessage(data.message);
  3340. }
  3341. );
  3342. } else {
  3343. PMA_ajaxShowMessage(data.error, false);
  3344. }
  3345. }); // end $.get()
  3346. }); // end $.PMA_confirm()
  3347. }); //end of Drop Table Ajax action
  3348. $("#drop_view_anchor.ajax").live('click', function (event) {
  3349. event.preventDefault();
  3350. /**
  3351. * @var question String containing the question to be asked for confirmation
  3352. */
  3353. var question = PMA_messages.strDropTableStrongWarning + ' ';
  3354. question += $.sprintf(
  3355. PMA_messages.strDoYouReally,
  3356. 'DROP VIEW ' + escapeHtml(PMA_commonParams.get('table'))
  3357. );
  3358. $(this).PMA_confirm(question, $(this).attr('href'), function (url) {
  3359. var $msgbox = PMA_ajaxShowMessage(PMA_messages.strProcessingRequest);
  3360. $.get(url, {'is_js_confirmed': '1', 'ajax_request': true}, function (data) {
  3361. if (data.success === true) {
  3362. PMA_ajaxRemoveMessage($msgbox);
  3363. // Table deleted successfully, refresh both the frames
  3364. PMA_reloadNavigation();
  3365. PMA_commonParams.set('table', '');
  3366. PMA_commonActions.refreshMain(
  3367. PMA_commonParams.get('opendb_url'),
  3368. function () {
  3369. PMA_ajaxShowMessage(data.message);
  3370. }
  3371. );
  3372. } else {
  3373. PMA_ajaxShowMessage(data.error, false);
  3374. }
  3375. }); // end $.get()
  3376. }); // end $.PMA_confirm()
  3377. }); //end of Drop View Ajax action
  3378. $("#truncate_tbl_anchor.ajax").live('click', function (event) {
  3379. event.preventDefault();
  3380. /**
  3381. * @var question String containing the question to be asked for confirmation
  3382. */
  3383. var question = PMA_messages.strTruncateTableStrongWarning + ' ';
  3384. question += $.sprintf(
  3385. PMA_messages.strDoYouReally,
  3386. 'TRUNCATE ' + escapeHtml(PMA_commonParams.get('table'))
  3387. );
  3388. $(this).PMA_confirm(question, $(this).attr('href'), function (url) {
  3389. PMA_ajaxShowMessage(PMA_messages.strProcessingRequest);
  3390. $.get(url, {'is_js_confirmed': '1', 'ajax_request': true}, function (data) {
  3391. if ($("#sqlqueryresults").length !== 0) {
  3392. $("#sqlqueryresults").remove();
  3393. }
  3394. if ($("#result_query").length !== 0) {
  3395. $("#result_query").remove();
  3396. }
  3397. if (data.success === true) {
  3398. PMA_ajaxShowMessage(data.message);
  3399. $("<div id='sqlqueryresults'></div>").prependTo("#page_content");
  3400. $("#sqlqueryresults").html(data.sql_query);
  3401. PMA_highlightSQL($('#page_content'));
  3402. } else {
  3403. PMA_ajaxShowMessage(data.error, false);
  3404. }
  3405. }); // end $.get()
  3406. }); // end $.PMA_confirm()
  3407. }); //end of Truncate Table Ajax action
  3408. }); // end of $() for Truncate Table
  3409. /**
  3410. * Attach CodeMirror2 editor to SQL edit area.
  3411. */
  3412. AJAX.registerOnload('functions.js', function () {
  3413. var $elm = $('#sqlquery');
  3414. if ($elm.length > 0) {
  3415. if (typeof CodeMirror != 'undefined') {
  3416. // for codemirror
  3417. codemirror_editor = CodeMirror.fromTextArea($elm[0], {
  3418. lineNumbers: true,
  3419. matchBrackets: true,
  3420. indentUnit: 4,
  3421. mode: "text/x-mysql",
  3422. lineWrapping: true
  3423. });
  3424. codemirror_editor.focus();
  3425. $(codemirror_editor.getWrapperElement()).bind(
  3426. 'keydown',
  3427. catchKeypressesFromSqlTextboxes
  3428. );
  3429. } else {
  3430. // without codemirror
  3431. $elm.focus().bind('keydown', catchKeypressesFromSqlTextboxes);
  3432. }
  3433. }
  3434. PMA_highlightSQL($('body'));
  3435. });
  3436. AJAX.registerTeardown('functions.js', function () {
  3437. if (codemirror_editor) {
  3438. $('#sqlquery').text(codemirror_editor.getValue());
  3439. codemirror_editor.toTextArea();
  3440. codemirror_editor = false;
  3441. }
  3442. });
  3443. /**
  3444. * jQuery plugin to cancel selection in HTML code.
  3445. */
  3446. (function ($) {
  3447. $.fn.noSelect = function (p) { //no select plugin by Paulo P.Marinas
  3448. var prevent = (p === null) ? true : p;
  3449. if (prevent) {
  3450. return this.each(function () {
  3451. if ($.browser.msie || $.browser.safari) {
  3452. $(this).bind('selectstart', function () {
  3453. return false;
  3454. });
  3455. } else if ($.browser.mozilla) {
  3456. $(this).css('MozUserSelect', 'none');
  3457. $('body').trigger('focus');
  3458. } else if ($.browser.opera) {
  3459. $(this).bind('mousedown', function () {
  3460. return false;
  3461. });
  3462. } else {
  3463. $(this).attr('unselectable', 'on');
  3464. }
  3465. });
  3466. } else {
  3467. return this.each(function () {
  3468. if ($.browser.msie || $.browser.safari) {
  3469. $(this).unbind('selectstart');
  3470. } else if ($.browser.mozilla) {
  3471. $(this).css('MozUserSelect', 'inherit');
  3472. } else if ($.browser.opera) {
  3473. $(this).unbind('mousedown');
  3474. } else {
  3475. $(this).removeAttr('unselectable');
  3476. }
  3477. });
  3478. }
  3479. }; //end noSelect
  3480. })(jQuery);
  3481. /**
  3482. * jQuery plugin to correctly filter input fields by value, needed
  3483. * because some nasty values may break selector syntax
  3484. */
  3485. (function ($) {
  3486. $.fn.filterByValue = function (value) {
  3487. return this.filter(function () {
  3488. return $(this).val() === value;
  3489. });
  3490. };
  3491. })(jQuery);
  3492. /**
  3493. * Create a jQuery UI tooltip
  3494. *
  3495. * @param $elements jQuery object representing the elements
  3496. * @param item the item
  3497. * (see http://api.jqueryui.com/tooltip/#option-items)
  3498. * @param myContent content of the tooltip
  3499. * @param additionalOptions to override the default options
  3500. *
  3501. */
  3502. function PMA_tooltip($elements, item, myContent, additionalOptions)
  3503. {
  3504. if ($('#no_hint').length > 0) {
  3505. return;
  3506. }
  3507. var defaultOptions = {
  3508. content: myContent,
  3509. items: item,
  3510. tooltipClass: "tooltip",
  3511. track: true,
  3512. show: false,
  3513. hide: false
  3514. };
  3515. $elements.tooltip($.extend(true, defaultOptions, additionalOptions));
  3516. }
  3517. /**
  3518. * Return value of a cell in a table.
  3519. */
  3520. function PMA_getCellValue(td) {
  3521. var $td = $(td);
  3522. if ($td.is('.null')) {
  3523. return '';
  3524. } else if (! $td.is('.to_be_saved') && $td.data('original_data')) {
  3525. return $td.data('original_data');
  3526. } else {
  3527. return $td.text();
  3528. }
  3529. }
  3530. /**
  3531. * Unbind all event handlers before tearing down a page
  3532. */
  3533. AJAX.registerTeardown('functions.js', function () {
  3534. $('a.themeselect').die('click');
  3535. $('.autosubmit').die('change');
  3536. $('a.take_theme').unbind('click');
  3537. });
  3538. AJAX.registerOnload('functions.js', function () {
  3539. /**
  3540. * Theme selector.
  3541. */
  3542. $('a.themeselect').live('click', function (e) {
  3543. window.open(
  3544. e.target,
  3545. 'themes',
  3546. 'left=10,top=20,width=510,height=350,scrollbars=yes,status=yes,resizable=yes'
  3547. );
  3548. return false;
  3549. });
  3550. /**
  3551. * Automatic form submission on change.
  3552. */
  3553. $('.autosubmit').live('change', function (e) {
  3554. $(this).closest('form').submit();
  3555. });
  3556. /**
  3557. * Theme changer.
  3558. */
  3559. $('a.take_theme').click(function (e) {
  3560. var what = this.name;
  3561. if (window.opener && window.opener.document.forms['setTheme'].elements['set_theme']) {
  3562. window.opener.document.forms['setTheme'].elements['set_theme'].value = what;
  3563. window.opener.document.forms['setTheme'].submit();
  3564. window.close();
  3565. return false;
  3566. }
  3567. return true;
  3568. });
  3569. });
  3570. /**
  3571. * Clear text selection
  3572. */
  3573. function PMA_clearSelection() {
  3574. if (document.selection && document.selection.empty) {
  3575. document.selection.empty();
  3576. } else if (window.getSelection) {
  3577. var sel = window.getSelection();
  3578. if (sel.empty) {
  3579. sel.empty();
  3580. }
  3581. if (sel.removeAllRanges) {
  3582. sel.removeAllRanges();
  3583. }
  3584. }
  3585. }
  3586. /**
  3587. * HTML escaping
  3588. */
  3589. function escapeHtml(unsafe) {
  3590. return unsafe
  3591. .replace(/&/g, "&amp;")
  3592. .replace(/</g, "&lt;")
  3593. .replace(/>/g, "&gt;")
  3594. .replace(/"/g, "&quot;")
  3595. .replace(/'/g, "&#039;");
  3596. }
  3597. /**
  3598. * Print button
  3599. */
  3600. function printPage()
  3601. {
  3602. // Do print the page
  3603. if (typeof(window.print) != 'undefined') {
  3604. window.print();
  3605. }
  3606. }
  3607. /**
  3608. * Unbind all event handlers before tearing down a page
  3609. */
  3610. AJAX.registerTeardown('functions.js', function () {
  3611. $('input#print').unbind('click');
  3612. $('a.create_view.ajax').die('click');
  3613. $('#createViewDialog').find('input, select').die('keydown');
  3614. });
  3615. AJAX.registerOnload('functions.js', function () {
  3616. $('input#print').click(printPage);
  3617. /**
  3618. * Ajaxification for the "Create View" action
  3619. */
  3620. $('a.create_view.ajax').live('click', function (e) {
  3621. e.preventDefault();
  3622. PMA_createViewDialog($(this));
  3623. });
  3624. /**
  3625. * Attach Ajax event handlers for input fields in the editor
  3626. * and used to submit the Ajax request when the ENTER key is pressed.
  3627. */
  3628. if ($('#createViewDialog').length !== 0) {
  3629. $('#createViewDialog').find('input, select').live('keydown', function (e) {
  3630. if (e.which === 13) { // 13 is the ENTER key
  3631. e.preventDefault();
  3632. // with preventing default, selection by <select> tag
  3633. // was also prevented in IE
  3634. $(this).blur();
  3635. $(this).closest('.ui-dialog').find('.ui-button:first').click();
  3636. }
  3637. }); // end $.live()
  3638. }
  3639. var $elm = $('textarea[name="view[as]"]');
  3640. if ($elm.length > 0) {
  3641. if (typeof CodeMirror != 'undefined') {
  3642. syntaxHighlighter = CodeMirror.fromTextArea(
  3643. $elm[0],
  3644. {
  3645. lineNumbers: true,
  3646. matchBrackets: true,
  3647. indentUnit: 4,
  3648. mode: "text/x-mysql",
  3649. lineWrapping: true
  3650. }
  3651. );
  3652. }
  3653. }
  3654. });
  3655. function PMA_createViewDialog($this)
  3656. {
  3657. var $msg = PMA_ajaxShowMessage();
  3658. var syntaxHighlighter = null;
  3659. $.get($this.attr('href') + '&ajax_request=1&ajax_dialog=1', function (data) {
  3660. if (data.success === true) {
  3661. PMA_ajaxRemoveMessage($msg);
  3662. var buttonOptions = {};
  3663. buttonOptions[PMA_messages.strGo] = function () {
  3664. if (typeof CodeMirror !== 'undefined') {
  3665. syntaxHighlighter.save();
  3666. }
  3667. $msg = PMA_ajaxShowMessage();
  3668. $.get('view_create.php', $('#createViewDialog').find('form').serialize(), function (data) {
  3669. PMA_ajaxRemoveMessage($msg);
  3670. if (data.success === true) {
  3671. $('#createViewDialog').dialog("close");
  3672. $('#result_query').html(data.message);
  3673. PMA_reloadNavigation();
  3674. } else {
  3675. PMA_ajaxShowMessage(data.error, false);
  3676. }
  3677. });
  3678. };
  3679. buttonOptions[PMA_messages.strClose] = function () {
  3680. $(this).dialog("close");
  3681. };
  3682. var $dialog = $('<div/>').attr('id', 'createViewDialog').append(data.message).dialog({
  3683. width: 600,
  3684. minWidth: 400,
  3685. modal: true,
  3686. buttons: buttonOptions,
  3687. title: PMA_messages.strCreateView,
  3688. close: function () {
  3689. $(this).remove();
  3690. }
  3691. });
  3692. // Attach syntax highlighted editor
  3693. if (typeof CodeMirror !== 'undefined') {
  3694. var $elm = $dialog.find('textarea');
  3695. var opts = {lineNumbers: true, matchBrackets: true, indentUnit: 4, mode: "text/x-mysql", lineWrapping: true};
  3696. syntaxHighlighter = CodeMirror.fromTextArea($elm[0], opts);
  3697. }
  3698. $('input:visible[type=text]', $dialog).first().focus();
  3699. } else {
  3700. PMA_ajaxShowMessage(data.error);
  3701. }
  3702. });
  3703. }
  3704. /**
  3705. * Makes the breadcrumbs and the menu bar float at the top of the viewport
  3706. */
  3707. $(function () {
  3708. if ($("#floating_menubar").length && $('#PMA_disable_floating_menubar').length === 0) {
  3709. var left = $('html').attr('dir') == 'ltr' ? 'left' : 'right';
  3710. $("#floating_menubar")
  3711. .css('margin-' + left, $('#pma_navigation').width() + $('#pma_navigation_resizer').width())
  3712. .css(left, 0)
  3713. .css({
  3714. 'position': 'fixed',
  3715. 'top': 0,
  3716. 'width': '100%',
  3717. 'z-index': 500
  3718. })
  3719. .append($('#serverinfo'))
  3720. .append($('#topmenucontainer'));
  3721. // Allow the DOM to render, then adjust the padding on the body
  3722. setTimeout(function () {
  3723. $('body').css(
  3724. 'padding-top',
  3725. $('#floating_menubar').outerHeight(true)
  3726. );
  3727. $('#topmenu').menuResizer('resize');
  3728. }, 4);
  3729. }
  3730. });
  3731. /**
  3732. * Scrolls the page to the top if clicking the serverinfo bar
  3733. */
  3734. $(function () {
  3735. $(document).delegate("#serverinfo, #goto_pagetop", "click", function (event) {
  3736. event.preventDefault();
  3737. $('html, body').animate({scrollTop: 0}, 'fast');
  3738. });
  3739. });
  3740. var checkboxes_sel = "input.checkall:checkbox:enabled";
  3741. /**
  3742. * Watches checkboxes in a form to set the checkall box accordingly
  3743. */
  3744. var checkboxes_changed = function () {
  3745. var $form = $(this.form);
  3746. // total number of checkboxes in current form
  3747. var total_boxes = $form.find(checkboxes_sel).length;
  3748. // number of checkboxes checked in current form
  3749. var checked_boxes = $form.find(checkboxes_sel + ":checked").length;
  3750. var $checkall = $form.find("input.checkall_box");
  3751. if (total_boxes == checked_boxes) {
  3752. $checkall.prop({checked: true, indeterminate: false});
  3753. }
  3754. else if (checked_boxes > 0) {
  3755. $checkall.prop({checked: true, indeterminate: true});
  3756. }
  3757. else {
  3758. $checkall.prop({checked: false, indeterminate: false});
  3759. }
  3760. };
  3761. $(checkboxes_sel).live("change", checkboxes_changed);
  3762. $("input.checkall_box").live("change", function () {
  3763. var is_checked = $(this).is(":checked");
  3764. $(this.form).find(checkboxes_sel).prop("checked", is_checked)
  3765. .parents("tr").toggleClass("marked", is_checked);
  3766. });
  3767. /**
  3768. * Toggles row colors of a set of 'tr' elements starting from a given element
  3769. *
  3770. * @param $start Starting element
  3771. */
  3772. function toggleRowColors($start)
  3773. {
  3774. for (var $curr_row = $start; $curr_row.length > 0; $curr_row = $curr_row.next()) {
  3775. if ($curr_row.hasClass('odd')) {
  3776. $curr_row.removeClass('odd').addClass('even');
  3777. } else if ($curr_row.hasClass('even')) {
  3778. $curr_row.removeClass('even').addClass('odd');
  3779. }
  3780. }
  3781. }
  3782. /**
  3783. * Formats a byte number to human-readable form
  3784. *
  3785. * @param bytes the bytes to format
  3786. * @param optional subdecimals the number of digits after the point
  3787. * @param optional pointchar the char to use as decimal point
  3788. */
  3789. function formatBytes(bytes, subdecimals, pointchar) {
  3790. if (!subdecimals) {
  3791. subdecimals = 0;
  3792. }
  3793. if (!pointchar) {
  3794. pointchar = '.';
  3795. }
  3796. var units = ['B', 'KiB', 'MiB', 'GiB'];
  3797. for (var i = 0; bytes > 1024 && i < units.length; i++) {
  3798. bytes /= 1024;
  3799. }
  3800. var factor = Math.pow(10, subdecimals);
  3801. bytes = Math.round(bytes * factor) / factor;
  3802. bytes = bytes.toString().split('.').join(pointchar);
  3803. return bytes + ' ' + units[i];
  3804. }
  3805. AJAX.registerOnload('functions.js', function () {
  3806. /**
  3807. * Opens pma more themes link in themes browser, in new window instead of popup
  3808. * This way, we don't break HTML validity
  3809. */
  3810. $("a._blank").prop("target", "_blank");
  3811. /**
  3812. * Reveal the login form to users with JS enabled
  3813. * and focus the appropriate input field
  3814. */
  3815. var $loginform = $('#loginform');
  3816. if ($loginform.length) {
  3817. $loginform.find('.js-show').show();
  3818. if ($('#input_username').val()) {
  3819. $('#input_password').focus();
  3820. } else {
  3821. $('#input_username').focus();
  3822. }
  3823. }
  3824. });
  3825. /**
  3826. * When user gets an ajax session expiry message, we show a login link
  3827. */
  3828. $('a.login-link').live('click', function (e) {
  3829. e.preventDefault();
  3830. window.location.reload(true);
  3831. });
  3832. /**
  3833. * Dynamically adjust the width of the boxes
  3834. * on the table and db operations pages
  3835. */
  3836. (function () {
  3837. function DynamicBoxes() {
  3838. var $boxContainer = $('#boxContainer');
  3839. if ($boxContainer.length) {
  3840. var minWidth = $boxContainer.data('box-width');
  3841. var viewport = $(window).width() - $('#pma_navigation').width();
  3842. var slots = Math.floor(viewport / minWidth);
  3843. $boxContainer.children()
  3844. .each(function () {
  3845. if (viewport < minWidth) {
  3846. $(this).width(minWidth);
  3847. } else {
  3848. $(this).css('width', ((1 / slots) * 100) + "%");
  3849. }
  3850. })
  3851. .removeClass('clearfloat')
  3852. .filter(':nth-child(' + slots + 'n+1)')
  3853. .addClass('clearfloat');
  3854. }
  3855. }
  3856. AJAX.registerOnload('functions.js', function () {
  3857. DynamicBoxes();
  3858. });
  3859. $(function () {
  3860. $(window).resize(DynamicBoxes);
  3861. });
  3862. })();
  3863. /**
  3864. * Formats timestamp for display
  3865. */
  3866. function PMA_formatDateTime(date, seconds) {
  3867. var result = $.datepicker.formatDate('yy-mm-dd', date);
  3868. var timefmt = 'HH:mm';
  3869. if (seconds) {
  3870. timefmt = 'HH:mm:ss';
  3871. }
  3872. return result + ' ' + $.datepicker.formatTime(
  3873. timefmt, {
  3874. hour: date.getHours(),
  3875. minute: date.getMinutes(),
  3876. second: date.getSeconds()
  3877. }
  3878. );
  3879. }
  3880. /**
  3881. * Check than forms have less fields than max allowed by PHP.
  3882. */
  3883. function checkNumberOfFields() {
  3884. if (typeof maxInputVars === 'undefined') {
  3885. return false;
  3886. }
  3887. if (false === maxInputVars) {
  3888. return false;
  3889. }
  3890. $('form').each(function() {
  3891. var nbInputs = $(this).find(':input').length;
  3892. if (nbInputs > maxInputVars) {
  3893. var warning = $.sprintf(PMA_messages.strTooManyInputs, maxInputVars);
  3894. PMA_ajaxShowMessage(warning);
  3895. return false;
  3896. }
  3897. return true;
  3898. });
  3899. return true;
  3900. }