Categories
discuss

How do I update an SVG text element using Snap.svg?

I’m using Snap.svg to load an SVG image and add text elements to it. However, I need to be able to update the text elements as the page loads.

Right now, I’m doing something like this:

var svg = Snap("#selector");
var text;
Snap.load(path_to_file, function(f) {
    svg.append(f);
    var g = svg.select("g");
    text = g.text(x_pos, y_pos, label);
}

Let’s say I want to update the text later, how do I do this? I am guaranteed to update the text object after it’s been created after calling load.

The only way I’ve managed to modify the text is by setting an id to the element and then doing it with jQuery like this:

self.selector.find("#my_id")[0].textContent = "New text";

However, this feels really wrong and I think I might just be missing something with the Snap API.

Answer

I think it should be as simple as

text.attr({ text: 'my new text'});

so

setTimeout( function() { text.attr({ text: 'my new text'}) }, 2000 );

would test it

Categories
discuss

Jquery param alternative for javascript

I need to convert following type dictionary:

{'key1': ['value1'], 'key2': ['value1', 'value2']}

to key1=value1&key2=....

i.e. post data form. I am doing this inside chrome extention, the above dictionary of formdata is returned by:

chrome.webRequest.onBeforeRequest.addListener(function(details) {
      if(details.method=="POST")         // ajax call
      {
        message.postdata = details.requestBody.formData;
      }
      return {requestHeaders: details.requestHeaders};
 }, {urls: ["<all_urls>"],types: ["main_frame", "sub_frame"]}, ["blocking", "requestBody"]);

I remeber achieving the same using JQuery $.params() function. How can the same be done in javascript.

Answer

function queryParams(source) {
  var array = [];

  for(var key in source) {
     array.push(encodeURIComponent(key) + "=" + encodeURIComponent(source[key]));
  }

  return array.join("&");
}
Categories
discuss

SocketConnector 8081 – Address Already in use

I am trying to run 2 Dropwizard Server applications on 2 different ports 8080 and 9000.The first application starts successfully but I keep getting the below exception when I try to run on port 9000. What I am not understanding is that why are 2 ports being used by the application and how do I enforce my application to use a different port number for the second port

INFO  [2014-03-22 17:17:28,031] org.eclipse.jetty.server.AbstractConnector: Started    
InstrumentedBlockingChannelConnector@0.0.0.0:9000 
WARN  [2014-03-22 17:17:28,033] org.eclipse.jetty.util.component.AbstractLifeCycle: FAILED   
SocketConnector@0.0.0.0:8081: java.net.BindException: Address already in use
! java.net.BindException: Address already in use
! at org.eclipse.jetty.server.bio.SocketConnector.newServerSocket(SocketConnector.java:96)    
~[jetty-server-8.1.10.v20130312.jar:8.1.10.v20130312]
! at org.eclipse.jetty.server.bio.SocketConnector.open(SocketConnector.java:85) ~[jetty-
server-8.1.10.v20130312.jar:8.1.10.v20130312]
! at org.eclipse.jetty.server.AbstractConnector.doStart(AbstractConnector.java:316) ~
[jetty-server-8.1.10.v20130312.jar:8.1.10.v20130312]
! at org.eclipse.jetty.server.bio.SocketConnector.doStart(SocketConnector.java:156) ~
[jetty-server-8.1.10.v20130312.jar:8.1.10.v20130312]
! at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64) 
[jetty-util-8.1.10.v20130312.jar:8.1.10.v20130312]
! at org.eclipse.jetty.server.Server.doStart(Server.java:291) [jetty-server-
8.1.10.v20130312.jar:8.1.10.v20130312]
! at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64) 
[jetty-util-8.1.10.v20130312.jar:8.1.10.v20130312]
! at com.yammer.dropwizard.cli.ServerCommand.run(ServerCommand.java:48) [dropwizard-core-
0.6.2.jar:na]
! at com.yammer.dropwizard.cli.EnvironmentCommand.run(EnvironmentCommand.java:39) 
[dropwizard-core-0.6.2.jar:na]
! at com.yammer.dropwizard.cli.ConfiguredCommand.run(ConfiguredCommand.java:58) 
[dropwizard-core-0.6.2.jar:na]
! at com.yammer.dropwizard.cli.Cli.run(Cli.java:53) [dropwizard-core-0.6.2.jar:na]
! at com.yammer.dropwizard.Service.run(Service.java:61) [dropwizard-core-0.6.2.jar:na]
! at com.paypal.demandgen.places.indexing.IndexingServer.main(IndexingServer.java:96)    
[classes/:na]
WARN  [2014-03-22 17:17:28,034] org.eclipse.jetty.util.component.AbstractLifeCycle: FAILED or

