1.  
  2. (function($) {
  3. var i18n_js;
  4. $.fn.DataObjectManager = function() {
  5.     this.each(function() {
  6.         $.fn.DataObjectManager.init(this);
  7.     });
  8. };
  9.  
  10. $.fn.DataObjectManager.init = function(obj) {
  11.         var $container = $(obj);
  12.         var container_id = '#'+$container.attr('id');
  13.         var nested = $('.DataObjectManager').hasClass('isNested');
  14.         if(!i18n_js)
  15.             i18n_js = $.fn.DataObjectManager.loadi18n();
  16.        
  17.         var facebox_close = function() {           
  18.             $('#facebox').fadeOut(function() {
  19.                 $('#facebox .content').removeClass().addClass('content');
  20.                 $('#facebox_overlay').remove();
  21.                 $('#facebox .loading').remove();
  22.                 refresh($container, $container.attr('href'));      
  23.             })
  24.         };
  25.        
  26.         // Popup links
  27.        
  28.         // For Nested DOMs
  29.         if(nested) {
  30.  
  31.       // Change 1
  32.       // Cache iframe html in a variable so it can be accessed later
  33.       var $mainIframe = $('<div id="iframe_'+$container.attr('id')+'" class="iframe_wrap" style="display:none;"><a href="javascript:void(0)" class="nested-close">close</a><iframe src="" frameborder="0" height="1"></iframe></div>');
  34.       $('body').append(
  35.          $mainIframe
  36.       );
  37.       // End of change
  38.  
  39.       var $iframeWrap = $('#iframe_'+$container.attr('id'));
  40.         $container.find('a.popup-button').unbind('click').click(function(e) {
  41.           $link = $(this);
  42.                 var $iframe = $iframeWrap.find('iframe');
  43.         $iframe.attr('src',$link.attr('href'));
  44.         //$('body').css({'opacity':.3});
  45.  
  46.         // Change 2
  47.         // Get the iframe width from the rel
  48.         var w = $(this).attr('rel');
  49.         w = w ? w : 430;
  50.         var width = new String(w) + 'px';
  51.         // Set the iframe width to width
  52.         $mainIframe.find('iframe:first').css({width:width});
  53.         // end of change
  54.  
  55.         top = $.fn.DataObjectManager.getPageScroll()[1] + ($.fn.DataObjectManager.getPageHeight() / 10);
  56.         $iframeWrap.show().css({
  57.             'position':'absolute',
  58.             'z-index':'999',
  59.             'left':'50%',
  60.             'top' : top,
  61.             // Change 3
  62.             // Set iframe wrapper width and margin-left
  63.             'width': width,
  64.             'margin-left':'-'+(parseInt(w) / 2)+'px'
  65.             // end of changes
  66.         }).addClass('loading');
  67.         $iframe.load(function() {
  68.             iframe_height = $iframe.contents().find('body').height()+36;
  69.             $iframe.attr('height',iframe_height);
  70.             $iframeWrap.removeClass('loading');
  71.         });
  72.         return false;
  73.       });
  74.       $('#iframe_'+$container.attr('id')).find('.nested-close').unbind('click').click(function(e) {
  75.         $iframeWrap.hide();
  76.         $iframeWrap.find('iframe').attr('src','');
  77.         refresh($container,$container.attr('href'));
  78.         e.stopPropagation();
  79.         return false;
  80.       });
  81.             $container.addClass("loaded");         
  82.         }
  83.         // For normal DOMs
  84.         else {
  85.         $container.find('a.popup-button').unbind('click').click(function(e) {
  86.             $(document).unbind('close.facebox').bind('close.facebox', facebox_close);
  87.             w = $(this).attr('rel');
  88.             width = new String(w);
  89.             height = $(this).attr('rel') == 'duplicate' ? 180 : ($.fn.DataObjectManager.getPageHeight()*.6);
  90.             $.facebox('<iframe src="'+$(this).attr('href')+'" frameborder="0" width="'+width+'" height="' + height + '"></iframe>');
  91.             e.stopPropagation();
  92.             return false;
  93.         });
  94.         }
  95.         // Delete
  96.     $deletes = $container.find('a.delete-link');
  97.         $deletes.unbind('click').click(function(e) {
  98.         $('.delete_dialog').remove();
  99.         params = $('#SecurityID') ? {'forceajax' : '1', 'SecurityID' : $('#SecurityID').attr('value')} : {'forceajax' : '1'};
  100.         $target = $(this);
  101.  
  102.             if($(this).attr('rel') == "confirm") {
  103.               $div = $('<div class="delete_dialog">'
  104.                        +i18n_js.delete_confirm
  105.                        +' <a class="yes" href="javascript:void(0)"><img src="dataobject_manager/images/accept.png" alt="yes" /></a> '
  106.                        +' <a class="no" href="javascript:void(0)"><img src="dataobject_manager/images/cancel.png" alt="no"/></a> '
  107.                        +'</div>'
  108.               ).click(function(e) {return false;e.stopPropagation()});
  109.  
  110.               $(this).parents('div:first').append($div);
  111.               height = $(this).parents('li').height();
  112.               $(this).parents('li').css({
  113.                'height' : height+'px',
  114.                'overflow' : 'visible'
  115.               });
  116.               $div.fadeIn("slow");
  117.               $div.find('.yes').click(function(e) {
  118.                 $.post($target.attr('href'),params,function() {$($target).parents('li:first').fadeOut();$(".ajax-loader").fadeOut("fast");});        
  119.           e.stopPropagation();
  120.                 return false;
  121.               });
  122.               $div.find('.no').click(function(e) {
  123.                $(this).parent().remove().parents('li').css({
  124.                  'height' : 'auto',
  125.                  'overflow' : 'hidden'
  126.                });
  127.                e.stopPropagation();
  128.                return false;
  129.               })
  130.             }
  131.             else {
  132.             $.post($target.attr('href'),params,function() {$($target).parents('li:first').fadeOut();$(".ajax-loader").fadeOut("fast");});
  133.       }
  134.           return false;
  135.         });
  136.        
  137.         // Refresh
  138.        
  139.         $container.find('a.refresh-button').unbind('click').click(function(e) {
  140.             $t = $(this);
  141.             $.post($t.attr('href'),{},function() {
  142.                 refresh($container, $container.attr('href'));
  143.             });
  144.             return false;
  145.         });
  146.                
  147.  
  148.         // Pagination
  149.         $container.find('.Pagination a').unbind('click').click(function() {
  150.             refresh($container, $(this).attr('href'));
  151.             return false;
  152.         });
  153.        
  154.         // View
  155.         if($container.hasClass('FileDataObjectManager') && !$container.hasClass('ImageDataObjectManager')) {
  156.             $container.find('a.viewbutton').unbind('click').click(function() {
  157.                 refresh($container, $(this).attr('href'));
  158.                 return false;
  159.             });
  160.         }
  161.        
  162.        
  163.  
  164.         // Sortable
  165.         $container.find('.sort-control input').unbind('click').click(function(e) {
  166.             refresh($container, $(this).attr('value'));
  167.             $(this).attr('disabled', true);
  168.             e.stopPropagation();
  169.         });
  170.         $container.find("ul[class^='sortable-']").sortable({
  171.             update : function(e) {
  172.                 $list = $(this);
  173.                 do_class = $.trim($list.attr('class').replace('sortable-','').replace('ui-sortable',''));
  174.                 type = $container.hasClass('ManyMany') ? $container.find('input[name=controllerID]').val() : '';
  175.                 $.post('DataObjectManager_Controller/dosort/'+do_class+'/'+type, $list.sortable("serialize"));
  176.                 e.stopPropagation();
  177.             },
  178.             items : 'li:not(.head)',
  179.             containment : 'document',
  180.             tolerance : 'intersect',
  181.             handle : ($('.list-holder').hasClass('grid') ? '.handle' : null)
  182.         });
  183.        
  184.         // Click function for the LI
  185.         $container.find('ul:not(.ui-sortable) li.data').unbind('click').click(function(e) {
  186.           $(this).find('a.popup-button:first').click();
  187.           e.stopPropagation();
  188.         }).css({'cursor' : 'pointer'});
  189.        
  190.        
  191.         // Column sort
  192.         if(!$container.hasClass('ImageDataObjectManager')) {
  193.             $container.find('li.head a').unbind('click').click(function() {
  194.                 refresh($container, $(this).attr('href'));
  195.                 return false;
  196.             });
  197.         }
  198.        
  199.         // Filter
  200.         $container.find('.dataobjectmanager-filter select').unbind('change').change(function(e) {
  201.             refresh($container, $(this).attr('value'));
  202.         });
  203.  
  204.         // Page size
  205.         $container.find('.per-page-control select').unbind('change').change(function(e) {
  206.             refresh($container, $(this).attr('value'));
  207.         });
  208.  
  209.        
  210.         // Refresh filter
  211.         $container.find('.dataobjectmanager-filter .refresh').unbind('click').click(function(e) {
  212.             refresh($container, $container.attr('href'));
  213.             e.stopPropagation();
  214.             return false;
  215.         })
  216.    
  217.         // Search
  218.         var request = false;
  219.         $container.find('#srch_fld').focus(function() {
  220.             if($(this).attr('value') == "Search") $(this).attr('value','').css({'color' : '#333'});
  221.         }).unbind('blur').blur(function() {
  222.             if($(this).attr('value') == '') $(this).attr('value','Search').css({'color' : '#666'});
  223.         }).unbind('keyup').keyup(function(e) {
  224.        
  225.         if ((e.keyCode == 9) || (e.keyCode == 13) || // tab, enter
  226.            (e.keyCode == 16) || (e.keyCode == 17) || // shift, ctl
  227.            (e.keyCode >= 18 && e.keyCode <= 20) || // alt, pause/break, caps lock
  228.            (e.keyCode == 27) || // esc
  229.            (e.keyCode >= 33 && e.keyCode <= 35) || // page up, page down, end
  230.            (e.keyCode >= 36 && e.keyCode <= 38) || // home, left, up
  231.             (e.keyCode == 40) || // down
  232.            (e.keyCode >= 36 && e.keyCode <= 40) || // home, left, up, right, down
  233.            (e.keyCode >= 44 && e.keyCode <= 45) || // print screen, insert
  234.            (e.keyCode == 229) // Korean XP fires 2 keyup events, the key and 229
  235.         ) return;
  236.                
  237.                 if(request) window.clearTimeout(request);
  238.                 $input = $(this);
  239.                 request = window.setTimeout(function() {
  240.                     url = $(container_id).attr('href').replace(/\[search\]=(.)*?&/, '[search]='+$input.attr('value')+'&');
  241.           refresh($container, url, '#srch_fld');
  242.                    
  243.                 },500)
  244.             e.stopPropagation();
  245.         });
  246.        
  247.         $container.find('#srch_clear').unbind('click').click(function() {
  248.             $container.find('#srch_fld').attr('value','').keyup();
  249.         });
  250.        
  251.  
  252.     $container.find('a.tooltip').tooltip({
  253.           delay: 500,
  254.           showURL: false,
  255.           track: true,
  256.           bodyHandler: function() {
  257.               return $(this).parents('li').find('span.tooltip-info').html();
  258.           }
  259.     });
  260.    
  261.    
  262.     // Add the slider to the ImageDataObjectManager
  263.     if($container.hasClass('ImageDataObjectManager')) {
  264.             var MIN_IMG_SIZE = 25
  265.             var MAX_IMG_SIZE = 300;
  266.             var START_IMG_SIZE = 100;
  267.             var new_image_size;
  268.             $('.size-control').slider({
  269.                
  270.                 // Stupid thing doesn't work. Have to force it with CSS
  271.                 //startValue : (START_IMG_SIZE - MIN_IMG_SIZE) / ((MAX_IMG_SIZE - MIN_IMG_SIZE) / 100),
  272.                 slide : function(e, ui) {
  273.                     new_image_size = MIN_IMG_SIZE + (ui.value * ((MAX_IMG_SIZE - MIN_IMG_SIZE)/100));
  274.                     $('.grid li img.image').css({'width': new_image_size+'px'});
  275.                     $('.grid li').css({'width': new_image_size+'px', 'height' : new_image_size +'px'});
  276.                 },
  277.                
  278.                 stop : function(e, ui) {
  279.                     new_image_size = MIN_IMG_SIZE + (ui.value * ((MAX_IMG_SIZE - MIN_IMG_SIZE)/100));              
  280.                     url = $(container_id).attr('href').replace(/\[imagesize\]=(.)*/, '[imagesize]='+Math.floor(new_image_size));
  281.                     refresh($container, url);
  282.                 }
  283.             });
  284.            
  285.             $('.ui-slider-handle').css({'left' : $('#size-control-wrap').attr('class').replace('position','')+'px'});    
  286.    
  287.     }  
  288.     // RelationDataObjectManager
  289.    
  290.     if($container.hasClass('RelationDataObjectManager')) {
  291.             var $checkedList = $(container_id+'_CheckedList');
  292.             $container.find('.actions input, .file-label input').unbind('click').click(function(e){
  293.                 if($(this).attr('type') == "radio") {
  294.                     $(this).parents('li').siblings('li').removeClass('selected');
  295.                     $(this).parents('li').toggleClass('selected');
  296.                     $checkedList.attr('value', ","+$(this).val()+",");
  297.                 }
  298.                 else {
  299.                     $(this).parents('li').toggleClass('selected');
  300.                     val = ($(this).attr('checked')) ? $checkedList.val() + $(this).val()+"," : $checkedList.val().replace(","+$(this).val()+",",",");
  301.                     $checkedList.attr('value', val);
  302.                 }
  303.                 e.stopPropagation();
  304.             });
  305.    
  306.             $container.find('.actions input, .file-label input').each(function(i,e) {
  307.                 if($checkedList.val().indexOf(","+$(e).val()+",") != -1)
  308.                     $(e).attr('checked',true).parents('li').addClass('selected');
  309.                 else
  310.                     $(e).attr('checked',false).parents('li').removeClass('selected');
  311.                    
  312.             });
  313.            
  314.             $container.find('a[rel=clear]').unbind('click').click(function(e) {
  315.              $container.find('.actions input, .file-label input').each(function(i,e) {
  316.                $(e).attr('checked', false).parents('li').removeClass('selected');
  317.                $checkedList.attr('value','');
  318.              });
  319.             });
  320.            
  321.         $container.find('.only-related-control input').unbind('click').click(function(e) {
  322.             refresh($container, $(this).attr('value'));
  323.             $(this).attr('disabled', true);
  324.             e.stopPropagation();
  325.         });
  326.                
  327.     }
  328.        
  329.     // Columns. God forbid there are more than 10.
  330.     cols = $('.list #dataobject-list li.head .fields-wrap .col').length;
  331.     if(cols > 10) {
  332.         $('.list #dataobject-list li .fields-wrap .col').css({'width' : ((Math.floor(100/cols)) - 0.1) + '%' });
  333.     }
  334.        
  335.  
  336. };
  337.  
  338. $.fn.DataObjectManager.getPageHeight = function() {
  339.     var windowHeight
  340.     if (self.innerHeight) { // all except Explorer
  341.       windowHeight = self.innerHeight;
  342.     } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
  343.       windowHeight = document.documentElement.clientHeight;
  344.     } else if (document.body) { // other Explorers
  345.       windowHeight = document.body.clientHeight;
  346.     }  
  347.     return windowHeight;
  348. };
  349.  
  350. $.fn.DataObjectManager.getPageScroll = function() {
  351.     var xScroll, yScroll;
  352.     if (self.pageYOffset) {
  353.       yScroll = self.pageYOffset;
  354.       xScroll = self.pageXOffset;
  355.     } else if (document.documentElement && document.documentElement.scrollTop) {     // Explorer 6 Strict
  356.       yScroll = document.documentElement.scrollTop;
  357.       xScroll = document.documentElement.scrollLeft;
  358.     } else if (document.body) {// all other Explorers
  359.       yScroll = document.body.scrollTop;
  360.       xScroll = document.body.scrollLeft;  
  361.     }
  362.     return new Array(xScroll,yScroll)
  363. };
  364.  
  365. $.fn.DataObjectManager.loadi18n = function() {
  366.   var json = null;
  367.   $.ajax({
  368.     'async': false,
  369.     'global': false,
  370.     'url': 'DataObjectManager_Controller/i18n_js/',
  371.     'dataType': "json",
  372.     'success': function (data) {
  373.         json = data;
  374.     }
  375.   });
  376.   return json;
  377. }
  378.  
  379.  
  380.  
  381. $().ajaxSend(function(r,s){  
  382.  $(".ajax-loader").show();  
  383. });  
  384.    
  385. $().ajaxStop(function(r,s){  
  386.   $(".ajax-loader").fadeOut("fast");  
  387. });
  388. $('.DataObjectManager').livequery(function(){
  389.    $(this).DataObjectManager();                          
  390. });
  391.  
  392. })(jQuery);
  393.  
  394.  
  395. function refresh($div, link, focus)
  396. {
  397.      // Kind of a hack. Pass the list of ids to the next refresh
  398.      var listValue = ($div.hasClass('RelationDataObjectManager')) ? jQuery('#'+$div.attr('id')+'_CheckedList').val() : false;
  399.          
  400.      jQuery.ajax({
  401.        type: "GET",
  402.        url: link,
  403.        success: function(html){
  404.             if(!$div.next().length && !$div.prev().length)
  405.                 $div.parent().html(html);
  406.             else
  407.                 $div.replaceWith(html);
  408.            
  409.             if(listValue) {
  410.                  jQuery('#'+$div.attr('id')+'_CheckedList').attr('value',listValue);
  411.             }
  412.      var $container = jQuery('#'+$div.attr('id'));
  413.      $container.DataObjectManager();
  414.      if (typeof focus == 'string') {
  415.        $container.find(focus).focus();
  416.      }         
  417.             //jQuery('#'+$div.attr('id')).DataObjectManager();
  418.         }
  419.      });
  420. }