├── README.txt
├── dialog-jquery.js
└── weblocks-jquery.js
/README.txt:
--------------------------------------------------------------------------------
1 | These are javascript files which allow to replace prototype-dependent functionality in weblocks with jquery-dependent functionality.
2 | To use it copy jquery files (or link if you use this repository as a submodule) to pub/scripts directory, unload prototype, load jquery and these files.
3 | Example of loading is following.
4 |
5 | (defwebapp test-app
6 | :prefix "/"
7 | :description "test-app: A new great application"
8 | :init-user-session 'test-app::init-user-session
9 | :autostart nil ;; have to start the app manually
10 | :ignore-default-dependencies t ;; accept the defaults
11 | :debug t
12 | :dependencies
13 | (list
14 | (make-instance 'script-dependency :url "/pub/scripts/jquery-1.7.2.min.js")
15 | (make-instance 'stylesheet-dependency :url "/pub/stylesheets/main.css")
16 | (make-instance 'script-dependency :url "/pub/scripts/weblocks-jquery.js")
17 | (make-instance 'script-dependency :url "/pub/scripts/dialog-jquery.js")
18 | (make-instance 'script-dependency :url "/pub/scripts/jquery-seq.js")))
19 |
20 | Repository also contains onAvailable utility function, useful for Weblocks plugisn writing.
21 |
22 |
--------------------------------------------------------------------------------
/dialog-jquery.js:
--------------------------------------------------------------------------------
1 | /*!
2 | * Dialog jQuery - dialog helper functions for Weblocks, v0.0.1
3 | */
4 |
5 | function showDialog(title, body, cssClass){
6 | if(jQuery('.graybox').length){
7 | jQuery('.graybox').show();
8 | }else{
9 | jQuery('body').append('
');
10 | }
11 |
12 | var dialogBody = '' + body + '
';
13 | var titleBar = '';
14 | var dialogHtml = '' + titleBar + dialogBody + '
';
15 | var dialog = jQuery(dialogHtml);
16 |
17 | jQuery('div.page-wrapper').append(dialog);
18 | var offsetTop = (jQuery(window).height() - dialog.height()) / 2;
19 | var offsetLeft = (jQuery(window).width() - dialog.width()) / 2;
20 | dialog.offset({top: offsetTop, left: offsetLeft});
21 | }
22 |
23 | function removeDialog(){
24 | jQuery('.dialog').remove();
25 | jQuery('.graybox').hide();
26 | }
27 |
--------------------------------------------------------------------------------
/weblocks-jquery.js:
--------------------------------------------------------------------------------
1 | /*!
2 | * Weblocks-jQuery - javascript helper functions for Weblocks
3 | * v0.2.0
4 | *
5 | * https://github.com/html/weblocks-jquery
6 | */
7 |
8 | // Taken from http://css-tricks.com/snippets/jquery/serialize-form-to-json/
9 | jQuery.fn.serializeObject = function()
10 | {
11 | var o = {};
12 | var a = this.serializeArray();
13 | jQuery.each(a, function() {
14 | if (o[this.name]) {
15 | if (!o[this.name].push) {
16 | o[this.name] = [o[this.name]];
17 | }
18 | o[this.name].push(this.value || '');
19 | } else {
20 | o[this.name] = this.value || '';
21 | }
22 | });
23 | return o;
24 | };
25 |
26 | /*
27 | * This prevents javascript error, but does not any effect like with usual weblocks flashes.
28 | */
29 | window.Effect = Effect = {
30 | Pulsate: function(){ return {};},
31 | BlindUp: function(){ return {};}
32 | };
33 |
34 | /*
35 | * This prevents javascript error and replaces weblocks focusFirstElement form functionality
36 | */
37 | jQuery.fn.focusFirstElement = function(){
38 | if(jQuery(this).length){
39 | jQuery(this).find('input:first').focus();
40 | }
41 | };
42 |
43 | jQuery.fn.serializeObjectWithSubmit = function(){
44 | var ret = this.serializeObject();
45 | var submitElement = jQuery(this).find('input[type=submit][clicked=true]');
46 | ret[submitElement.attr('name')] = submitElement.val();
47 | submitElement.attr('clicked', null);
48 |
49 | return ret;
50 | };
51 |
52 | // Utilities
53 | function updateElementBody(element, newBody) {
54 | element.update(newBody);
55 | }
56 |
57 | function updateElement(element, newElement) {
58 | var $newElement = jQuery(newElement);
59 | element.replaceWith($newElement);
60 | }
61 |
62 | function applySubmitClickEvent() {
63 | jQuery("form input[type=submit]")
64 | .unbind('click.weblocks-submit-event')
65 | .bind('click.weblocks-submit-event', function() {
66 | $("input[type=submit]", $(this).parents("form")).removeAttr("clicked");
67 | $(this).attr("clicked", "true");
68 | });
69 | }
70 |
71 | function selectionEmpty() {
72 | if(document.getSelection) {
73 | return document.getSelection() == "";
74 | } else if(document.selection && document.selection.createRange) {
75 | return document.selection.createRange().text == "";
76 | } else {
77 | return true;
78 | }
79 | }
80 |
81 | function addCss(cssCode) {
82 | var styleElement = document.createElement("style");
83 | styleElement.type = "text/css";
84 | if (styleElement.styleSheet) {
85 | styleElement.styleSheet.cssText = cssCode;
86 | } else {
87 | styleElement.appendChild(document.createTextNode(cssCode));
88 | }
89 | document.getElementsByTagName("head")[0].appendChild(styleElement);
90 | }
91 |
92 | function stopPropagation(event) {
93 | if(event.preventDefault) {
94 | event.stopPropagation();
95 | } else {
96 | event.cancelBubble = true;
97 | };
98 | }
99 |
100 | function startProgress(){
101 | jQuery('#ajax-progress').html("
");
102 | }
103 |
104 | function stopProgress(){
105 | jQuery('#ajax-progress').html("");
106 | }
107 |
108 | // Register global AJAX handlers to show progress
109 | jQuery(document).ajaxStart(function() {
110 | try{
111 | startProgress();
112 | }catch(e){
113 | window.console && console.log(e, e.message);
114 | }
115 | });
116 |
117 | jQuery(document).ajaxStop(function() {
118 | stopProgress();
119 | });
120 |
121 | Object.values = function (obj) {
122 | var vals = [];
123 | for( var key in obj ) {
124 | if ( obj.hasOwnProperty(key) ) {
125 | vals.push(obj[key]);
126 | }
127 | }
128 | return vals;
129 | }
130 |
131 | function dumpTree(tree, deepness){
132 | if(!deepness){
133 | deepness = 1;
134 | }
135 | var prefix = '';
136 | for(var i=0;i=0;i--) {
422 | if (classes[i] != myClass)
423 | result.push(classes[i]);
424 | }
425 |
426 | el.setAttribute('class', result.join(" ")); /* FIXME: ie6/7 need className here */
427 | }
428 |
429 | function hasClass(el, myClass){
430 | if ((el.className === null) || (typeof el == 'undefined'))
431 | return false;
432 |
433 | var classes = el.className.split(" ");
434 |
435 | for (i=classes.length;i>=0;i--) {
436 | if (classes[i] == myClass)
437 | return true;
438 | }
439 |
440 | return false;
441 | }
442 |
443 | /* collapsible sections */
444 | function toggleExpandCollapse (heading,container) {
445 | if (hasClass(heading,"collapsed")) {
446 | removeClass(heading,"collapsed");
447 | removeClass(container,"collapsed");
448 | addClass(heading,"expanded");
449 | addClass(container,"expanded");
450 | } else {
451 | removeClass(heading,"expanded");
452 | removeClass(container,"expanded");
453 | addClass(heading,"collapsed");
454 | addClass(container,"collapsed");
455 | }
456 | }
457 |
458 | function updateWidgetStateFromHash() {
459 | libraryMissingWarning('updateWidgetStateFromHash');
460 |
461 | jQuery.getScript("/pub/scripts/jquery.ba-bbq.js", function(){
462 | $(window).bind('hashchange', function(event){
463 | var hash = window.location.hash;
464 | if (hash)
465 | initiateActionWithArgs(null, null, {'weblocks-internal-location-hash':hash}, "GET", "/");
466 | }).trigger('hashchange');
467 | }).error(function(){
468 | if(!jQuery.bbq){
469 | window.console && console.log("It seems that jQuery BBQ library is missing, hashchange event will not be dispatched by weblocks");
470 | }
471 | });
472 | }
473 |
474 | function setLocationHash (hash) {
475 | window.location.hash = hash;
476 | }
477 |
478 | function libraryMissingWarning(feature){
479 | if(!window.withScripts){
480 | window.console && console.log("Please use javascript library https://github.com/html/jquery-seq to use " + feature + " functionality");
481 | return;
482 | }
483 | }
484 |
485 | $ = function(id){
486 | if(typeof(id) == 'string'){
487 | return jQuery('#' + id);
488 | }else{
489 | return jQuery(id);
490 | }
491 | };
492 |
493 | jQuery(function(){
494 | applySubmitClickEvent();
495 | });
496 |
497 |
498 | window.Event.observe = function(obj, evtType, func){
499 | if(obj == window && evtType == 'load'){
500 | jQuery(func);
501 | }else{
502 | window.console && console.log("Don't know what to do for " + obj + " and event type " + evtType);
503 | }
504 | };
505 |
506 | /*
507 | * Useful for waiting until element is appeared in html, usage is
508 | *
509 | * // Init code for input loaded with ajax
510 | * $('#some-input-should-be-loaded-with-ajax').onAvailable(function(){
511 | * console.log("Hey, I'm loaded for 100%, now you don't have bugs with me");
512 | * });
513 | *
514 | */
515 | jQuery.fn.onAvailable = function(fn){
516 | var sel = this.selector;
517 | var timer;
518 | var self = this;
519 | if (this.length > 0) {
520 | fn.call(this);
521 | }
522 | else {
523 | timer = setInterval(function(){
524 | if (jQuery(sel).length > 0) {
525 | fn.call(jQuery(sel));
526 | clearInterval(timer);
527 | }
528 | },50);
529 | }
530 | }
531 |
--------------------------------------------------------------------------------