Answer

Dropwizard actually serves up two sites: the main site, and an admin site on a different port. The admin section allows you to view metrics and healthchecks on your site.

In this instance, the problem is due to the Admin client, which by default runs on port 8081. So you need to also set -Ddw.http.adminPort=9001 (for instance)

Categories
discuss

Cannot instantiate class android.support.v7.widget.SearchView

Please somebody help me! I trying add appcompat search view to my project. Unsuccessfully. When I change android.support.v7.widget.SearchView to SearchView (API > 10) everything’s fine.

This my menu res:

<item android:id="@+id/action_search"
    android:title="@string/search_view_title"
    android:icon="@drawable/action_search"
    sabd:showAsAction="always|collapseActionView"
    sabd:actionViewClass="android.support.v7.widget.SearchView" />

This how I try to use SeachView:

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.teach, menu);
        mSearchItem = menu.findItem(R.id.action_search);
        mSearchItem.setVisible(true);
        mSearchView = (SearchView) MenuItemCompat.getActionView(mSearchItem);
        mSearchView.setQueryHint(getString(R.string.search_view_hint));
        mSearchView.setOnQueryTextListener(this);
    }

This my manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="ru.rgups.time"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="10"
        android:targetSdkVersion="17" />

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/Theme.Base.AppCompat.Light"
        android:name="RTApplication">
        <activity
            android:name="ru.rgups.time.MainActivity"
            android:configChanges="orientation|keyboardHidden"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".fragments.WelcomeActivity"></activity>
        <service android:name=".spice.SampleSpiceService"></service>
        <activity android:name=".activities.AuthActivity"></activity>
        <activity 
            android:name=".activities.HomeWorkActivity"
            android:uiOptions="splitActionBarWhenNarrow">

        </activity>

        <receiver android:name="ru.rgups.time.receiver.HomeWorkNotificationReceiver">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>
        </receiver>

    </application>

</manifest>

And this what I see in logs:

   03-22 21:46:58.583: W/SupportMenuInflater(28798): Cannot instantiate class: android.support.v7.widget.SearchView
    03-22 21:46:58.583: W/SupportMenuInflater(28798): java.lang.reflect.InvocationTargetException
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at java.lang.reflect.Constructor.constructNative(Native Method)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at android.support.v7.internal.view.SupportMenuInflater$MenuState.newInstance(SupportMenuInflater.java:482)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at android.support.v7.internal.view.SupportMenuInflater$MenuState.setItem(SupportMenuInflater.java:441)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at android.support.v7.internal.view.SupportMenuInflater$MenuState.addItem(SupportMenuInflater.java:462)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at android.support.v7.internal.view.SupportMenuInflater.parseMenu(SupportMenuInflater.java:196)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at android.support.v7.internal.view.SupportMenuInflater.inflate(SupportMenuInflater.java:118)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at ru.rgups.time.fragments.TeachersListFragment.onCreateOptionsMenu(TeachersListFragment.java:109)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at android.support.v4.app.Fragment.performCreateOptionsMenu(Fragment.java:1582)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at android.support.v4.app.FragmentManagerImpl.dispatchCreateOptionsMenu(FragmentManager.java:1956)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:225)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at android.support.v7.app.ActionBarActivity.superOnCreatePanelMenu(ActionBarActivity.java:232)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at android.support.v7.app.ActionBarActivityDelegateICS.onCreatePanelMenu(ActionBarActivityDelegateICS.java:147)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at android.support.v7.app.ActionBarActivity.onCreatePanelMenu(ActionBarActivity.java:199)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at android.support.v7.app.ActionBarActivityDelegateICS$WindowCallbackWrapper.onCreatePanelMenu(ActionBarActivityDelegateICS.java:285)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:436)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at com.android.internal.policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:800)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:221)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at android.view.Choreographer.doCallbacks(Choreographer.java:574)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at android.view.Choreographer.doFrame(Choreographer.java:543)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at android.os.Handler.handleCallback(Handler.java:733)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at android.os.Handler.dispatchMessage(Handler.java:95)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at android.os.Looper.loop(Looper.java:136)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at android.app.ActivityThread.main(ActivityThread.java:5017)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at java.lang.reflect.Method.invokeNative(Native Method)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at java.lang.reflect.Method.invoke(Method.java:515)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at dalvik.system.NativeStart.main(Native Method)
    03-22 21:46:58.583: W/SupportMenuInflater(28798): Caused by: android.view.InflateException: Binary XML file line #41: Error inflating class <unknown>
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at android.view.LayoutInflater.createView(LayoutInflater.java:620)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at android.support.v7.widget.SearchView.<init>(SearchView.java:262)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at android.support.v7.widget.SearchView.<init>(SearchView.java:254)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  ... 31 more
    03-22 21:46:58.583: W/SupportMenuInflater(28798): Caused by: java.lang.reflect.InvocationTargetException
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at java.lang.reflect.Constructor.constructNative(Native Method)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at android.view.LayoutInflater.createView(LayoutInflater.java:594)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  ... 39 more
    03-22 21:46:58.583: W/SupportMenuInflater(28798): Caused by: android.content.res.Resources$NotFoundException: Resource is not a Drawable (color or path): TypedValue{t=0x2/d=0x7f010069 a=-1}
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at android.content.res.Resources.loadDrawable(Resources.java:2068)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at android.widget.ImageView.<init>(ImageView.java:129)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  at android.widget.ImageView.<init>(ImageView.java:119)
    03-22 21:46:58.583: W/SupportMenuInflater(28798):  ... 42 more

Answer

Problem was in bad v7 support library. After update everything fine.

Categories
discuss

Angularjs datepicker popup bootstrap-ui does not work in view

I am an angularjs and bootstrap ui newbie! My date picker popup is not working in my included view. It works if its outside the view. i.e. as demoed here Any assistance will be appreciated. But, because I am using the ngRoute approach, I need to compose my view in the partial html and include in the main index.html. The inline calendar does not show and the popup calendar does not show as well. I cannot figure out the problem, but I think the date-picker.html cannot reference the script and style in my index.html. I have other included pane in the date-picker hmtl which references the scripts and css in the index.html fine. Any assistance will be appreciated. Below is the code:

**index.html**

    <!doctype html>
    <html lang="en" ng-app="ngdemo">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Some Services</title>
        <link rel="stylesheet" href="css/app.css"/>
        <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.8/angular.js"></script>
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0rc1/angular-route.min.js"></script>   
        <script src="js/app.js"></script>
        <script src="js/services.js"></script>
        <script src="js/controllers.js"></script>
        <script src="js/filters.js"></script>
        <script src="js/directives.js"></script>
        <script src="http://angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.10.0.js"></script>
        <link href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css" rel="stylesheet">

    </head>

    <body>

<ul class="menu">
    <li><a href="#/date-picker"> <b> Date Management </b> </a></li> 
</ul> 

    <div ng-view></div>

    </body>
    </html>

* app.js *

'use strict';

// Declare app level module which depends on filters, and services
angular.module('ngdemo', ['ngRoute','ngdemo.filters','ngdemo.services', 'ngdemo.directives', 'ngdemo.controllers']).
    config(['$routeProvider', function ($routeProvider) {
        $routeProvider.when('/date-picker', {templateUrl: 'partials/datepicker.html', controller: 'MixedContentCtrl'});
        $routeProvider.otherwise({redirectTo: '/date-picker'});
    }]);

controller.js *

'use strict';

/* Controllers */

var app = angular.module('ngdemo.controllers',['ui.bootstrap']);


//Clear browser cache (in development mode)

//http://stackoverflow.com/questions/14718826/angularjs-disable-partial-caching-on-dev-machine
app.run(function ($rootScope, $templateCache) {
    $rootScope.$on('$viewContentLoaded', function () {
        $templateCache.removeAll();
    });
});

var DatepickerDemoCtrl = function ($scope) {
  $scope.today = function() {
    $scope.dt = new Date();
  };
  $scope.today();

  $scope.showWeeks = true;
  $scope.toggleWeeks = function () {
    $scope.showWeeks = ! $scope.showWeeks;
  };

  $scope.clear = function () {
    $scope.dt = null;
  };

  // Disable weekend selection
  $scope.disabled = function(date, mode) {
    return ( mode === 'day' && ( date.getDay() === 0 || date.getDay() === 6 ) );
  };

  $scope.toggleMin = function() {
    $scope.minDate = ( $scope.minDate ) ? null : new Date();
  };
  $scope.toggleMin();

  $scope.open = function($event) {
    $event.preventDefault();
    $event.stopPropagation();

    $scope.opened = true;
  };

  $scope.dateOptions = {
    'year-format': "'yy'",
    'starting-day': 1
  };

  $scope.formats = ['dd-MMMM-yyyy', 'yyyy/MM/dd', 'shortDate'];
  $scope.format = $scope.formats[0];
};

datepicker.html **

<div ng-controller="DatepickerDemoCtrl">
    <pre>Selected date is: <em>{{dt | date:'fullDate' }}</em></pre>

    <h4>Inline</h4>
    <div style="display:inline-block; min-height:290px;">
      <div class="well well-sm" ng-model="dt">
          <datepicker min="minDate" show-weeks="showWeeks"></datepicker>
      </div>
    </div>

    <h4>Popup</h4>
    <div class="row">
        <div class="col-md-6">
            <p class="input-group">
              <input type="text" class="form-control" datepicker-popup="{{format}}" ng-model="dt" is-open="opened" min="minDate" max="'2015-06-22'" datepicker-options="dateOptions" date-disabled="disabled(date, mode)" ng-required="true" close-text="Close" />
              <span class="input-group-btn">
                <button class="btn btn-default" ng-click="open($event)"><i class="glyphicon glyphicon-calendar"></i></button>
              </span>
            </p>
        </div>
    </div>
    <div class="row">
        <div class="col-md-6">
            <label>Format:</label> <select class="form-control" ng-model="format" ng-options="f for f in formats"><option></option></select>
        </div>
    </div>

    <hr />
    <button class="btn btn-sm btn-info" ng-click="today()">Today</button>
    <button class="btn btn-sm btn-default" ng-click="dt = '2009-08-24'">2009-08-24</button>
    <button class="btn btn-sm btn-success" ng-click="toggleWeeks()" tooltip="For inline datepicker">Toggle Weeks</button>
    <button class="btn btn-sm btn-danger" ng-click="clear()">Clear</button>
    <button class="btn btn-sm btn-default" ng-click="toggleMin()" tooltip="After today restriction">Min date</button>
</div>

Answer

The culprit lies in

app.run(function ($rootScope, $templateCache) {
    $rootScope.$on('$viewContentLoaded', function () {
        $templateCache.removeAll();
    });
});

remove that and your date picker should work, I have a plunker here that is similar to your demo code.

Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..